What it is
Distributed tracing records the path of one request through a distributed system. A trace is a tree of spans; each span is a unit of work (an incoming request, an outgoing call, a database query) with a start time, a duration, attributes, a status and the id of its parent span. Displayed as a waterfall, a trace shows what ran, in which order and for how long.
Why it exists
In a microservice system, latency propagates upstream: if Payments is slow, Checkout and the gateway are slow too, because their time includes waiting. Per-service metrics all turn red at once and cannot say where the time actually goes. Logs from each service can be joined by id, but reconstructing timing from them is slow. A trace answers "where did the time go?" directly.
How it works
Instrumentation. Server and client libraries (HTTP, gRPC, database drivers, messaging clients) start and end spans automatically; developers add spans for important business steps. OpenTelemetry is the common standard.
Propagation. When a service calls another, it injects the trace context into the request. The W3C Trace Context header is traceparent: 00-<32-hex trace id>-<16-hex parent span id>-<flags>; the trace id is constant for the whole trace, the parent id is the caller's span, and the flags carry the sampling decision. A tracestate header carries vendor-specific data, and baggage can carry application key-values. The callee extracts the context and starts a child span.
Asynchronous work. For messages, inject the context into message headers at publish time. The consumer either continues the trace with a child span or starts a new trace with a span link to the producer, which suits batch consumers and work that happens long after the request finished.
Analysis. Sort a trace's spans by self time (duration minus time spent in children). The span with high self time is where work happens; a long span whose time is all inside a child is only waiting. The critical path is the chain of spans that determines end-to-end latency; speeding up anything off it does not help.
Export and storage. Spans are batched and exported (usually via an OpenTelemetry Collector) to a trace backend such as Jaeger, Grafana Tempo, Zipkin or a commercial APM. Because traces are large, they are usually sampled.
When to use it
In any system where a request crosses more than a couple of services, queues or databases, especially when latency matters or failures cascade. Also invaluable for understanding unfamiliar systems: traces document the real call graph.
When not to
- A single monolith with one database may get most of the value from good metrics, logs and a profiler.
- Do not use traces as the source of counts and rates when sampling is on; use metrics.
- Very high-volume, low-value paths (health checks, static assets) are often excluded from tracing.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Auto-instrumentation | Coverage with little code | Generic span names; some overhead and noise |
| Manual business spans | Meaningful steps in the waterfall | Code to maintain |
| Higher sampling rate | More traces to investigate | Network, storage, collector capacity |
| Child spans across queues | One trace end to end | Very long traces; awkward for batch consumers (links fit better) |
| Rich span attributes | Faster filtering and grouping | Cost; risk of sensitive data in attributes |
Alternatives
- Correlated logs: request ids on log lines give a poor man's trace without timing structure.
- Metrics per dependency: client-side latency histograms per downstream call locate slow dependencies at an aggregate level.
- Service mesh telemetry: sidecars can emit per-hop spans without code changes, but cannot see inside a service or link across async work, and still need header propagation in the app.
- Continuous profiling for "which function is slow" inside one service.
Failure modes
- Broken context: a hand-written HTTP client, a thread pool or a queue that does not carry the context splits one request into orphan traces.
- Clock skew: spans from different hosts with drifting clocks appear to start before their parents; backends adjust, but durations within a host stay reliable.
- Sampling hides the problem: the rare failing request was not sampled (see tail sampling in the OpenTelemetry lesson).
- Span explosion: a span per loop iteration produces traces with thousands of spans and high cost.
- Blaming the wrong span: picking the longest span rather than the one with the most self time.
In production
Use OpenTelemetry SDKs and auto-instrumentation, propagate W3C Trace Context everywhere (including message headers), name spans by operation (route template, not full URL), add the trace id to log lines, and sample deliberately. Use traces from alerts: exemplars on latency histograms and trace links in error logs shorten investigations considerably.
Common misconceptions
- "The service with the highest latency is the culprit." The entry point always has the highest latency; look at self time.
- "A service mesh gives me tracing for free." The application must still forward the trace headers it received to its outgoing calls.
- "Trace ids and request ids are different things to maintain." Use the trace id as the correlation id.
- "Tracing is too slow for production." Span creation is cheap relative to network calls; cost is mostly export and storage, controlled by sampling.
Interview angle
When asked how to debug latency in a microservice system, explain that metrics show a symptom everywhere, traces locate it: context propagation via traceparent, spans with parent ids, self time and the critical path. Mention propagation through queues, sampling (head vs tail) and linking traces with logs and metrics (exemplars).
Model assumptions
Simulated: a gateway (16 workers, 5 ms), Checkout (16 workers, 20 ms), Inventory (8 workers, 15 ms), Payments (12 workers, 30 ms) and a ledger database (8 connections, 10 ms), called in sequence; callers hold a worker while waiting. Degraded Payments is 8× slower in its own work. Spans quoted in the text are measured from the simulation's hop events (arrival of a request at a service to its answer). The trace context values, the waterfall walk-through, the email queue and the mailer are scripted illustrations; the engine has no notion of trace context. Arrivals are Poisson, service times lognormal, hops a fixed 2–20 ms. Numbers show behaviour, not benchmarks.