opentelemetry.sdk.resources package

This package implements OpenTelemetry Resources:

A Resource is an immutable representation of the entity producing telemetry. For example, a process producing telemetry that is running in a container on Kubernetes has a Pod name, it is in a namespace and possibly is part of a Deployment which also has a name. All three of these attributes can be included in the Resource.

Resource objects are created with Resource.create, which accepts attributes (key-values). Resource attributes can also be passed at process invocation in the OTEL_RESOURCE_ATTRIBUTES environment variable. You should register your resource with the opentelemetry.sdk.metrics.MeterProvider and opentelemetry.sdk.trace.TracerProvider by passing them into their constructors. The Resource passed to a provider is available to the exporter, which can send on this information as it sees fit.

metrics.set_meter_provider(
    MeterProvider(
        resource=Resource.create({
            "service.name": "shoppingcart",
            "service.instance.id": "instance-12",
        }),
    ),
)
print(metrics.get_meter_provider().resource.attributes)

{'telemetry.sdk.language': 'python',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '0.13.dev0',
'service.name': 'shoppingcart',
'service.instance.id': 'instance-12'}

Note that the OpenTelemetry project documents certain “standard attributes” that have prescribed semantic meanings, for example service.name in the above example.

OTEL_RESOURCE_ATTRIBUTES

The OTEL_RESOURCE_ATTRIBUTES environment variable allows resource attributes to be passed to the SDK at process invocation. The attributes from OTEL_RESOURCE_ATTRIBUTES are merged with those passed to Resource.create, meaning OTEL_RESOURCE_ATTRIBUTES takes lower priority. Attributes should be in the format key1=value1,key2=value2. Additional details are available in the specification.

$ OTEL_RESOURCE_ATTRIBUTES="service.name=shoppingcard,will_be_overridden=foo" python - <<EOF
import pprint
from opentelemetry.sdk.resources import Resource
pprint.pprint(Resource.create({"will_be_overridden": "bar"}).attributes)
EOF
{'service.name': 'shoppingcard',
'telemetry.sdk.language': 'python',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '0.13.dev0',
'will_be_overridden': 'bar'}
class opentelemetry.sdk.resources.Resource(attributes)[source]

Bases: object

static create(attributes=None)[source]
Return type

Resource

static create_empty()[source]
Return type

Resource

property attributes
Return type

Dict[str, Union[str, bool, int, float]]

merge(other)[source]
Return type

Resource

class opentelemetry.sdk.resources.ResourceDetector(raise_on_error=False)[source]

Bases: abc.ABC

abstract detect()[source]
Return type

Resource

class opentelemetry.sdk.resources.OTELResourceDetector(raise_on_error=False)[source]

Bases: opentelemetry.sdk.resources.ResourceDetector

detect()[source]
Return type

Resource

opentelemetry.sdk.resources.get_aggregated_resources(detectors, initial_resource=None, timeout=5)[source]

Retrieves resources from detectors in the order that they were passed

Parameters
  • detectors (List[ResourceDetector]) – List of resources in order of priority

  • initial_resource (Optional[Resource]) – Static resource. This has highest priority

  • timeout – Number of seconds to wait for each detector to return

Return type

Resource

Returns