Course contents · Lesson 14 of 22

Trace Sampling Explained: Head vs Tail Sampling

Traces · Lesson 14 of 22 · 10 min Engineer

Your shop handles 2,000 requests a second, and each request produces about 20 spans. Recording all of them means 40,000 spans a second, more than 3 billion a day, to send, index and pay for. Trace sampling is how you choose which traces to keep and which to throw away. Where you make that choice decides whether the one trace you need during an incident is still there when you go looking.

Why sample traces at all

Tracing every request costs you three times. Each span uses network bandwidth to leave the app, the backend stores and indexes it, and most hosted tracing tools bill by the volume of spans or bytes they ingest. At low traffic you can keep everything. At thousands of requests a second, almost all of what you store is near-identical successful traces that nobody ever opens.

Sampling keeps a subset. The unit is the whole trace: a sampler keeps or drops all the spans of one request together, because half a trace is hard to read. The real question is when the decision is made, and that is the whole difference between head and tail sampling.

One rule before the details: count with metrics, not with sampled traces. Request and error rates belong in counters, which see every request. A sampled trace store answers "show me an example", not "how many".

Head sampling: decide at the start

Head sampling decides when the root span is created, before any work has happened. The sampler knows the trace ID and little else, so it cannot know whether this request will fail or be slow. It records its decision in the sampled flag of the traceparent header: 01 to record, 00 to skip. Downstream services read the flag and follow it, so the whole trace is kept or dropped together.

In OpenTelemetry SDKs you set this with two standard environment variables, read when the app starts. This example keeps 10% of new traces:

export OTEL_TRACES_SAMPLER=parentbased_traceidratio
export OTEL_TRACES_SAMPLER_ARG=0.1

traceidratio keeps a fraction of traces, here 0.1 or 10%, computed from the trace ID itself, so the decision for a given trace is repeatable. parentbased means: if the incoming request already carries a sampled flag, obey it, and use the ratio only for new root spans. Without these variables the SDK uses parentbased_always_on, which keeps everything. A dropped span still exists in memory as a non-recording span, so its context keeps propagating with flag 00, but nothing is exported.

Head sampling is cheap and simple. Dropped traces never leave the app, so you save network, CPU and storage at once, and no extra infrastructure is needed. Its weakness is that it is blind: at 10%, it also throws away 90% of your errors and 90% of the slow requests you most wanted to study.

Tail sampling: decide after the trace ends

Tail sampling waits until a trace is complete and then decides with all of its spans in hand. Now the rules can be about outcomes: keep every trace with an error, keep every trace slower than 500 ms, and keep 10% of the rest. The apps send all their spans to an OpenTelemetry Collector, a separate program that receives, processes and forwards telemetry, and its tail_sampling processor makes the decision.

Two flowcharts: head sampling checks only whether a new request falls in the 10% and keeps or drops it; tail sampling buffers the finished trace, checks error, latency and 10% rules, and keeps the trace if any rule matches
The head sampler answers a question about luck; the tail rules answer a question about what actually happened.

A collector cannot tell when a trace is finished, because no span is marked as the last one. So it waits: it buffers each trace for decision_wait after the trace's first span arrives, then evaluates the policies, and keeps the trace if any policy matches. This configuration implements the three rules above:

processors:
  tail_sampling:
    decision_wait: 10s     # wait this long after a trace's first span
    num_traces: 100000     # traces held in memory at once
    policies:
      - name: keep-errors
        type: status_code
        status_code: {status_codes: [ERROR]}
      - name: keep-slow
        type: latency
        latency: {threshold_ms: 500}
      - name: baseline
        type: probabilistic
        probabilistic: {sampling_percentage: 10}

The processor ships in the contrib distribution of the collector (otelcol-contrib). The latency policy measures from the earliest span start to the latest span end in the trace. Without these settings, decision_wait is 30 s and num_traces is 50,000. A trace that runs longer than the wait is judged only on the spans that had arrived by then.

Bar charts for 1,000 requests with 10 errors: head sampling at 10 percent keeps about 1 error trace and about 100 traces; tail rules keep all 10 errors and about 109 traces
For almost the same storage, the tail rules keep every failure while head sampling keeps about one of them.

The trade-offs, and one rule you cannot skip

Tail sampling buys that visibility with cost and complexity. Every span still crosses the network to the collector, so you save storage but not export traffic. The collector holds every in-flight trace in memory for the whole wait: at 2,000 new traces a second and a 10-second wait, that is about 20,000 traces buffered at any moment. And you now run, size and monitor one more tier of servers.

The rule you cannot skip: all spans of a trace must reach the same collector instance. Behind an ordinary round-robin load balancer, three collectors each see a third of every trace, and the one holding only healthy spans drops a trace whose error span went elsewhere. The fix is two tiers. A front tier uses the loadbalancing exporter with routing_key: traceID, which sends every span of a trace to the same back-tier collector, and the back tier runs tail sampling.

Two lanes: head sampling decides in the app and sends only 10 percent onward; tail sampling sends 100 percent through a load balancer that routes by trace ID to two sampler collectors where the decision is made
Moving the decision later trades a thin, cheap pipe and a blind choice for a thick pipe, a memory buffer and a choice that sees everything.

A sensible default

A good starting point uses both, each for what it does best. Leave the SDKs on parentbased_always_on so every span reaches the collectors, and let tail rules keep all errors, all slow traces and a small baseline, 5 to 10%, of everything else. The baseline matters because it shows you what a normal request looks like, so you have something to compare the bad traces against.

If sending every span costs too much, add a head ratio in the SDK, say 25%, as a pressure valve. Remember that the two stack: the tail rules can only choose among the traces that head sampling kept, so an error dropped at the head is gone for good. Set the ratio only where traces start and let parentbased carry that decision to every service downstream.

Check yourself

Your SDKs sample 10% at the head and the collector's tail rules keep all errors. Do you see every error?

No: the tail rules only see the 10% of traces the head kept, so about 90% of errors never reached the collector.

Why must every span of a trace reach the same tail-sampling collector?

The decision needs the whole trace; a collector that holds only part of it may drop a trace whose error span went to another instance.

With head sampling, how does a downstream service know whether to record its spans?

It reads the sampled flag in the incoming traceparent (01 or 00), and a parent-based sampler follows it.

Go deeper