Distributed tracing and sampling
Follow one request across many services, and keep the cost under control with head and tail sampling.
15 min readSpans, traces and context propagation
A trace is a tree of spans. Each span records one operation, such as an HTTP handler, a database query or a queue publish, with a start time, duration, status and attributes. The trace ID and parent span ID travel between services in a header (the W3C traceparent standard), so every service adds its spans to the same tree. Propagation is the fragile part: one service or message queue that drops the header splits the trace in two. For asynchronous work, put the context in the message metadata and link the consumer span back to the producer.
Why you must sample
Tracing every request is expensive. A request that crosses 15 services with 3 spans each produces 45 spans of roughly 500 bytes, so about 22 KB per request. At 20 000 RPS that is 440 MB per second, or about 38 TB per day, more than the application itself may store. Sampling keeps a representative fraction. Even 1% gives 200 traces per second, plenty to see typical behaviour across all routes. Low-traffic routes are the exception: at 1% a route with ten requests a minute yields a trace every ten minutes, so many systems guarantee a minimum number of traces per route or service per second.
Head versus tail sampling
The sampling decision can be made at the start or the end of a request, and the choice decides which traces you will have when you need them. During an incident you rarely want a random healthy request; you want the failing and slow ones, which might be 0.1% of traffic. A sampler that keeps 1% at random will capture only one in a hundred of those. The trade-off is cost and complexity in the collector tier versus the value of the traces you keep. Many organisations combine both: a cheap head sample for baseline coverage and a tail sampler for the interesting cases.
- Head sampling: the first service decides (say, keep 1%) and propagates the decision. Cheap and simple, but it throws away 99% of errors and slow requests along with the boring ones
- Tail sampling: collectors buffer all spans of a trace for a few seconds, then keep every error, every request over the latency threshold and a small random share of the rest
- Tail sampling needs all spans of a trace to reach the same collector, so route by trace ID, and enough memory to buffer about 10 seconds of traffic
The tracing pipeline
A typical pipeline has an SDK in each service, a local agent or sidecar, a collector tier that samples and batches, a stream to absorb bursts, and a store indexed by trace ID plus a few searchable attributes such as service, route and status. Keep traces for 7 to 14 days; old traces are rarely useful. Link traces to metrics with exemplars so a spike on a latency graph opens an example slow trace in one click. Aggregated span data is valuable too: deriving service-to-service call graphs and per-edge latency from traces gives you an always-current architecture map.
Key takeaways
- Trace context must propagate through every hop, including queues
- Unsampled tracing can cost more than the system it observes
- Tail sampling keeps the errors and slow requests that head sampling throws away
Sign in to track lessons and earn badges.