W3C Trace Context Across HTTP Requests
W3C Trace Context is a standard way for services to carry distributed-tracing identity across network requests. For HTTP, it uses the traceparent header for the core trace and span identifiers, and the optional tracestate header for vendor-specific tracing data.
The problem it solves
A single user action often crosses several processes: an API receives a request, calls an authentication service, queries an inventory service, and publishes a message. Each service can measure its own work, but separate measurements are not useful unless you can connect them.
Without a shared context, an engineer investigating an incident has to match timestamps, URLs, request IDs, and log messages manually. Those clues are incomplete, and clock differences make them unreliable. A trace context gives each service a small, standard piece of information that says, in effect, “this request belongs to this trace, and this caller was this span.”
A trace is the complete journey of one operation through multiple services. A span is one timed piece of that journey, such as handling an HTTP request or querying a database. Spans form a parent-child relationship: the service receiving a call creates a child span beneath the span that made the call.
The traceparent header
A version 00 traceparent value has four hyphen-separated fields:
00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
They are, in order:
- Version:
00identifies the current format. - Trace ID: 32 lowercase hexadecimal characters identifying the whole trace. Every span in the trace uses this same ID.
- Parent ID: 16 lowercase hexadecimal characters identifying the span that directly caused this request.
- Trace flags: two hexadecimal characters. In version 00, bit 0 is the sampled flag.
The parent ID is not the ID of the entire trace. It changes at every service boundary. If Service A makes a request to Service B, the outgoing header contains the ID of A's current span as the parent ID. B creates its own span ID while retaining the same trace ID.
The sampled flag is a hint about whether the trace should be recorded or exported. It is not a command to store data, and a service may still apply its own sampling and resource limits. A service must not treat the trace ID or flags as proof of identity: incoming headers are request data, not authentication.
A valid context also has structural rules. The IDs cannot be all zeroes, the fields must have the expected lengths and character forms, and unsupported versions must be handled according to the specification rather than guessed at. In practice, tracing libraries perform this parsing and validation.
The tracestate header
tracestate carries additional information that is not standardized by W3C. Tracing systems and vendors can put their own entries in a comma-separated list of key=value members, for example:
tracestate: vendor-a=opaque-value,vendor-b=another-value
The values are intentionally opaque to services that do not own them. A service should preserve entries it does not understand when forwarding context. A vendor that does understand an entry may update its own entry and move it to the front of the list, while leaving unrelated entries intact. This lets multiple tracing implementations participate in one request chain without putting vendor-specific fields into traceparent.
tracestate is optional. traceparent is the portable identity; tracestate is extra state that can improve sampling, routing, or vendor interoperability. If traceparent is missing or invalid, an implementation normally starts a new trace rather than trusting an unusable parent. Handling of an invalid tracestate must not make an otherwise valid traceparent unusable.
How propagation works between services
Propagation is the process of extracting context from an incoming request and injecting context into an outgoing request. The usual sequence is:
- Receive and extract. The HTTP server or tracing instrumentation reads
traceparentand, if present,tracestate. - Create a server span. The receiving service creates a span for handling the request. Its parent is the incoming parent ID, and its trace ID is the incoming trace ID. If there was no valid incoming context, this span starts a new trace.
- Make downstream calls in that context. Code handling the request runs with the new span as its current context.
- Inject before sending. HTTP client instrumentation writes a new
traceparentheader whose trace ID is unchanged and whose parent ID is the current span's ID. It forwards the appropriatetracestateas well. - Finish spans. Each service records start and end times, status, and other attributes, then exports the span according to its tracing configuration.
For example, if an incoming request creates span a1, and that handler calls another service while a child client span b2 is active, the downstream request carries b2 as its parent ID:
# Request received by Service A
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-1111111111111111-01
# Service A calls Service B
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-2222222222222222-01
Service B therefore sees one trace, but a different immediate parent. This creates a chain such as client → Service A server span → Service A client span → Service B server span.
What services must preserve
A service does not preserve context by copying the original header blindly. It must keep the trace identity while representing the current operation as the new parent for the next call. Most observability libraries provide HTTP server and client instrumentation that performs extraction and injection automatically, but this only works when the instrumentation covers the relevant framework and HTTP client.
Context can also be lost in background tasks, thread pools, queues, retries, or custom HTTP wrappers. The same rule applies: capture the active context when work is scheduled, restore it while the work runs, and inject the resulting current context into the downstream request. When context propagation is working, the familiar trace ID in logs, error reports, and distributed trace views is what connects work that occurred in different processes.