Skip to main content

extract_trace_id

Function extract_trace_id 

Source
pub fn extract_trace_id(headers: &HeaderMap) -> Option<String>
Expand description

Resolve the wire trace_id: live OTel span context (the same source a log-correlation formatter uses) → incoming W3C traceparent (validated by parse_trace_id).

The live span is preferred so the wire id is always the one the caller’s own log lines carry: on a successful continuation the two are equal, and on a fresh root (no propagator, or set_parent failed) the span’s id is what the logs show — reading the header first would ship an id the logs don’t carry. The header is used only when no span is active (no otel, or outside any request span), and an invalid traceparent is treated as absent.

It does NOT fall back to x-request-id or a span handle: those are different identifiers that resolve to nothing under trace_id in a trace backend. Absent both, trace_id is left absent rather than filled with a stand-in.