Prompt
How do I set up trace context propagation across services
Latest observation
To set up trace context propagation across services, you want every service to:
- Extract incoming trace context from request headers
- Continue the same trace when creating child spans
- Inject the updated trace context into outbound requests
1) Choose a propagation standard
Use an industry standard so different languages/tools interoperate:
- W3C Trace Context:
traceparent,tracestate - Optional baggage:
baggage - Some legacy systems use B3 (
x-b3-traceid, etc.)
If you can, prefer W3C Trace Context.
2) Instrument your services with OpenTelemetry
OpenTelemetry is the common way to handle tracing propagation.
Typical flow:
- Initialize OpenTelemetry SDK in each service
- Configure a propagator (W3C or B3)
- Add instrumentation for your HTTP/gRPC clients and servers
Example concept
- Service A receives request with
traceparent - It extracts context and starts a server span
- When Service A calls Service B, the client instrumentation injects the same trace context into headers
- Service B extracts it and creates its own server span as a child
3) HTTP propagation
For HTTP calls, propagation is usually done via headers automatically by your tracing library.
Incoming request headers
Example:
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00
tracestate: congo=t61rcWkgMzE
Outgoing request
Your client instrumentation should inject the same headers.
4) gRPC propagation
For gRPC, metadata is used instead of headers, but OpenTelemetry handles this similarly with interceptors/middleware.
5) Message queues / async jobs
For Kafka, RabbitMQ, SQS, etc.:
- Inject trace context into message headers/properties before publishing
- Extract it when consuming
- Start the consumer span as a child of the producer span
This is especially important because async boundaries often break traces if propagation is not explicit.
6) Context handling rules
- Always carry the active context through request handlers
- Don’t create a new trace at each service unless it’s intentionally a separate transaction
- Propagate baggage only if you need extra cross-service metadata; keep it small and non-sensitive
7) Verify it works
Check your tracing backend (Jaeger, Zipkin, Grafana Tempo, Honeycomb, Datadog, New Relic, etc.) and confirm:
- One trace ID spans multiple services
- Parent/child span relationships are correct
- Service-to-service calls appear in sequence
8) Common pitfalls
- Forgetting to instrument one service or one outbound client
- Mixing propagation formats without configuring both sides
- Losing context in async callbacks / thread pools / background tasks
- Manually creating spans without attaching them to the current context
- Passing sensitive data in baggage
If you want, I can give you a language-specific setup for:
- Java/Spring
- Node.js
- Python
- Go
- .NET
- gRPC
- Kafka/RabbitMQ