Glossary · T

Tail-based Sampling

Trace sampling method that decides whether to keep a trace only after all or most of its spans have arrived, based on properties such as errors or latency.

By Obseria TeamUpdated

Definition

Tail-based sampling is a trace sampling approach in which the keep or drop decision is made at the end of a trace instead of at its start. A component such as the OpenTelemetry Collector buffers all spans of a trace for a short time, evaluates the complete trace against policies, for example contains an error or exceeds a latency threshold, and then stores or discards it as a whole. This keeps the interesting traces while reducing the volume of routine traffic, at the cost of buffering and a more complex pipeline.

What is tail-based sampling?

Storing every trace of a busy system is expensive, so most teams use sampling. The question is when the decision is made. With head-based sampling it happens at the start of a trace, usually in the first service, based on the trace ID and a probability. That is cheap and simple, but blind: at the start nobody knows whether the request will fail or be slow.

Tail-based sampling waits until the trace is complete. All spans are collected, the whole trace is evaluated, and only then is it kept or dropped. Because the decision sees the outcome, you can keep every trace with an error and every slow request, while keeping only a small share of fast, successful ones.

Head-based vs. tail-based sampling

Head-basedTail-based
Decision timeAt the root spanAfter the trace is complete
Knows errors and latencyNoYes
Resource costVery lowBuffering in memory, extra pipeline tier
Saves network traffic from servicesYes, unsampled spans are not sentNo, all spans are sent to the sampler
ComplexityLowHigher, spans of a trace must meet in one place

Both approaches can be combined: a moderate head sampling rate reduces raw volume, and tail-based sampling then picks the valuable traces from what remains.

Typical sampling policies

The tail_sampling processor of the OpenTelemetry Collector (contrib distribution) evaluates policies per trace. Common types are:

  • status_code: keep traces that contain a span with status ERROR.
  • latency: keep traces whose duration exceeds a threshold.
  • string_attribute / numeric_attribute: keep traces with specific attribute values, for example a particular customer tier or endpoint.
  • probabilistic and rate_limiting: keep a baseline share of normal traffic, or a maximum number of spans per second.
  • composite, and and ottl_condition: combine conditions or express them in OTTL.

As an illustration, a configuration that keeps all errors, all traces slower than two seconds and ten percent of the rest:

processors:
  tail_sampling:
    decision_wait: 10s
    num_traces: 50000
    policies:
      - name: errors
        type: status_code
        status_code: {status_codes: [ERROR]}
      - name: slow
        type: latency
        latency: {threshold_ms: 2000}
      - name: baseline
        type: probabilistic
        probabilistic: {sampling_percentage: 10}

A trace is kept if any of the policies decides to sample it. decision_wait defines how long the processor buffers spans after the first span of a trace arrives.

Architecture: all spans in one place

Tail-based sampling only works if all spans of a trace reach the same sampler instance. With several Collector replicas behind a normal load balancer, spans of one trace would be spread across instances, and each would see only fragments. The usual solution is a two tier setup: a first tier of Collectors uses the loadbalancing exporter with the trace ID as routing key, and a second tier runs the tail_sampling processor. Changes in the number of second tier instances briefly move traces to other instances, which can split a few traces during scaling. Our guide to tail-based sampling with the OpenTelemetry Collector walks through this setup, and incomplete distributed traces covers what happens when spans get separated.

Trade-offs and pitfalls

  • Memory: the sampler holds every trace for the duration of decision_wait. Throughput times wait time determines the memory needed, and num_traces must be sized accordingly, otherwise traces are dropped early.
  • Late spans: spans arriving after the decision, for example from long running async jobs, cannot change it. Too short a wait time cuts traces, too long a wait time increases memory.
  • Skewed metrics: if you derive request rates or latency percentiles from sampled traces, errors and slow requests are overrepresented. Compute RED metrics from the full stream before sampling, for example with the spanmetrics connector.
  • Cost shift: tail sampling reduces storage, not the traffic from services to the sampler. Network and pipeline compute still scale with the full volume.

How Obseria helps

Obseria ingests OpenTelemetry traces natively via OTLP. Trace sampling in Obseria works at ingest by trace ID, so it is head-style sampling, not tail-based: Obseria does not buffer traces to keep all errors or slow requests automatically. If you need tail-based sampling, run the tail_sampling processor in your own OpenTelemetry Collector and send the retained traces to Obseria. The Obseria Pipeline can additionally filter and reduce data before storage, and distributed tracing shows the retained traces with correlated logs. Keep in mind that metrics derived from traces only reflect the spans that reach Obseria. For a cost focused approach, read reducing observability costs pipeline-first.

Frequently asked questions

Head-based sampling decides at the start of a trace, without knowing its outcome. Tail-based sampling decides after the trace is complete and can therefore keep traces with errors or high latency selectively.

The sampling decision is based on the complete trace. If spans are spread across several instances, each sees only part of the trace and makes inconsistent decisions. A loadbalancing exporter that routes by trace ID solves this.

Not between services and the sampler, because all spans must be sent there first. It reduces what is stored and forwarded afterwards. Combining it with moderate head sampling also reduces upstream traffic.

Only if they are computed before sampling. Metrics derived from sampled traces overrepresent errors and slow requests, so request rate, error rate and latency should come from the full span stream.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.