Skip to main content

Module trace_propagation

Module trace_propagation 

Source
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:

§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§

HeaderMapExtractor
Adapter that lets the OpenTelemetry TextMapPropagator read from an axum/http HeaderMap.

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::Context from the supplied headers via whichever global propagator has been installed (typically TraceContextPropagator after install_w3c_propagator has 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.