Prompt
How do I set up OpenTelemetry tracing across microservices?
Latest observation
To set up OpenTelemetry tracing across microservices, you want to do 4 things:
- Instrument each service
- Propagate trace context between services
- Export traces to a backend
- Standardize service naming, sampling, and correlation
Here’s a practical setup.
1) Pick an OpenTelemetry strategy
There are two common approaches:
- Auto-instrumentation: quickest to start, minimal code changes.
- Manual instrumentation: more control, better for custom spans and business events.
Most teams use:
- auto-instrumentation for HTTP/gRPC/database calls
- manual spans around important business operations
2) Add the OpenTelemetry SDK/agent to every service
Install the OpenTelemetry libraries for your language/runtime.
Examples:
- Java: OpenTelemetry Java Agent
- Node.js:
@opentelemetry/sdk-node - Python:
opentelemetry-sdk, instrumentations - .NET: OpenTelemetry packages
- Go: OpenTelemetry SDK + instrumentation libs
Make sure each service sets:
service.nameservice.versiondeployment.environment
These become important in your trace backend.
3) Propagate trace context across service boundaries
This is the key part for “across microservices.”
OpenTelemetry uses standards like:
- W3C Trace Context (
traceparent,tracestate) - optional baggage headers
When Service A calls Service B, the trace context must be injected into outgoing requests and extracted on incoming requests.
Example flow
- Request enters API Gateway / Service A
- Trace starts there
- Service A calls Service B with propagated headers
- Service B continues the same trace
- Any downstream DB/cache calls become child spans
If you use OTel auto-instrumentation, this propagation is usually handled automatically for supported frameworks and clients.
4) Send telemetry to an OpenTelemetry Collector
A very common architecture is:
Services → OpenTelemetry Collector → tracing backend
Why use the Collector?
- central place to batch, retry, sample, and transform
- avoids each app talking directly to your vendor
- makes backend switching easier
Collector responsibilities
- receive OTLP data
- batch spans
- apply sampling
- export to backend such as:
- Jaeger
- Grafana Tempo
- Honeycomb
- Datadog
- New Relic
- Elastic
- AWS X-Ray (via exporters)
5) Configure sampling
Tracing everything can be expensive at scale.
Common sampling options:
- AlwaysOn: useful in dev
- ParentBased + TraceIdRatioBased: common in prod
- Tail-based sampling via Collector: keep traces based on latency/errors after seeing the whole trace
For microservices, tail-based sampling is often best if you want to keep:
- slow traces
- error traces
- specific endpoints
6) Instrument inbound and outbound requests
You want spans for:
- HTTP/gRPC server requests
- HTTP/gRPC client calls
- database queries
- message queue publish/consume
- cache calls
- critical internal operations
A good trace usually shows:
- API gateway span
- service request spans
- DB spans
- queue spans
- downstream service spans
7) Add logs correlation
Not strictly tracing, but very helpful.
Include trace IDs in logs so you can jump from a log line to a trace.
Typical fields:
trace_idspan_idservice.name
This makes debugging much easier.
8) Define conventions
Use consistent naming:
- spans: short verb-noun names like
GET /orders/{id}orprocess payment - attributes:
http.methodhttp.routeuser.idonly if safedb.systemmessaging.system
Avoid high-cardinality attributes unless needed.
9) Verify end-to-end propagation
Test with a request that crosses several services and confirm:
- one trace ID across all services
- parent/child span relationships are correct
- errors appear on the right span
- timestamps look reasonable
10) Minimal architecture example
Client
↓
API Gateway / Service A
↓
Service B
↓
Service C
↓
DB / Queue
All services send OTLP → OpenTelemetry Collector → Trace backend
Example: common environment variables
Many SDKs/agents can be configured with env vars:
OTEL_SERVICE_NAME=orders-service
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317
OTEL_TRACES_EXPORTER=otlp
OTEL_PROPAGATORS=tracecontext,baggage
OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1
Best practices
- Use the Collector
- Enable auto-instrumentation first
- Set service.name consistently
- Propagate context on every network hop
- Correlate logs with trace IDs
- Sample intelligently
- Add custom spans only where they add value
- Don’t record sensitive data in span attributes
Recommended rollout plan
- Instrument one service end-to-end
- Add the Collector
- Confirm trace propagation across one downstream service
- Roll out to all services
- Add custom spans for business-critical workflows
- Tune sampling and backend retention
If you want, I can give you a working setup example for:
- Java Spring Boot
- Node.js Express
- Python FastAPI
- Go
- .NET
- or a Docker Compose + OpenTelemetry Collector example for multiple microservices.