Distributed Tracing with W3C Trace Context
TL;DR:
traceparentis a 55-byte header with a fixed format (version-trace_id-parent_id-flags, lowercase hex). It's the modern default for cross-service trace propagation, replacing per-vendorX-B3-*,X-Datadog-*, etc. OpenTelemetry'sW3CTraceContextPropagatoris the canonical implementation. Decide head vs tail sampling early — they're not interchangeable. For HTTP→SQL correlation, append the trace context as a SQL comment via sqlcommenter format.
Jump to your fire
| Symptom | Section |
|---|---|
| "Traces break across our Node→Go service boundary" | The traceparent format |
| "Should I use B3 / X-Ray / Datadog headers?" | Why W3C wins |
| "Sampling 10% but error traces still missing" | Head vs tail sampling |
| "Need to correlate slow SQL to HTTP request" | SQL commenter |
| "Custom headers with vendor data?" | tracestate |
Decision diagram
flowchart TD
A[Setting up distributed tracing] --> B{Existing tracing infra?}
B -->|None| C[Default: OpenTelemetry SDK<br/>+ W3CTraceContextPropagator]
B -->|Has B3/Jaeger/X-Ray headers| D[Configure composite propagator<br/>W3C + legacy for transition]
C --> E[Decide sampling strategy]
D --> E
E --> F{Need to filter traces by trace-wide attribute<br/>e.g. always sample errors?}
F -->|No, simple % sampling| G[Head sampling<br/>at SDK ParentBased+TraceIDRatio]
F -->|Yes| H[Tail sampling<br/>at Collector<br/>tailsamplingprocessor]
G --> I{Need to correlate to DB queries?}
H --> I
I -->|Yes| J[Enable sqlcommenter<br/>OTel SQL instrumentation MAY-flag]
I -->|No| K[Done]
1. The `traceparent` header (byte-precise)
From the W3C Trace Context Recommendation §3.2.2, the ABNF:
HEXDIGLC = DIGIT / "a" / "b" / "c" / "d" / "e" / "f" ; lowercase hex only
value = version "-" version-format
version = 2HEXDIGLC ; version 00; "ff" forbidden
version-format = trace-id "-" parent-id "-" trace-flags
trace-id = 32HEXDIGLC ; 16 bytes; all-zeros forbidden
parent-id = 16HEXDIGLC ; 8 bytes; all-zeros forbidden
trace-flags = 2HEXDIGLC ; 8-bit flags
Total length for version 00: exactly 55 ASCII bytes.
Canonical example (W3C §3.1):
traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
↑ ↑ ↑ ↑
version trace-id (16 bytes hex) parent-id flags
MUST requirements (W3C, verbatim)
- "Vendors MUST expect the header name in any case (upper, lower, mixed), and SHOULD send the header name in lowercase." (§3.2.1)
- "If the
trace-idvalue is invalid (for example if it contains non-allowed characters or all zeros), vendors MUST ignore thetraceparent." (§3.2.2.3) - "Vendors MUST ignore the
traceparentwhen theparent-idis invalid (for example, if it contains non-lowercase hex characters)." All-zeros parent-id is also invalid. (§3.2.2.4) - "A vendor receiving a
traceparentrequest header MUST send it to outgoing requests." "Theparent-idfield MUST be set to a new value with thesampledflag update." (§3.4) - "Vendors MUST NOT parse or assume anything about unknown fields for this version." (§3.2.4)
Sampled flag (§3.2.2.5.1)
The current spec (version 00) defines one flag bit:
static final byte FLAG_SAMPLED = 1; // 00000001
boolean sampled = (traceFlags & FLAG_SAMPLED) == FLAG_SAMPLED;
01 = sampled (record + export); 00 = not sampled. The flag may flip when parent-id is updated but should reflect the upstream sampling decision unless the local sampler overrides explicitly.
2. Why W3C replaces vendor headers
Pre-2020, every vendor shipped its own headers:
| Vendor | Headers |
|---|---|
| Zipkin / B3 | X-B3-TraceId, X-B3-SpanId, X-B3-Sampled (or single-header b3) |
| Datadog | x-datadog-trace-id, x-datadog-parent-id, x-datadog-sampling-priority |
| AWS X-Ray | X-Amzn-Trace-Id |
| Jaeger | uber-trace-id |
A polyglot stack with one of each was a graveyard of broken trace continuity. From the OpenTelemetry context propagation docs:
If pre-configured,
Propagators SHOULD default to a compositePropagatorcontaining the W3C Trace Context Propagator and the BaggagePropagatorspecified in the Baggage API.
OpenTelemetry deprecated Jaeger and OT Trace propagators in favor of W3C ("use the W3C TraceContext instead"). B3 is still maintained as a backward-compat option.
Migration: composite propagator
When transitioning a fleet from B3 to W3C:
// JS / Node OTel example
import { CompositePropagator } from '@opentelemetry/core'
import { W3CTraceContextPropagator } from '@opentelemetry/core'
import { B3Propagator } from '@opentelemetry/propagator-b3'
const propagator = new CompositePropagator({
propagators: [
new W3CTraceContextPropagator(), // primary
new B3Propagator(), // fallback for legacy upstreams
],
})
Inject sends both header sets; extract reads whichever arrives. Run the composite propagator until the last legacy upstream is migrated, then drop the B3 entry.
3. Sampling decisions
Definitions (from [OpenTelemetry sampling docs](https://opentelemetry.io/docs/concepts/sampling/))
Sampled: A trace or span is processed and exported. … Not sampled: A trace or span is not processed or exported.
Head sampling
Head sampling is a sampling technique used to make a sampling decision as early as possible. A decision to sample or drop a span or trace is not made by inspecting the trace as a whole.
The most common form of head sampling is Consistent Probability Sampling. This is also referred to as Deterministic Sampling. In this case, a sampling decision is made based on the trace ID and the desired percentage of traces to sample.
Pros: cheap, deterministic across services (same trace-id everywhere → same decision), runs in the SDK.
Con (verbatim): "It is not possible to make a sampling decision based on data in the entire trace. For example, you cannot ensure that all traces with an error within them are sampled with head sampling alone."
Tail sampling
Tail sampling is where the decision to sample a trace takes place by considering all or most of the spans within the trace.
Use cases: always-sample-on-error, latency-based sampling, attribute-based sampling, differential rates per service.
Cons: stateful (the collector must hold all spans for a trace until it decides), vendor-specific tooling, expensive.
The pragmatic recipe
| Need | Strategy |
|---|---|
| Bulk volume reduction | Head sampling (e.g., ParentBased(TraceIDRatio(0.1))) — 10% baseline |
| Always sample errors | Tail sampling at the OTel Collector with tailsamplingprocessor |
| Always sample slow requests (p99+) | Tail sampling on latency policy |
| Always sample for a specific tenant | Both: head sampling 100% via ParentBased, ratio low elsewhere |