Modern applications generate a large amount of telemetry. Every API request, database operation, message queue interaction, and service-to-service call can produce multiple trace spans. As applications grow, the number of spans increases, and storing all of them can become expensive.
Many organizations use OpenTelemetry to collect distributed traces and send them to observability platforms. However, retaining every trace is not always necessary. Most successful requests follow normal execution paths, while errors, slow requests, and unusual behavior provide more valuable information during troubleshooting.
Tail sampling helps solve this problem. Instead of deciding whether to keep a trace when it starts, the system waits until it has received enough information about the trace. It then evaluates the complete or nearly complete trace and decides whether to retain it.
For example, an application might generate 100,000 traces per hour. You may want to retain every trace containing an error, every request taking more than one second, and a small percentage of normal requests. This approach can significantly reduce the amount of trace data sent to your backend.
In this article, you will learn how tail sampling works, how to configure it using the OpenTelemetry Collector, how to create practical sampling policies, and how to deploy it safely in production environments.
What Is Tail Sampling in OpenTelemetry?
Tail sampling is a distributed tracing strategy in which the sampling decision is made after the spans belonging to a trace have been collected.
A distributed trace represents the complete journey of a request through an application. It can contain multiple spans generated by different services.
For example, an e-commerce request might follow this path:
API Gateway
|
v
Order Service
|
v
Payment Service
|
v
Inventory Service
|
v
DatabaseEach service generates spans containing information about its operation, duration, status, and attributes.
With head sampling, the sampling decision is made when the trace begins. If the trace is not selected, its spans are generally not recorded or exported for that trace.
With tail sampling, the system collects spans first and makes the decision after examining the trace.
This allows you to retain traces based on information that may not be available when the request starts.
For example, a request might begin normally but fail while communicating with a payment service. Head sampling cannot make its initial decision based on that future error. Tail sampling can inspect the completed trace and retain it because one of its spans contains an error.
Head Sampling vs. Tail Sampling
Both strategies help control telemetry volume, but they solve the problem differently.
Feature | Head Sampling | Tail Sampling |
|---|---|---|
Sampling decision | At the beginning of a trace | After collecting trace data |
Error-based selection | Cannot reliably use future errors | Can select traces containing errors |
Latency-based selection | Cannot know final latency at the start | Can select slow traces |
Resource usage | Usually lower | Higher because spans must be buffered |
Architecture | Relatively simple | Requires stateful processing |
Sampling consistency | Requires consistent sampling decisions across services | Requires all spans of a trace to reach the same sampling instance |
Best use case | Reducing telemetry at the source | Retaining important traces based on their outcomes |
Head sampling is useful when you need predictable sampling with low processing overhead. Tail sampling is useful when trace quality and diagnostic information are more important than making an immediate sampling decision.
Some architectures use both approaches, but combining them requires careful planning. If head sampling discards a trace before it reaches the tail sampler, the tail sampler cannot recover its missing spans.
How the Tail Sampling Processor Works
The OpenTelemetry Collector provides a tail sampling processor that evaluates traces using configurable policies.
The general workflow looks like this:
Application Services
|
v
OpenTelemetry SDKs
|
v
OpenTelemetry Collector
|
v
Tail Sampling Processor
|
v
Buffer Spans by Trace ID
|
v
Evaluate Sampling Policies
|
+----------------------+
| |
v v
Keep Trace Drop Trace
|
v
OTLP Exporter
|
v
Observability BackendThe processor performs several important operations.
Collecting Spans
The Collector receives spans from instrumented applications through receivers such as OTLP over gRPC or HTTP.
Each span contains a trace ID that identifies the distributed trace to which it belongs.
The tail sampling processor uses this trace ID to group related spans.
Buffering Trace Data
The processor temporarily stores spans in memory while waiting for additional spans from the same trace.
This is necessary because a request may generate spans across several services, and those spans may arrive at different times.
Waiting for a Sampling Decision
The decision_wait setting controls how long the processor waits before evaluating a trace.
For example:
processors:
tail_sampling:
decision_wait: 15sThis configuration tells the processor to wait for the configured decision window before evaluating buffered trace data.
The appropriate value depends on application latency, service communication patterns, and span delivery delays.
A longer waiting period gives more time for delayed spans to arrive, but it also increases memory usage and the time before traces become available in the backend.
Evaluating Sampling Policies
After the waiting period, the processor evaluates the configured policies.
For example, it can retain traces based on:
Error status codes.
Overall trace duration.
Specific span attributes.
Service names.
Numeric attribute values.
A probabilistic sampling percentage.
If a trace matches a policy that selects it for retention, the processor forwards its spans to the configured exporter.
If it does not match the retention policies, the trace is dropped.
Configure Tail Sampling in the OpenTelemetry Collector
Let's implement a practical configuration that retains error traces, slow requests, and a small percentage of normal traffic.
The following example uses the OpenTelemetry Collector distribution that includes the tail sampling processor, such as the Contrib distribution.
Create a Collector configuration file named otel-collector-config.yaml.
Configure the OTLP Receiver
The receiver accepts telemetry from instrumented applications.
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318Applications can send traces through either supported OTLP protocol.
The receiver listens on ports 4317 and 4318 in this example. In production, expose these endpoints through an appropriately secured network and configure authentication and TLS where required.
Configure the Memory Limiter
Tail sampling temporarily stores spans in memory. A high trace volume or long decision window can increase Collector memory consumption.
Use the memory limiter processor to protect the Collector.
processors:
memory_limiter:
check_interval: 1s
limit_mib: 1500
spike_limit_mib: 300The configuration defines a hard memory limit of 1500 MiB and a spike allowance of 300 MiB. The soft limit is therefore 1200 MiB.
These are example values, not universal production settings. Adjust them based on the Collector container memory limit, traffic volume, and observed memory usage.
The memory limiter should be placed first in the pipeline so it can apply backpressure before downstream processors accumulate additional data.
Configure Tail Sampling Policies
Now configure the tail sampling processor.
processors:
tail_sampling:
decision_wait: 15s
num_traces: 50000
expected_new_traces_per_sec: 1000
policies:
- name: retain-errors
type: status_code
status_code:
status_codes:
- ERROR
- name: retain-slow-traces
type: latency
latency:
threshold_ms: 1000
- name: retain-normal-traffic
type: probabilistic
probabilistic:
sampling_percentage: 5This configuration defines three policies.
The first policy retains traces containing spans with an error status.
The second policy retains traces whose calculated duration exceeds one second.
The third policy selects a five-percent probabilistic sample of the remaining traffic.
The policies provide multiple ways to retain useful trace data. A trace that matches an applicable retention policy is selected for sampling.
The num_traces setting controls the approximate number of traces the processor can hold in its in-memory buffer. It is not a percentage or a limit on the total number of spans.
The expected_new_traces_per_sec setting helps the processor allocate its internal data structures based on expected traffic.
You should tune these values using actual trace volume and Collector memory measurements rather than copying them directly into a production deployment.
Configure the OTLP Exporter
The exporter sends selected traces to your observability backend.
For example:
exporters:
otlp:
endpoint: tracing-backend.example.com:4317
tls:
insecure: falseReplace the example endpoint with your actual backend endpoint.
Configure the required authentication headers, certificates, or other security settings according to your tracing platform.
Configure the Traces Pipeline
Finally, connect the receiver, processors, and exporter.
service:
pipelines:
traces:
receivers: [otlp]
processors:
- memory_limiter
- tail_sampling
- batch
exporters: [otlp]The order is important.
The memory limiter runs first to protect the Collector. Tail sampling then evaluates traces and drops those that do not match the retention policies. The batch processor groups the retained spans into batches before export.
The complete configuration combines the receiver, processors, exporter, and service pipeline:
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
memory_limiter:
check_interval: 1s
limit_mib: 1500
spike_limit_mib: 300
tail_sampling:
decision_wait: 15s
num_traces: 50000
expected_new_traces_per_sec: 1000
policies:
- name: retain-errors
type: status_code
status_code:
status_codes: [ERROR]
- name: retain-slow-traces
type: latency
latency:
threshold_ms: 1000
- name: retain-normal-traffic
type: probabilistic
probabilistic:
sampling_percentage: 5
batch:
exporters:
otlp:
endpoint: tracing-backend.example.com:4317
tls:
insecure: false
service:
pipelines:
traces:
receivers: [otlp]
processors:
- memory_limiter
- tail_sampling
- batch
exporters: [otlp]
Join the conversation! Your thoughts help the community grow.