# Telemetry sampling

## What it is

Telemetry sampling selects a portion of a stream of observations for collection
or retention. A busy service can produce many traces, events or log records
while it works. Sampling can keep a useful portion within a manageable
processing and storage budget. A sampling policy describes what can be kept,
when the decision happens and which questions the retained observations can help
answer.

With trace sampling, a head decision happens early, often when a request begins
a trace. It can use attributes available at that time and may follow a decision
passed from another component. A tail policy can decide later, after seeing more
of the trace. It can keep examples based on the final outcome, latency or other
information that appears during the operation. It needs time and resources to
collect enough spans before deciding.

Sampling can be random, rule-based or a combination. A broad sample can describe
common work. A targeted sample can help find rare failures. The appropriate
policy depends on which evidence a team needs and what changes the policy makes
to the retained population. A retained sample can describe examples more
completely than it describes the true frequency of all outcomes.

## Why it matters

Detailed tracing requires processing, network capacity and storage. Collecting
every data point can compete with the service it describes. A sampling policy
can keep enough examples available for diagnosis and reserve capacity for work
that needs more attention. It gives a team a way to make those decisions as
traffic changes.

Rare failures deserve particular care. If a service keeps a very small random
portion of traces, an error that occurs infrequently may have no retained
example during an incident. A tail policy can keep selected failures once their
result is known. This improves diagnostic evidence. It can also change the
distribution in storage because the policy retains more failures than ordinary
successes. Publish reliability counts using a measurement designed for that
purpose.

Think about how evidence remains useful across a system. If one service keeps
one span and another decides independently to remove the span connected to it,
the result can be hard to follow. Use propagation and consistent decisions where
complete traces matter. Some transitions call for separate policies, such as
when traffic crosses an organisational or privacy boundary. Define which
information can be shared and how a receiver starts its own internal
observation.

## What it looks like in practice

Imagine a trace pipeline for API requests. Use a modest baseline sample for
ordinary requests. Give a later decision process a policy to retain traces with
an error or a performance problem the team can investigate. Inject a controlled
error after an upstream hop and a slow response whose final status is
successful. Check how each appears in the retained traces, including the spans
that were needed to explain why it happened.

Set a realistic time for a trace to arrive at the decision point. A collector
might see a request's initial spans long before slower work finishes. A finite
decision buffer can protect the telemetry service. Measure late arrivals,
discarded data and how long each retained trace takes to appear. If events often
reach the collector out of order, exercise a deliberately delayed span and
decide which partial results can still be useful.

Engineer's
[first-party monitoring case study](/portfolio/built-first-party-error-monitoring-and-tracing/)
describes OpenTelemetry traces, sanitised alerts and a heartbeat for the
observation pipeline itself. A sampling policy belongs inside the same operating
discipline. The illustrative test checks both an application's retained details
and what the pipeline reports when its own capacity is exhausted. A missing
trace needs an explanation people can observe.

Keep independent aggregate measurements where the service needs an overall rate.
For example, counters recorded at the user-facing request boundary can describe
the proportion of failures. A curated set of traces can then explain particular
failures. Verify both using injected traffic and a known number of outcomes,
with the sampling policy active.

## Where it goes wrong

A common mistake is to treat the retained trace set as a census. If the policy
keeps nearly every failed request and only a small portion of successes, a graph
built from those records can overstate the overall failure rate. Document that
bias and provide the population measurement from another signal. A sample's
useful size is tied to the question it answers.

Tail decisions can also increase the cost of a collector during an incident. If
many traces suddenly qualify for retention, the downstream store can become the
bottleneck. Define what happens when buffers fill, and test a burst of failures
without depending on a healthy baseline. Use the same trace identity throughout
the processing path where a complete trace is needed.

Consider obligations that require a complete record, such as a durable
application audit trail or a completed job's status. Those records have a
separate lifecycle and need a clear storage and retention contract. Configure
sampling at the layer that owns diagnostic evidence, then test that the required
product record remains available whether a corresponding trace is kept or
dropped. If the team needs a statistical estimate from a sample, establish the
sampling method and inclusion probabilities before interpreting it.

Finally, preserve useful provenance when changing policies. Record the version
of the rule and relevant periods when it was active. A policy change can reduce
the number of stored traces. Compare a stable aggregate measure before
attributing that decline to the product. A reliable sampling design gives
operators usable diagnostic examples and an explicit indication of the evidence
that was missed or filtered.

## Further reading

- [OpenTelemetry sampling](https://opentelemetry.io/docs/concepts/sampling/).
  Describes head and tail policies and their trade-offs.
- [OpenTelemetry trace SDK sampling](https://opentelemetry.io/docs/specs/otel/trace/sdk/#sampling).
  Specifies decisions and the data available to SDK samplers.
- [OpenTelemetry Collector tail sampling processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/tailsamplingprocessor).
  Documents a practical collector implementation and its limits.

<https://engineer.company/glossary/telemetry-sampling/>
