README

#OpenTelemetry Context API

This package stores the cross-cutting values that instrumentation carries between calls: the active span context, baggage, and the telemetry suppression flag.

#Design

Context is immutable. Every with_* helper returns a new value, which makes it safe to derive child contexts without mutating the original.

#Method behavior

  • Context::new(): alias for Context::empty()
  • Context::empty(): returns a context with no span, empty baggage, and suppression disabled
  • Context::span_context(): returns the stored span context, if present
  • Context::has_active_span(): returns true only when a valid span context is present
  • Context::with_span_context(span_context): returns a copy with a local span context attached
  • Context::with_remote_span_context(span_context): returns a copy with a remote span context attached
  • Context::baggage(): returns the stored baggage value
  • Context::with_baggage(baggage): returns a copy with the given baggage
  • Context::with_baggage_item(key, value, metadata?): returns a copy with one baggage item inserted or replaced; baggage validation rules still apply
  • Context::is_telemetry_suppressed(): returns the suppression flag
  • Context::with_telemetry_suppressed(suppressed?): returns a copy with the suppression flag updated; omitting the argument turns suppression on

#Local vs remote span contexts

Use with_remote_span_context() for span contexts extracted from inbound headers. Use with_span_context() for locally created spans. Propagators rely on that distinction.

#
Context

Immutable context container shared across traces, logs, metrics, and propagation.

Each with_* helper returns a new value instead of mutating the original context.

Use with_remote_span_context() for contexts extracted from inbound carriers and with_span_context() for locally created spans. Propagators and log correlation rely on that distinction. Spec: https://opentelemetry.io/docs/specs/otel/context/#overview
impl Default for Context

#
Context::baggage

Returns the baggage stored in the context.

Baggage is propagated to downstream services. Do not place secrets or unbounded values in it. Spec: https://opentelemetry.io/docs/specs/otel/baggage/api/#context-interaction

#
Context::empty

fn Context::empty() -> Context

Returns a context with no span, no baggage, and telemetry suppression disabled. Spec: https://opentelemetry.io/docs/specs/otel/context/#overview

#
Context::has_active_span

fn Context::has_active_span(self : Context) -> Bool

Returns whether the context contains a valid active span context.

Invalid span contexts are treated as absent for parent/child relationships and propagation. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#context-interaction

#
Context::is_telemetry_suppressed

fn Context::is_telemetry_suppressed(self : Context) -> Bool

Returns whether telemetry is suppressed for work performed in this context.

Suppression is useful inside exporters and instrumentation internals to avoid recursively producing telemetry while exporting telemetry. Related spec: https://opentelemetry.io/docs/specs/otel/context/#overview

#
Context::new

fn Context::new() -> Context

Returns a new empty context.

This is an alias for Context::empty(). Spec: https://opentelemetry.io/docs/specs/otel/context/#overview

#
Context::span_context

Returns the stored span context, if any. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#context-interaction

#
Context::with_baggage

Returns a copy of the context with the given baggage value. Spec: https://opentelemetry.io/docs/specs/otel/baggage/api/#context-interaction

#
Context::with_baggage_item

fn Context::with_baggage_item(self : Context, key : StringView, value : StringView, metadata? :
BaggageMetadata
) -> Context

Returns a copy of the context with one baggage item inserted or replaced.

The same validation and limit rules as Baggage::insert_with_metadata() apply. Spec: https://opentelemetry.io/docs/specs/otel/baggage/api/#context-interaction

#
Context::with_remote_span_context

Returns a copy of the context with a remote span context attached.

The stored span context is forced to is_remote = true. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#context-interaction

#
Context::with_span_context

Returns a copy of the context with a local span context attached.

The stored span context is forced to is_remote = false. Spec: https://opentelemetry.io/docs/specs/otel/trace/api/#context-interaction

#
Context::with_telemetry_suppressed

fn Context::with_telemetry_suppressed(self : Context, suppressed? : Bool) -> Context

Returns a copy of the context with telemetry suppression toggled.

The default argument enables suppression. Related spec: https://opentelemetry.io/docs/specs/otel/context/#overview

Source Files