Expand description
W3C Trace Context propagation helpers.
This module is the single point of contact between the inbound HTTP
request’s traceparent / tracestate headers and the local tracing
span tree. It exists so the wiring lives in exactly one place — the
tower middleware in crate::middleware is the only consumer at the
moment, but the same helpers are usable from integration tests and from
any future surface that wants to participate in distributed tracing
(e.g. a CLI shim that calls back into the gateway, a benchmark harness
that captures the resulting trace IDs, etc.).
The actual span-creation logic still lives in
crate::middleware::trace_layer_with_propagation; this file owns:
- the
HeaderMapExtractoradapter that lets the OpenTelemetryTextMapPropagatorAPI read fromaxum::http::HeaderMap; install_w3c_propagator, an idempotent one-shot installer for the globalopentelemetry_sdk::propagation::TraceContextPropagatorso middleware extraction is not a silent no-op;extract_parent_context, a thin helper around the global propagator that the middleware uses to attach a parent to its span;current_trace_id, a helper for emitting the active trace id on responses (x-trace-idheader) and in audit records.
§Why install the propagator from the API crate
tensor-wasm-core’s telemetry module
initialises the global tracer provider but its OTel deps are
feature-gated under otlp. The gateway crate, by contrast, needs the
propagator on every deployment regardless of whether the operator has
enabled OTLP export — without it the middleware sees an empty context
and traceparent from the client is silently dropped. So we install
the propagator here, unconditionally, at router build time. It’s a
single global write guarded by a Once.
Structs§
- Header
MapExtractor - Adapter that lets the OpenTelemetry
TextMapPropagatorread from anaxum/httpHeaderMap.
Constants§
- HEADER_
TRACE_ ID - HTTP header name used by the gateway to surface the request’s resolved trace id on every response. Lowercased per HTTP/2 norms; axum normalises outgoing headers regardless, but using the canonical lowercase spelling here matches the rest of the codebase.
Functions§
- current_
trace_ id - Return the 32-character lowercase hex representation of the active span’s trace id, if any.
- extract_
parent_ context - Extract a parent
opentelemetry::Contextfrom the supplied headers via whichever global propagator has been installed (typicallyTraceContextPropagatorafterinstall_w3c_propagatorhas run). - inject_
trace_ id_ header - Middleware that stamps
x-trace-id: <hex>on every response when the active span has a resolved OTel trace id. - install_
w3c_ propagator - Install the W3C Trace Context propagator as the global text-map propagator if no other propagator has been installed yet.