Start free trial
Start free trial Log in

14-day free trial. No credit card.

Head-based vs tail-based trace sampling

  • Glossary
  • 3 minute read

What's the difference between head-based and tail-based sampling?

Head-based sampling decides to keep a trace before the trace completes, usually a random % per trace ID. Cheap, simple, but you throw away traces without knowing whether they were interesting. Tail-based sampling buffers the whole trace and decides after it finishes, so you can keep every error and every slow trace while sampling normal ones. Smarter but operationally heavier.

Updated

Sampling is the unglamorous decision that quietly determines whether your tracing bill is $60 or $6,000 a month, and whether the one trace you need during an incident is actually there. The two strategies (deciding before the request finishes or after) sound like an implementation detail and aren't, because one of them can keep every error trace and the other can't.

Why sample at all

A fully-instrumented request generates 5-20 spans. At 1,000 req/s (86.4 million requests a day) that's roughly 430 million to 1.7 billion spans a day. At about 1 KB per span that's roughly 430 GB to 1.7 TB a day, before retention. Storage adds up quickly. Sampling trades data volume for cost.

Head-based sampling

Decide at the root span. A deterministic hash of the trace ID lets distributed services agree on the same decision: a trace either lives or dies across every service it touches.

OpenTelemetry head sampler (PHP)
use OpenTelemetry\SDK\Trace\Sampler\TraceIdRatioBasedSampler;
use OpenTelemetry\SDK\Trace\TracerProvider;

$sampler = new TraceIdRatioBasedSampler(0.1); // keep 10%

$tracerProvider = TracerProvider::builder()
    ->setSampler($sampler)
    ->build();

Pros: cheap (no buffering), works cross-service, little operational complexity. Cons: you throw away traces before knowing whether they were errors or slow. At a 10% rate you drop 90% of all traces, and that includes about 90% of your errors and slow requests.

Tail-based sampling

Buffer every span for a fixed window, keyed by trace ID (decision_wait, 30 seconds by default). The Collector has no signal that a trace is finished, so when the window ends it applies the rules to whatever spans have arrived.

OTel Collector tail sampling config
processors:
  tail_sampling:
    decision_wait: 30s
    num_traces: 50000
    policies:
      - name: errors-always
        type: status_code
        status_code:
          status_codes: [ERROR]
      - name: slow-always
        type: latency
        latency:
          threshold_ms: 1000
      - name: sample-normal
        type: probabilistic
        probabilistic:
          sampling_percentage: 10

Pros: keep all errors, keep all slow traces, sample the rest. Much better data quality per dollar. Cons: you need Collectors with enough memory to buffer all in-flight traces, and every span of a trace has to reach the same Collector instance, which in a cluster means a load-balancing tier that routes by trace ID.

Hybrid approach

Many production systems combine both: head-sample at 100% (keep everything) through a Collector that then applies tail rules. The head-sampler stops being a sampler and becomes a pass-through; the tail rules do the actual filtering.

Which one to actually pick

The honest decision tree is short. Under a few hundred requests a second, keep everything. Sampling solves a problem you don't have yet, and the full dataset is worth more than the savings. Above that, sample the routine traffic but keep every error and every slow request. Only tail rules can do that, because a head sampler decides before it knows whether the request will fail. At very high volume, buffering every span in a Collector gets expensive too, and teams put a head sampler in front of the tail rules, accepting that it drops some errors. Most Laravel apps stay in the first two bands their whole life.

The NightOwl approach

NightOwl doesn't sample by default. It writes to your own PostgreSQL, so keeping everything costs database storage rather than a per-event fee, and the full dataset matters for debugging rare issues. If storage cost starts to matter, sampling happens in the Nightwatch config in your app, not in the NightOwl agent: the nightwatch.sampling.requests key, set with NIGHTWATCH_REQUEST_SAMPLE_RATE (commands, exceptions and scheduled tasks have their own rates).

Keep every request from your Laravel app

NightOwl has no event quota and keeps every request by default, because the data goes into your own PostgreSQL. If you ever want to sample, you set the rate in your app's Nightwatch config.

No credit card needed.

Frequently asked questions

Anything else, write to support@usenightowl.com

What's the difference between head-based and tail-based trace sampling?

Head-based sampling decides to keep a trace at the moment the root span starts, before the trace is complete. Tail-based sampling buffers the whole trace and decides after it finishes. Head is cheap and fast; tail is smarter because you can keep all error traces and all slow traces and drop the boring ones.

Why do I need to sample traces at all?

Cost. At 1,000 req/s with about 10 spans per request, keeping every trace means roughly 864 million spans a day. At about 1 KB per span that's roughly 860 GB of trace data per day. Keep 14 days and you're holding about 12 TB, which at an illustrative $0.50 per GB-month is around $6,000 a month just for traces. Sampling at 10% cuts it to about $600, and 1% to about $60. Add tail rules that always keep errors and slow requests, and those traces survive even at 1%.

How do I configure head-based sampling in Laravel?

In OpenTelemetry SDK config, set TraceIdRatioBasedSampler with a ratio (0.1 = 10%, 0.01 = 1%). The SDK will deterministically keep or drop based on the trace ID's low 56 bits, so all services with the same sampler config agree on the same decision, keeping cross-service trace consistency.

What's the right sampling rate?

It depends on traffic and budget. Up to a few hundred requests a second, keep everything. Above that, keep every error and slow request with tail rules and sample around 10% of the rest. Tail rules only see traces that reach the Collector, so a head sampler in front of them would drop errors too. At very high traffic some teams accept that trade and head-sample before the Collector to cap its load.

How does NightOwl handle sampling?

NightOwl stores every request by default (no sampling) because it writes to your own PostgreSQL, where storage costs what your database costs rather than a per-event price. If volume gets expensive, lower the sample rate in the Nightwatch config in your app (NIGHTWATCH_REQUEST_SAMPLE_RATE, with separate rates for commands, exceptions and scheduled tasks). The NightOwl agent itself doesn't sample. This is the opposite tradeoff from cloud APMs, where per-event cost forces sampling earlier.

Can I use tail-based sampling with OpenTelemetry?

Yes, via the tail_sampling processor in the OTel Collector's contrib distribution. You run a Collector cluster that buffers spans per trace ID for a fixed window (decision_wait, 30 seconds by default) then decides what to keep based on rules (always keep errors, keep everything slower than one second, sample 10% of the rest). More operational complexity than head sampling, but much better data per dollar.

Keep reading

Flat pricing, whatever your traffic does

Every plan starts with a 14-day free trial. No credit card needed.

Hobby
$5 a month

One app for a side project. Issues and alerts, with a 14-day dashboard window.

Start my 14-day trial
TeamRecommended
$15 a month

Up to 3 apps and 5 people per team. Full history, the MCP server, data clearing and agent health.

Start my 14-day trial
Agency
$69 a month

Unlimited client apps and people, one overview of every app, and apps you can hand over.

Start my 14-day trial