Tail-Based Trace Sampling
Tail-based trace sampling is a tracing strategy that waits until a trace is complete, then decides whether to keep it. A collector temporarily buffers that trace, examines facts such as errors and total latency, and exports only traces that match the policy.
The problem it solves
A trace is the end-to-end record of one operation as it crosses services. It is made of spans, which represent individual operations such as an HTTP handler, a database query, or an outbound RPC. Every span carries a trace ID, the identifier that connects it to the other spans from the same request.
Without sampling, tracing systems must store and transmit a span for every operation. That produces useful detail, but the volume can be expensive: one user request might create dozens of spans, and a busy service can create millions per hour.
The simplest alternative is head-based sampling. The first service makes the decision when the request starts—for example, keep 1% of traces—and places that choice in the trace context, the metadata propagated between services. Every service follows the choice. This is cheap and consistent, but it must decide before anyone knows whether the request will fail or become slow. A sampled request can be uninteresting, while a discarded request might contain the incident you need to investigate.
Tail sampling reverses that timing. It can say, in effect:
Keep every trace with an error, keep traces slower than one second, and sample only a small fraction of successful fast traces.
Buffering a trace
Tail sampling normally runs in a tracing collector rather than inside each application. Applications send spans to the collector as they finish. The collector groups incoming spans by trace ID and stores them in an in-memory or bounded temporary buffer.
It cannot immediately export a trace when the first span arrives, because that span does not reveal the eventual outcome. Instead, the collector waits for a configured period called the decision wait time. During that period it accepts spans from all participating services, including spans that were created later or arrived out of order.
A simplified lifecycle looks like this:
- A request starts with trace ID
abc123. - Service A emits a span, followed by services B and C.
- The collector routes all three spans for
abc123to the same trace-sampling worker. - The worker waits for the decision window or for an explicit end-of-trace signal, if the tracing system has one.
- It evaluates the complete set of spans.
- It exports the trace or drops it.
Most distributed tracing systems do not provide a reliable “the last span has arrived” signal. A collector therefore usually relies on a timeout. The wait must be long enough for normal network delays, retries, and asynchronous work, but not so long that buffered traces consume excessive memory.
This is not permanent retention of every request. The collector retains only spans for traces currently inside the decision window. Once a trace is accepted and exported, or rejected and discarded, its buffered spans are released. A production setup still needs bounds: a maximum number of traces, a maximum memory limit, and an action for spans that arrive after eviction or timeout.
Choosing traces after completion
A tail-sampling policy examines attributes collected across the trace. Common predicates include:
- Status or error: retain a trace containing an application error, failed RPC, or non-success HTTP result.
- Latency: retain a trace whose total duration exceeds a threshold.
- Route or service: retain requests to a checkout endpoint or traces involving a particular service.
- Fallback probability: retain a small percentage of traces that matched none of the important conditions.
Policies are usually evaluated with precedence. For example, an error rule should keep a failed trace even if a later probabilistic rule would discard it. A policy can also combine conditions, such as retaining slow traces from a particular endpoint.
The collector must make this decision from span data that actually arrived. If a service never emitted its span, or the span arrived after the buffer was evicted, the policy cannot inspect it. Tail sampling improves the decision point; it does not repair incomplete instrumentation or lost telemetry.
Propagating the decision
The trace ID is propagated at request start, usually in a standard trace-context header. That lets every service attach its spans to the same trace and lets the collector route those spans to one decision-maker. This is the first kind of propagation: propagate identity now, decide retention later.
The final tail decision is different. A collector usually cannot travel backward in time and tell the already-finished application not to create or send spans. It also cannot reliably change a trace-context header that has already crossed several services. For that reason, tail sampling requires the pipeline to receive enough spans for all candidate traces, at least until the collector decides.
After the decision, the collector propagates the result forward by exporting all buffered spans of a kept trace together. Downstream collectors or storage systems see the trace as sampled or retained, depending on the telemetry protocol and exporter. Some vendor-specific systems can also distribute a later sampling decision to components that emit future or asynchronous spans. That can reduce subsequent work, but it is an extension, not a general property of tail sampling.
This distinction explains a common configuration surprise: setting an application’s head sampler to keep only 1% can prevent a tail sampler from seeing the other 99% at all. To make error and latency rules reliable, applications commonly record and send all spans to a collector, while the collector performs the selective export.
The trade-off
Tail sampling spends resources before it saves them. The collector needs memory, CPU, a decision window, and a way to distribute traces consistently across collector instances. If spans for one trace land on different workers, neither worker has the complete evidence, so deployments commonly route by trace ID.
In return, the system preserves the traces engineers most often need during an incident: failures, slow requests, and unusual paths—without permanently storing every ordinary request. The buffer size, wait time, fallback policy, and handling of late spans are therefore operational settings, not incidental implementation details.