Prompt
How do I set up distributed tracing across microservices
Latest observation
To set up distributed tracing across microservices, use a tracing standard, instrument each service, propagate context between services, and export spans to a backend that can visualize traces end-to-end.
1) Pick a tracing standard and backend
Use OpenTelemetry (OTel) for instrumentation and context propagation. It’s the current best default.
Choose a backend such as:
- Jaeger
- Grafana Tempo
- Zipkin
- Vendor tools like Datadog, New Relic, Honeycomb, Dynatrace
OpenTelemetry can export to most of them.
2) Define trace context propagation
Distributed tracing works by passing trace context between services in headers.
Use the W3C standard:
traceparenttracestate
If you already have older systems, you may also need support for:
- B3 (
x-b3-traceid, etc.)
3) Instrument each service
Each microservice should:
- Create spans for incoming requests
- Create child spans for key internal operations
- Inject trace context into outbound HTTP/gRPC/message calls
- Extract trace context from inbound calls
Typical span types:
- Server span for incoming request
- Client span for outbound request
- Internal span for DB calls, cache calls, business logic
4) Add automatic instrumentation first
For common frameworks/languages, start with auto-instrumentation:
- HTTP frameworks
- DB clients
- gRPC
- Messaging clients
Then add manual spans for important business boundaries.
5) Propagate context across async boundaries
If you use:
- Kafka, RabbitMQ, SQS
- background jobs
- async tasks / thread pools
make sure trace context is passed along in message headers or task metadata.
6) Export traces to a collector
Recommended architecture:
- App → OpenTelemetry SDK → OTel Collector → tracing backend
The collector helps with:
- batching
- retries
- sampling
- enrichment
- switching backends later without changing apps
7) Decide on sampling
Tracing every request can be expensive at scale. Common approaches:
- Head sampling: decide at request start
- Tail sampling: decide after seeing full trace in collector
Useful strategies:
- Sample 1–10% of normal traffic
- Sample 100% of errors and slow requests
- Increase sampling for specific users/tenants during incidents
8) Correlate logs and metrics
Add trace IDs to logs so you can jump from a trace to logs. Also consider metrics like:
- request rate
- latency
- error rate
- queue depth
A good observability stack combines traces + logs + metrics.
9) Establish naming and conventions
Keep spans readable:
- Use stable service names
- Name operations consistently
- Add attributes like:
http.methodhttp.routedb.systemrpc.servicemessaging.system
Avoid putting sensitive data in span names or attributes.
10) Validate end-to-end
Test with a request that crosses multiple services and confirm:
- one trace ID throughout
- parent/child span relationships are correct
- context survives retries and async hops
- errors appear in the same trace
Example flow
Client → API Gateway → User Service → Payment Service → DB
A single trace should show:
- gateway span
- user service span
- payment service span
- DB span
with each service as a child of the previous one, or linked appropriately for async work.
Minimal implementation plan
- Install OpenTelemetry SDK in each service
- Enable auto-instrumentation for HTTP/gRPC/DB
- Configure W3C trace context propagation
- Deploy OTel Collector
- Export to Jaeger/Tempo/Datadog/etc.
- Add logs with trace IDs
- Set sampling rules
- Test with a multi-service request
Common pitfalls
- Forgetting to propagate headers between services
- Not instrumenting async/message-based communication
- Creating too many low-value spans
- Sampling too aggressively and missing incidents
- Logging sensitive data in spans
- Mixing multiple tracing standards without bridges
If you want, I can give you:
- a language-specific setup (Java, Python, Go, Node, .NET, etc.), or
- a reference architecture using OpenTelemetry + Jaeger/Tempo.