Tracing
Distributed tracing records the path of a single request or job as it moves through many services, with the timing of every step. One request produces one trace; each unit of work inside it (an HTTP call, a SQL query, a Kafka publish) is a span with a start time, a duration, a parent and attributes. Lined up on a timeline, the spans show exactly where the time went and which call failed, something neither logs nor metrics show on their own.
Reading a trace
- A checkout request took 480 ms. The gateway created a trace_id and every service will attach its spans to it.
- The root span is the gateway's GET /checkout. Its child, orders-svc create_order, covers almost the same window: the work happens below it.
- The postgres INSERT takes 45 ms. Not our problem.
- payment-svc charge takes 335 ms, most of the request. Drill into its children.
- The first call to the bank API timed out after 200 ms; a retry then succeeded in 105 ms. The trace shows the cause directly: a timeout plus retry, not a slow database.
- Finally the order event is produced to Kafka. Without the hung first bank call, this request would have taken about 270 ms.
How it works: context propagation
A trace only connects across services if each hop passes the trace context along. The W3C Trace Context standard defines a traceparent header:
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
│ │ │ └ flags (01 = sampled)
│ │ └ parent span-id
│ └ trace-id (same for the whole trace)
└ versionInstrumentation libraries read this header on incoming requests, create child spans, and inject it into outgoing HTTP calls, gRPC metadata or Kafka message headers. When any service in the chain drops the header, the trace breaks into pieces.
OpenTelemetry
OpenTelemetry (OTel) is the standard way to produce traces. It provides SDKs and auto-instrumentation for most languages (Python, Java, Go, Node.js, .NET…) and popular libraries (HTTP clients, database drivers, Kafka clients), plus the Collector to batch, sample and export spans to any backend over OTLP. It replaced the earlier OpenTracing and OpenCensus projects, which are archived.
from opentelemetry import trace
tracer = trace.get_tracer("orders-loader")
with tracer.start_as_current_span("load_partition") as span:
span.set_attribute("table", "orders")
span.set_attribute("partition", "2026-11-16")
rows = load() # child spans from instrumented DB driver
span.set_attribute("rows", rows)Sampling
Tracing every request in a high-traffic system is expensive. Two strategies:
- Head sampling: decide at the start (keep 1% of traces). Cheap, but you may throw away the rare slow or failing request.
- Tail sampling: buffer all spans in the Collector and decide after the trace completes (keep all errors, all requests over 1 s, and 1% of the rest). Better signal, more infrastructure.
Tracing in data pipelines
Traces are not just for web requests. Useful data-engineering cases:
- Follow an event from the producer, through Kafka, into a stream processor and the sink, by propagating context in message headers.
- Wrap each Airflow task or Dagster op in a span to see which step of a DAG run is slow.
- Trace query engines: Trino and many databases can emit spans for query planning and execution stages.
Backends
| Backend | Notes |
|---|---|
| Jaeger | CNCF graduated, created at Uber. Jaeger v2 is built on the OpenTelemetry Collector. |
| Grafana Tempo | Stores traces cheaply in object storage, indexes little; pairs with Grafana, Loki and Prometheus. |
| Zipkin | The original open-source tracer, inspired by Google’s Dapper paper. Simple and stable. |
| Honeycomb, Datadog APM, Splunk Observability, New Relic | Commercial, with strong high-cardinality querying and analysis. |
Common mistakes
- Broken propagation. Async queues, thread pools and custom HTTP clients often drop context. Check that one trace actually spans every service.
- Spans for everything. A span per row or per loop iteration explodes cost. Span the meaningful units: a request, a batch, a query.
- No link to logs. Put
trace_idandspan_idin every log line so you can jump from a slow span to its logs. - Sensitive attributes. SQL with literal values, emails or tokens in span attributes leak data. Sanitize in the SDK or Collector.
Learning Resources
Papers and standards
Official
Books
- Mastering Distributed Tracing, Yuri Shkuro (Packt): by Jaeger’s creator
- Learning OpenTelemetry, Ted Young and Austin Parker (O’Reilly)
- Distributed Systems Observability, Cindy Sridharan (O’Reilly report)
See also: Logging, Monitoring.