1% head sampling threw away almost every trace of the errors we were trying to debug
A checkout failure affected 0.3 percent of requests. With 1 percent head sampling, the chance of keeping any given failing trace was one in a hundred, and the few we kept were missing spans. Why head sampling decides too early, and how tail sampling in the OpenTelemetry Collector keeps every error and slow trace.
Customers reported intermittent "Payment could not be processed" errors at checkout. Our error rate dashboard showed it clearly: about 0.3 percent of checkout requests returning 502, spread evenly through the day. Not enough to trip the SLO alert, enough for support to take forty tickets a day.
We use distributed tracing with OpenTelemetry, so the plan was simple: find a trace of a failed checkout and see
which service failed. In the tracing backend, searching for http.response.status_code = 502 on the checkout
route over the last 24 hours returned 61 traces. The service had served about 1.4 million checkout requests that
day, so roughly 4,200 had failed.
61 out of 4,200. And of those 61, most were incomplete:
trace 7f3a... checkout-api POST /checkout 502 2.31s
├─ checkout-api validate cart 12ms
├─ checkout-api call payments 2.29s ERROR
└─ (no child spans from payments-service)
The span for the call to the payments service showed the error, but there were no spans from the payments service itself, which is where the failure happened. We had the symptom and not the cause.
The sampling configuration
# SDK configuration in each service
OTEL_TRACES_SAMPLER: parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG: "0.01"
1 percent head sampling, chosen to control the cost of the tracing backend at about 40,000 spans per second across all services.
Head sampling decides before anything happens
With head sampling, the decision to keep or drop a trace is made at the start, when the root span is created,
usually by hashing the trace ID against the ratio. The parentbased part means downstream services respect the
decision passed in the trace context, so a trace is either fully kept or fully dropped across services, in
principle.
Head sampling is cheap and simple: dropped traces are never even recorded. But at the moment of the decision, the system knows nothing about how the request will go. It cannot know that this request will fail, or be slow. A 1 percent sample keeps 1 percent of errors, just as it keeps 1 percent of everything else.
For a failure affecting 0.3 percent of requests, that means about 42 failing traces kept out of 4,200 per day, if everything else worked. Which it did not.
Why the kept traces were incomplete
Two services in the checkout path did not respect the parent's decision:
- The payments service was configured with
OTEL_TRACES_SAMPLER=traceidratio(withoutparentbased), so it made its own independent 1 percent decision. A trace kept by checkout had only a 1 percent chance of being kept by payments too. Most of our kept failing traces had no payments spans. - A legacy fraud-check service used an older tracing library that ignored the incoming sampled flag and sampled everything, which produced orphan spans with no parent in the backend.
Inconsistent sampling across services is easy to end up with, because each team configures its own SDK.
Environment
| Item | Value |
|---|---|
| Tracing | OpenTelemetry SDKs (Java, Go, Python), OTLP to a collector, commercial backend |
| Sampling | head, 1% ratio, inconsistent parentbased across services |
| Failure rate | 0.3% of checkouts |
| Useful traces of the failure per day | effectively zero complete ones |
1. Tail sampling in the OpenTelemetry Collector
Tail sampling makes the decision after a trace has finished, when it is known whether it contains an error or was slow. Services record and export 100 percent of spans to the collector; the collector buffers all spans of a trace for a short time, then applies policies and forwards only the traces that match.
processors:
tail_sampling:
decision_wait: 10s
num_traces: 200000
expected_new_traces_per_sec: 5000
policies:
- name: keep-errors
type: status_code
status_code: { status_codes: [ERROR] }
- name: keep-slow
type: latency
latency: { threshold_ms: 1500 }
- name: keep-checkout-5xx
type: numeric_attribute
numeric_attribute: { key: http.response.status_code, min_value: 500, max_value: 599 }
- name: baseline
type: probabilistic
probabilistic: { sampling_percentage: 1 }
Policies are combined with OR: a trace is kept if it has an error span, or took over 1.5 seconds, or returned a 5xx, or falls in the 1 percent baseline. The baseline keeps a representative sample of normal traffic, so we can still compare healthy and failing traces.
decision_wait is how long the collector waits after the first span before deciding. It must be longer than the
longest normal trace, or late spans arrive after the decision and are dropped.
2. Route all spans of a trace to the same collector
Tail sampling only works if one collector instance sees every span of a trace. With several collector replicas
behind a normal load balancer, spans of one trace end up on different instances, each seeing a fragment. We run
two collector tiers: a first tier with the loadbalancing exporter, which routes spans by trace ID to a second
tier that does the tail sampling:
# tier 1
exporters:
loadbalancing:
routing_key: traceID
protocol:
otlp: { tls: { insecure: false } }
resolver:
k8s: { service: otel-sampler.observability }
3. Consistent SDK configuration everywhere
Since sampling now happens in the collector, every service exports everything:
OTEL_TRACES_SAMPLER: parentbased_always_on
This is set in our shared deployment template, not per service, so it cannot drift. The legacy fraud-check service was migrated to the OpenTelemetry SDK.
4. Cost
Tail sampling moves cost from the tracing backend to the collector. Exporting 100 percent of spans to the collector increased network traffic inside the cluster and needed memory for buffering: about 200,000 traces in flight at our volume, which fit in 6 GB across the sampling tier. The backend received about 1.6 percent of traces (1 percent baseline plus errors and slow requests), so backend cost went up only slightly.
1. Inject a known failure. In staging, we configured the payments service's fault injection to fail 0.2 percent of requests with a specific error code, and ran a load test with 500,000 checkouts:
| Sampling | Failing requests | Failing traces in backend | Complete (with payments spans) |
|---|---|---|---|
| Head 1%, inconsistent | 1,004 | 9 | 1 |
Head 1%, consistent parentbased |
1,004 | 11 | 11 |
| Tail sampling | 1,004 | 1,004 | 1,004 |
2. Find the real bug. With tail sampling deployed to production, the next day's search returned thousands of complete failing traces. They all showed the payments service's call to the card network gateway failing on connection reuse: an idle keep-alive connection closed by the gateway after 60 seconds, reused by our HTTP client whose idle timeout was 90 seconds. Lowering the client's idle timeout to 50 seconds fixed it, and the error rate dropped to zero.
3. Collector health. We monitor the tail sampling processor's metrics, especially
otelcol_processor_tail_sampling_sampling_trace_dropped_too_early, which counts traces evicted before a decision
because the buffer was full. It stays at zero at current traffic; it is the signal to add capacity.
4. Security implications. Keeping every error trace means keeping more span attributes, some of which can contain user data. We added an attribute processor in the collector that removes or hashes known sensitive attributes (email, card BIN, IP) before export, so the increase in kept traces does not increase personal data in the backend.
Takeaways
- Head sampling decides before the outcome is known, so it keeps errors at the same low rate as everything else.
- Inconsistent sampling across services produces traces with missing spans; use
parentbasedor sample centrally. - Tail sampling in the collector keeps every error and slow trace plus a baseline of normal ones.
- Route spans by trace ID so one collector sees the whole trace, and size the buffer for your decision window.
References: OpenTelemetry sampling concepts, Tail sampling processor, Load balancing exporter, OpenTelemetry SDK environment variables.
Add a label
Embed this post
<iframe src="https://hibug.me/head-sampling-dropped-error-traces" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I lead a small team responsible for observability and quality tooling, and I spend a fair share of my time on application security reviews. That mix means I see many incidents twice: once when they happen and again when we ask why nothing warned us. The posts here come from that second look. An alert that never fired because the metric disappeared, sampling that dropped exactly the traces we needed, a catch-all that reported success for weeks, a secret recoverable from an image layer, tests that passed against mocks while production failed. Each one covers how the gap was found, what we changed in code or configuration, and how we verified the fix with a test or a drill. Security topics stay defensive and practical: controls you can add to your own code and a way to test each one.
Related Stories
A network partition made both nodes of our active-passive pair act as primary
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.