Otel.SDK.Resource (otel v0.2.0)

Copy Markdown View Source

Immutable representation of the entity producing telemetry (resource/sdk.md §"SDK").

A Resource is a set of key-value attributes describing the service (e.g., service.name, host.name) and an optional Schema URL. Resources are associated with TracerProvider/MeterProvider at creation time.

Environment-derived defaults

default/0 reads two environment variables per spec resource/sdk.md L172-L193 and merges their attributes on top of the SDK-provided base (telemetry.sdk.{name,language,version}):

  • OTEL_RESOURCE_ATTRIBUTES (L179-L189) — comma- separated key=value pairs. , and = characters in keys / values MUST be percent-encoded; we decode via Otel.API.Baggage.Percent.decode/1 (RFC 3986 §2.1). On any malformed pair the entire variable value is discarded per L191-L193 SHOULD.
  • OTEL_SERVICE_NAME (configuration/sdk-environment-variables.md L116) — populates service.name. Takes precedence over service.name from OTEL_RESOURCE_ATTRIBUTES when both are present. When neither is set the spec semantic-convention fallback "unknown_service" is used.

References

  • OTel Resource SDK: opentelemetry-specification/specification/resource/sdk.md
  • OTLP proto Resource: opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto
  • OTel SDK env vars: opentelemetry-specification/specification/configuration/sdk-environment-variables.md L116

Summary

Functions

Creates a new Resource from attributes and optional schema_url.

Returns the SDK-provided default Resource.

Merges two Resources.

Types

primitive()

@type primitive() ::
  String.t() | {:bytes, binary()} | boolean() | integer() | float() | nil

primitive_any()

@type primitive_any() ::
  primitive() | [primitive_any()] | %{required(String.t()) => primitive_any()}

t()

@type t() :: %Otel.SDK.Resource{
  attributes: %{required(String.t()) => primitive_any()},
  schema_url: String.t()
}

Functions

create(attributes, schema_url \\ "")

@spec create(
  attributes :: %{required(String.t()) => term()} | [{String.t(), term()}],
  schema_url :: String.t()
) :: t()

Creates a new Resource from attributes and optional schema_url.

default()

@spec default() :: t()

Returns the SDK-provided default Resource.

Includes telemetry.sdk.* attributes plus environment-derived attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME (see module doc ## Environment-derived defaults). When service.name is supplied by neither environment variable, the spec semantic-convention fallback "unknown_service" is used.

merge(old, updating)

@spec merge(old :: t(), updating :: t()) :: t()

Merges two Resources.

The updating resource's attribute values take precedence when keys overlap. Schema URL rules:

  • If old has empty schema_url → use updating's
  • If updating has empty schema_url → use old's
  • If both match → use that URL
  • If both differ → empty (merge conflict)