Video in progress

What is the difference between head-based and tail-based sampling in tracing?

In head-based sampling, the decision is made at the start of the request (for example, 10% at random): simple and cheap, but rare traces with errors may be dropped. In tail-based sampling the decision is made once the trace is complete: error and slow traces are kept, but this requires buffering the whole trace in the Collector.

📖 Answer

Tracing and storing every request is expensive: 5000 requests per second with 20 spans each is billions of spans a day. That's why sampling is used.

Head-based (the default in Spring Boot):

management:
  tracing:
    sampling:
      probability: 0.05

The first service flips a coin, and the decision spreads along the whole chain through the sampled flag in traceparent. Every service makes the same decision and the trace is stored whole.

The problem: 1 request in 1000 ended with an error. With 5% sampling, the chance that its trace is stored is 5%. The very trace you need is missing.

Tail-based, with the tail_sampling processor in the OpenTelemetry Collector:

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: 2 }

Here all error and slow traces are kept, and 2% of the rest.

The cost:

  • the Collector must gather all the spans of one trace in one place. If there are several Collectors, you put a load balancer in front that routes by traceId;
  • during decision_wait the spans sit in memory;
  • the applications still send every span, so network load doesn't go down.

In practice the two are often combined: light head sampling in the application (say 50%), and tail sampling in the Collector.