Skip to content

context

Basic trace context foundation for observability core

Provides W3C trace context implementation and basic context management ports. Domain-specific implementations (LLM, A2A, etc.) are provided by higher-level crates.

W3C Trace Context implementation for distributed tracing

Fields

FieldTypeDescription
trace_idStringW3C trace ID (32 hex characters)
parent_idStringW3C span ID (16 hex characters)
trace_flagsStringTrace flags (2 hex characters)
trace_stateOption<String>Additional trace state

Methods

fn new_root() -> Self

Create a new root trace context

fn new_child(&self) -> Self

Create a child span context

fn from_traceparent(header: &str) -> ObservabilityResult<Self>

Create from W3C traceparent header

fn from_headers(headers: &HashMap<String, String>) -> ObservabilityResult<Option<Self>>

Create from W3C headers

fn to_traceparent(&self) -> String

Generate W3C traceparent header value

fn to_headers(&self) -> HashMap<String, String>

Generate W3C headers for propagation

fn is_sampled(&self) -> bool

Check if the trace is sampled

fn set_sampled(&mut self, sampled: bool)

Set sampling flag

fn add_trace_state(&mut self, key: &str, value: &str)

Add or update trace state

fn get_trace_state(&self, key: &str) -> Option<String>

Get value from trace state

Simplified trace context for internal use

Fields

FieldTypeDescription
trace_idString
span_idString
parent_span_idOption&lt;String&gt;
sampledbool

Methods

fn new_root() -> Self

Create a new root trace context

fn new_child(&self) -> Self

Create a child span

fn to_w3c(&self) -> W3CTraceContext

Convert to W3C trace context

fn from_w3c(w3c: &W3CTraceContext) -> Self

Create from W3C trace context

Future wrapper that restores trace context across async poll boundaries.

Header injector for W3C trace context propagation

Methods

fn inject(&mut self, context: &W3CTraceContext)

Header extractor for W3C trace context propagation

Methods

fn extract(&self) -> ObservabilityResult<Option<W3CTraceContext>>
fn set_current_context(context: TraceContext)

Set the current trace context for this thread

fn get_current_context() -> Option<TraceContext>

Get the current trace context for this thread

fn clear_current_context()

Clear the current trace context

fn with_context<F, R>(context: TraceContext, f: F) -> R
where
F: FnOnce

Execute a function with a specific trace context

fn with_context_future<F>(context: TraceContext, future: F) -> ContextFuture<F>
where
F: Future

Execute a future with a specific trace context re-applied on every poll.