patternjavascriptMajor
Distributed tracing context propagation across HTTP and message queue boundaries
Viewed 0 times
@opentelemetry/api ^1.x
traceparentW3C trace contextB3 headerspropagation inject extractkafka tracingcontext.withdistributed trace stitching
Problem
Traces break at service boundaries because trace context (trace ID, span ID, sampling decision) is not propagated in outgoing requests. Jaeger or Zipkin shows disconnected single-service traces instead of end-to-end request flows.
Solution
Use W3C Trace Context headers (traceparent, tracestate) for HTTP and inject/extract context for message queues.
HTTP (automatic with OTel auto-instrumentation):
The OTel HTTP instrumentation automatically injects
Message queues (manual):
HTTP (automatic with OTel auto-instrumentation):
The OTel HTTP instrumentation automatically injects
traceparent into outgoing fetch/axios/http requests and extracts it from incoming requests. Verify with:// Should see traceparent header in outgoing requests
console.log(outgoingRequest.headers['traceparent']);
// Example: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01Message queues (manual):
const { context, propagation } = require('@opentelemetry/api');
// Producer: inject context into message headers
const headers = {};
propagation.inject(context.active(), headers);
await producer.send({ topic: 'orders', messages: [{ value: JSON.stringify(order), headers }] });
// Consumer: extract context from message headers
const parentContext = propagation.extract(context.active(), message.headers);
context.with(parentContext, () => {
tracer.startActiveSpan('process-order', (span) => {
// This span is now a child of the producer span
span.end();
});
});Why
Without context propagation, each service creates isolated root spans. The trace ID must flow with the request for the distributed trace to be stitched together in the backend.
Gotchas
- B3 headers (X-B3-TraceId) and W3C traceparent are different formats — configure the same propagator on all services
- Message queue consumers often run in a different async context — you must explicitly re-attach the parent context
- Sampling decisions are encoded in traceparent flags — a sampled=false decision must be respected downstream
- gRPC uses metadata, not HTTP headers — use the OTel gRPC instrumentation which handles this automatically
Context
Enabling end-to-end distributed tracing across HTTP services and/or message queues
Revisions (0)
No revisions yet.