Glossary · H

Head-Based Sampling

Trace sampling method that decides when a trace starts whether it is recorded, and passes that decision on to all downstream services.

By Obseria TeamUpdated

Definition

Head-based sampling is a trace sampling method in which the decision to keep or drop a trace is made at its very beginning, usually in the SDK of the first service, before anything is known about the request's outcome. The decision travels with the trace context to downstream services, so all spans of a trace are either recorded or not. Head-based sampling is cheap and simple, but it cannot specifically keep traces with errors or high latency, which is what tail-based sampling is for.

What is head-based sampling?

A distributed trace follows a request through many services and consists of many spans. Recording and storing every trace of a busy system is expensive, so most teams keep only a part of them. This is sampling. With head-based sampling, the decision is made at the "head" of the trace: when the root span is created, typically in the first service that receives the request.

At that moment, nobody knows yet whether the request will fail, be slow or be perfectly ordinary. The decision is therefore based on information available up front, most often a probability such as "keep 10 % of traces", sometimes combined with rules on the route, the service or a rate limit.

How it works

  1. A request arrives at the first service. The SDK creates a new trace ID and asks the configured sampler whether this trace should be recorded.
  2. The sampler returns a decision, for example based on the trace ID and a ratio.
  3. The decision is stored in the trace context and sent along with every outgoing call through context propagation.
  4. Downstream services read the flag and follow it, so the trace is recorded consistently end to end or not at all.

With W3C Trace Context, the decision is the last field of the traceparent header. The flag 01 means sampled, 00 means not sampled:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
             |  |                                |                |
             |  trace ID                         parent span ID   sampled flag
             version

Unsampled requests still carry a trace ID. Logs can include it, but no spans are exported for that trace.

Head-based sampling in OpenTelemetry

The OpenTelemetry SDKs ship with a small set of samplers:

  • AlwaysOn and AlwaysOff: record everything or nothing.
  • TraceIdRatioBased: keeps a fixed ratio of traces, decided deterministically from the trace ID, so the same trace ID always gets the same result.
  • ParentBased: follows the decision of the parent span if there is one and only uses its root sampler for new traces. This is what makes the decision propagate.

The usual production setup is a parent-based sampler with a ratio for root spans. It can be configured with standard environment variables:

OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1

Or in code, here with the Python SDK:

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased

provider = TracerProvider(sampler=ParentBased(root=TraceIdRatioBased(0.1)))

Probabilistic sampling can also happen later, for example in an OpenTelemetry Collector with the probabilistic sampler processor, which hashes the trace ID so that all Collector instances make the same choice. Strictly speaking that is no longer head sampling, but it shares its main property: the decision does not depend on what happened in the trace.

Beyond fixed ratios

A single probability is the most common head-based strategy, but not the only one:

  • Rate limiting: keep at most a fixed number of traces per second per service, so traffic spikes do not multiply the cost. Jaeger, for example, offers rate-limiting samplers.
  • Per-operation strategies: different ratios per endpoint, such as 100 % for a rare payment call and 1 % for a busy search endpoint. Jaeger's remote sampling can distribute such strategies centrally to its clients.
  • Rule-based samplers: custom samplers that drop health checks or always keep requests with a debug header. They can only use attributes that are known when the span starts, such as the HTTP method, the URL or the service name. Whether a framework sets the route template before or after the sampler runs depends on the instrumentation.

Whatever the strategy, the root decides and all children follow, so the rules belong in the services that start traces, typically gateways and frontends.

Head-based vs tail-based sampling

AspectHead-based samplingTail-based sampling
When the decision is madeAt the start of the traceAfter the trace is complete
Information availableTrace ID, service, route, attributes known at startThe whole trace, including errors and duration
Overhead in the applicationLowest: unsampled spans are not exportedAll spans must be exported first
InfrastructureNone beyond the SDKBuffering component, such as a Collector with tail sampling
Keeps all errors and slow tracesNo, only by chanceYes, if configured
Typical useReducing volume cheaply and predictablyKeeping interesting traces while dropping routine traffic

The two are often combined: a moderate head-based ratio limits the cost in the applications, and tail-based sampling later keeps the interesting traces out of what is left.

Common mistakes

  • Counting sampled spans: request and error rates calculated from a 10 % sample are wrong unless they are scaled up. Better derive metrics from all spans or from separate metric instrumentation.
  • Inconsistent decisions: services that ignore the parent decision and sample on their own produce broken traces with missing spans. Use parent-based samplers everywhere.
  • Lost context: if the trace context is not propagated, for example through a message queue or a proxy, downstream services start new traces and sample them independently.
  • One ratio for everything: a rate that is fine for a busy endpoint may leave a rare but critical endpoint with no traces at all.
  • Double sampling without noticing: if the SDK keeps 10 % and the backend keeps another 10 %, only 1 % of traces remain.

Best practices

  • Start with a ratio you can afford and lower it step by step while checking that rare paths still appear.
  • Use the same sampler configuration in all services, ideally through environment variables managed centrally.
  • Record metrics independently of trace sampling, so dashboards and alerts stay accurate.
  • Write the trace ID into logs, so you can still correlate logs of unsampled requests.
  • Document where sampling happens, in the SDK, in a Collector or in the backend, and at which rates.

The observability perspective

Head-based sampling is a trade-off between cost and visibility. It reduces overhead where it matters most, in the application, but it is blind to outcomes: the one failed checkout in a thousand may simply not be recorded. Teams should treat sampled traces as examples for understanding latency and dependencies, and rely on metrics computed from complete data for rates, percentiles and alerts. Where specific error traces must always be available, tail-based sampling or a higher head ratio for critical routes is needed.

How Obseria helps

Obseria accepts OpenTelemetry traces natively, so the head-based samplers configured in your SDKs work unchanged. In addition, trace sampling in the Obseria Pipeline samples per trace ID at ingest, before storage, so every kept trace stays complete across all services. Rules for converting spans to metrics can run before the sampling stage, which keeps request rates, error rates and latencies in metrics monitoring based on all spans that arrive. Kept traces appear in distributed tracing like any other trace. Keep in mind that ingest sampling comes on top of any sampling in the SDK.

Frequently asked questions

Because the decision is made at the head of the trace, when the root span is created, before any downstream work has happened. Tail-based sampling decides at the tail, after the trace is complete.

Not if all services use a parent-based sampler and the trace context is propagated. Then every span follows the root decision. Traces break when services decide independently or when context is lost, for example across a queue.

There is no universal value. It depends on traffic, the number of services, the budget and how rare the important paths are. Start higher than you think you need, measure the volume and reduce step by step.

No. At the start of a trace it is not known whether an error will occur. To keep all error traces, use tail-based sampling, typically in an OpenTelemetry Collector.

ParentBased is an OpenTelemetry sampler that respects the sampling decision of the parent span. Only for new root spans does it delegate to another sampler, such as TraceIdRatioBased.

They are wrong if you calculate them from sampled spans without correction. Compute rates and latencies from all spans before sampling, or from separate metric instrumentation, and use sampled traces as examples.

Put these concepts into practice

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