#OpenTelemetry Resource SDK

This package represents the entity producing telemetry and the detectors used to discover that information.

#Default detectors

Resource::builder() starts with three detectors:

  • sdk_provided_resource_detector()
  • telemetry_resource_detector()
  • env_resource_detector()

The default output always includes SDK metadata and a fallback service.name=unknown_service:<process_name> when the application does not provide one.

#Merge semantics

Resource::merge() prefers attributes from the right-hand resource. Schema URL handling follows OpenTelemetry resource merge rules:

  • same schema URL: keep it
  • only one schema URL present: keep the present one
  • conflicting schema URLs: clear it

#Environment variables

env_resource_detector() currently reads:

  • OTEL_RESOURCE_ATTRIBUTES
  • OTEL_SERVICE_NAME

OTEL_SERVICE_NAME takes precedence because its detected resource is merged after the generic resource attributes.

Resource

pub struct Resource {
attributes : Map[String,
Value
]
schema_url : String?
} derive(Eq, ToJson,
Debug
)

Immutable description of the entity producing telemetry. Spec: https://opentelemetry.io/docs/specs/otel/resource/data-model/#resource-data-model
impl Default for Resource

Resource::attributes

Returns a copy of all resource attributes. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#retrieve-attributes

Resource::builder

fn Resource::builder() -> ResourceBuilder

Starts building a resource with the SDK-provided, telemetry, and environment detectors enabled by default. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#create

Resource::builder_empty

fn Resource::builder_empty() -> ResourceBuilder

Starts building a resource without any default detectors. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#create

Resource::empty

fn Resource::empty() -> Resource

Returns an empty resource. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#the-empty-resource

Resource::get

Looks up one resource attribute. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#retrieve-attributes

Resource::is_empty

fn Resource::is_empty(self : Resource) -> Bool

Returns whether the resource carries no attributes. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#retrieve-attributes

Resource::len

fn Resource::len(self : Resource) -> Int

Returns the number of attributes stored in the resource. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#retrieve-attributes

Resource::merge

fn Resource::merge(self : Resource, other : Resource) -> Resource

Merges two resources, preferring attributes from other. Conflicting schema URLs clear the merged schema URL. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#merge

Resource::new

Creates a resource from explicit attributes. Later duplicate keys win. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#create

Resource::schema_url

fn Resource::schema_url(self : Resource) -> String?

Returns the optional schema URL attached to this resource. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#retrieve-attributes

ResourceBuilder

pub struct ResourceBuilder {
resource : Resource
detectors : Array[ResourceDetector]
}

Builder that combines explicit attributes with detector output. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceBuilder::build

Runs detectors in order and then merges in explicit builder attributes so direct configuration takes precedence. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceBuilder::with_attribute

Adds or replaces one attribute. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceBuilder::with_attributes

Adds or replaces multiple attributes. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceBuilder::with_detector

fn ResourceBuilder::with_detector(self : ResourceBuilder, detector : ResourceDetector) -> ResourceBuilder

Appends an additional detector that will run before explicit builder attributes are merged in. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#detecting-resource-information-from-the-environment

ResourceBuilder::with_schema_url

fn ResourceBuilder::with_schema_url(self : ResourceBuilder, schema_url : StringView) -> ResourceBuilder

Sets the schema URL on the resource being built. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceBuilder::with_service_name

fn ResourceBuilder::with_service_name(self : ResourceBuilder, service_name : StringView) -> ResourceBuilder

Sets service.name on the resource being built. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#resource-creation

ResourceDetector

pub struct ResourceDetector {
detect_fn : () -> Resource
}

Detector callback used to discover resource attributes from the runtime or environment. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#detecting-resource-information-from-the-environment

ResourceDetector::detect

Runs the detector and returns the discovered resource. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#detecting-resource-information-from-the-environment

ResourceDetector::new

fn ResourceDetector::new(detect_fn : () -> Resource) -> ResourceDetector

Creates a detector from a callback. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#detecting-resource-information-from-the-environment

env_resource_detector

fn env_resource_detector() -> ResourceDetector

Detects resources from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME. OTEL_SERVICE_NAME is merged last so it wins over service.name from the generic attribute list. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#specifying-resource-information-via-an-environment-variable

sdk_provided_resource_detector

fn sdk_provided_resource_detector() -> ResourceDetector

Detects the fallback service.name derived from the current executable or script name. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#sdk-provided-resource-attributes

telemetry_resource_detector

fn telemetry_resource_detector() -> ResourceDetector

Detects the standard telemetry.sdk.* attributes for this MoonBit SDK. Spec: https://opentelemetry.io/docs/specs/otel/resource/sdk/#sdk-provided-resource-attributes

Source Files