Troubleshooting guide
OpenTelemetry sampling: head vs tail, and how to choose
Sampling decides which traces you keep. Head sampling is cheap but blind. Tail sampling can keep every error, at the cost of memory.
3 min read10 sectionsUpdated
On this page
The two approaches
Head sampling makes the decision when the trace begins, based on the trace ID, and it costs almost nothing. It cannot know if a trace will fail. Tail sampling buffers all spans for a short time, then applies rules once the trace is complete, so it can keep errors and slow requests.
# Head sampling in any SDK: keep 10 percent, follow the parent's decision
export OTEL_TRACES_SAMPLER=parentbased_traceidratio
export OTEL_TRACES_SAMPLER_ARG=0.1
# Tail sampling in the Collector (config excerpt)
processors:
tail_sampling:
decision_wait: 10s
policies:
- { name: errors, type: status_code, status_code: { status_codes: [ERROR] } }
- { name: slow, type: latency, latency: { threshold_ms: 1000 } }
- { name: rest, type: probabilistic, probabilistic: { sampling_percentage: 5 } }Choosing
Start with no sampling if volume is manageable. When cost pushes you, use parentbased_traceidratio at a rate that still leaves enough traces for your low-traffic endpoints. Add tail sampling when you need every error. With more than one Collector, put a first tier running loadbalancing exporter by trace ID in front of the sampling tier, so that all spans of a trace reach the same instance.
The memory cost of tail sampling
The tail sampler must keep every span in memory for decision_wait. At 10,000 spans per second and 10 seconds of wait with 1 KB per span, that is about 100 MB of data before overhead, and traffic spikes multiply it. Traces that take longer than the wait are decided on incomplete data. Set decision_wait a little above your typical maximum trace duration, set num_traces accordingly and monitor the sampler's own metrics, because dropped decisions look like random gaps in your data rather than an obvious failure.
Catch it automatically
Verify the effect: compare the trace count with the known request rate. APM needs correct rates from metrics even when traces are sampled. See trace sampling, and reducing observability costs.
Related reading
- What is a span in tracing?: What a span is in distributed tracing: its fields, parent and child spans, span kinds, attributes and status, with an annotated example you can read.
- ECS and Fargate monitoring: Monitor Amazon ECS and Fargate: CPU and memory per service, stopped-task reasons, ALB target health, logs and traces.
- How to monitor a Next.js app: How to monitor a Next.js app in production: uptime checks, Core Web Vitals, server and edge errors, OpenTelemetry traces for SSR and API routes, and alerts.
- OpenTelemetry for FastAPI: Trace a FastAPI service with OpenTelemetry: instrument the app, async database drivers and httpx, run uvicorn workers correctly and export.
- OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
- OpenTelemetry for PHP and Laravel: Trace a Laravel app with OpenTelemetry: install the PHP extension and auto-instrumentation packages, configure PHP-FPM and trace queries.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.