Distributed Tracing with OpenTelemetry
Follow a single request across services, queues and databases to see exactly where time and errors come from.
Advanced · about 5 hours · difficulty 3/5
Learn first: Metrics, Dashboards and Alerting, Microservices and Service-Oriented Architecture
When a request touches an API gateway, three services, a queue and two databases, neither logs nor metrics alone show where the 2 seconds went. Distributed tracing records the request's full path.
Traces and spans
- A trace represents one end-to-end operation and has a trace id.
- It is made of spans: timed units of work (an HTTP handler, a SQL query, an outgoing call) with a parent-child structure, attributes and status.
- A trace viewer shows spans as a waterfall, making slow or failing steps obvious.
Context propagation
For spans from different services to join one trace, the trace context must travel with the request. The W3C traceparent header does this for HTTP; message headers do it for queues. Instrumentation libraries inject and extract it automatically.
OpenTelemetry
OpenTelemetry (OTel) is the vendor-neutral standard for traces, metrics and logs:
- SDKs and auto-instrumentation for most languages capture HTTP servers, clients, database drivers and queue libraries with little code.
- You add custom spans around important business logic.
- The OTel Collector receives telemetry, processes it (batching, sampling, redaction) and exports it to backends like Jaeger, Grafana Tempo, Zipkin, Honeycomb or Datadog. Switching vendors becomes a config change.
Sampling
Recording every trace is expensive at scale. Head sampling keeps a percentage chosen at the start; tail sampling decides after the trace completes, so you can keep all errors and slow requests.
Connecting signals
Put the trace id in your logs and link metrics exemplars to traces, so you can jump from a latency spike to an example trace to its logs.
You are ready to move on when a request through two of your services shows up as one trace in Jaeger, including the database query spans, and your logs carry the same trace id.
Key concepts
- Traces and spans
- Context propagation and traceparent
- OpenTelemetry SDKs and auto-instrumentation
- Custom spans and attributes
- OpenTelemetry Collector
- Head vs tail sampling
- Jaeger and Grafana Tempo
- Correlating logs, metrics and traces
Mini project
Add OpenTelemetry auto-instrumentation to two services and a queue consumer, export through a Collector to Jaeger, and find the slowest span in a deliberately slow endpoint.
Check yourself
How does a downstream service know which trace an incoming request belongs to?
Free resources
- OpenTelemetry documentation (OpenTelemetry)
- OpenTelemetry: Traces (OpenTelemetry)
- Jaeger documentation (Jaeger)