Context Propagation: How Traces Follow a Request
You open the trace for a slow checkout and it stops dead at the cart service: no pricing call, no database query, nothing after it. Those steps did happen, but their spans landed in a separate trace that nothing connects to this one. The missing piece is context propagation: the step where each service hands the trace's identity to the next service, inside the request itself.
What travels with the request
In the previous lesson, every span of a trace shared one trace ID and pointed at its parent's span ID. Inside one process, the tracing library keeps track of the current span in memory. The moment a request leaves for another service, that memory stays behind, so the identity has to travel inside the request.
That identity is the trace context, and it has three parts. The trace ID says which trace this is. The parent span ID says which span made the call, so the receiver can hang its own span underneath. The trace flags carry the sampling decision, meaning whether this trace is being recorded at all. Over HTTP, the standard carrier is the W3C Trace Context specification, which defines two headers: traceparent and tracestate.
The traceparent header, field by field
This is the example header from the W3C specification. It is an ordinary HTTP header, sent with the request like any other:
traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
Its value is four fields of lowercase hexadecimal (digits 0 to 9 and letters a to f) joined by dashes, 55 characters in all:
- version
00: the format version.00is the only one defined so far, andffis forbidden. - trace-id: 16 bytes as 32 hex characters, identical at every hop. All zeros is invalid.
- parent-id
b7ad6b7169203331: 8 bytes as 16 hex characters, the span ID of the caller's span. All zeros is invalid. - trace-flags
01: one byte as 2 hex characters. The lowest bit is the sampled flag:01means the caller is recording this trace,00means it is not. Level 2 of the spec adds a second bit,02, which says the trace ID was generated randomly.
A receiver that cannot parse traceparent, because of a wrong length, uppercase letters or an all-zero ID, discards it and starts a fresh trace. It must then ignore tracestate as well.
The optional tracestate header carries vendor-specific data as a comma-separated list of key=value pairs, for example rojo=00f067aa0ba902b7,congo=t61rcWkgMzE. The list holds at most 32 entries, and a vendor that updates its entry moves it to the front. You rarely touch it yourself. Tracing tools use it, and OpenTelemetry keeps its own entry under the key ot.
Inject on the way out, extract on the way in
Two verbs describe all propagation. To inject is to write the current context into an outgoing request's headers just before it is sent. To extract is to read those headers on arrival and make that context the parent of the new span. The piece of code that does both for one header format is called a propagator.
In the diagram, cart's outgoing header carries cart's own span ID. If the frontend also records a client span for its outgoing call, the parent ID is that client span's ID instead. That adds one more level to the tree and changes nothing else.
You rarely write this code. OpenTelemetry's instrumentation for common HTTP clients, servers and frameworks injects and extracts automatically, and the OTEL_PROPAGATORS environment variable picks the formats. The default is tracecontext,baggage; you would add b3 to interoperate with older Zipkin-instrumented services. Where no instrumentation exists, you call the propagator yourself. In Python, with request standing for your web framework's incoming request, it looks like this:
import requests
from opentelemetry import trace
from opentelemetry.propagate import inject, extract
tracer = trace.get_tracer("pricing-client")
# Outgoing: copy the current context into a dict of headers
headers = {}
inject(headers) # adds "traceparent" (plus "tracestate"/"baggage" if set)
requests.post("http://pricing:8080/quote", json=order, headers=headers)
# Incoming: turn the received headers back into a parent context
ctx = extract(request.headers)
with tracer.start_as_current_span("POST /quote", context=ctx,
kind=trace.SpanKind.SERVER):
...
Both functions work on any dictionary-like carrier, so the same two calls cover HTTP headers, message headers or even a column in a jobs table. HTTP treats header names as case-insensitive, but many message systems do not, so keep the key exactly traceparent.
The same propagators also carry baggage, a separate W3C header of small key-value pairs that your own code sets, such as baggage: tenant=acme,plan=gold. Every downstream service can read it, but nothing copies it onto spans unless you do. Keep it tiny: systems only have to pass on 64 entries or 8,192 bytes. Never put secrets or personal data in it, because it is forwarded to every service you call, third-party APIs included.
Where propagation breaks
A broken trace almost always comes from one hop where nobody injected or nobody extracted. These are the usual places:
- Message queues. A Kafka record or RabbitMQ message is not an HTTP request, so the context must go into the message's own headers and the consumer must extract it. Client-library instrumentation often does this; a hand-rolled producer does not. When a consumer processes a batch drawn from many traces, OpenTelemetry records span links to each producer instead of one parent.
- Async jobs and thread pools. The current context is tied to the running thread or task. Work handed to a thread pool or a background job starts with an empty context unless you capture it and pass it along.
- Proxies and gateways. A load balancer or API gateway with a header allow-list drops
traceparent, and some proxies that trace on their own start a new trace instead of continuing yours. - Hand-written HTTP clients. Automatic instrumentation patches well-known libraries. A raw socket client, an obscure library or a shell-out to
curlsends no header.
The fix is always the same: find the hop, inject on the sending side and extract on the receiving side. To find it, log the incoming traceparent at each service and look for the first one whose trace ID does not match.
Check yourself
Service B receives traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01 and then calls C. Which field changes in the header B sends?
Only the parent-id, which becomes B's span ID; the version, trace ID and sampled flag pass through unchanged.
A worker's spans always show up as separate one-service traces. What is the likely cause?
The context is not crossing the queue: the producer does not inject it into message headers, or the worker does not extract it, so each job starts a new trace.
Why should a user's email address never go into baggage?
Baggage is sent in plain text to every downstream service, including third parties, so it would leak personal data across your whole call chain.