Video in progress

Strong Middle 🧩 Microservices

Why is 100% sampling expensive in tracing, and how do head-based and tail-based sampling differ?

Creating, sending and storing spans for every request costs network, CPU and storage, and at a high RPS that's a lot of money. Head-based sampling makes the decision randomly at the start of the trace; tail-based sampling waits until the trace finishes and keeps the ones with errors or that were slow.

📖 Answer

Do the math: 5,000 RPS, 15 spans per request, ~1 KB each. That's 75 MB per second, terabytes of data a day. You pay for the collector, the backend (Tempo, Jaeger + Elasticsearch) and network traffic, and the application creates an object for every span.

Head-based sampling. The first service (or the gateway) randomly decides "is this trace recorded?" and passes it to the following ones with the sampled flag in traceparent. Every service follows that decision, so a trace is either complete or absent.

management:
  tracing:
    sampling:
      probability: 0.05

The problem: the decision is made at the start of the request, when the outcome isn't known yet. Most of the failing requests, which happen in 0.05% of cases, won't be recorded, and those are exactly the traces you need.

Tail-based sampling. Applications send all spans to an OpenTelemetry Collector. The collector waits a little for the trace to finish and decides by rules: has an error, keep it; slower than 2 seconds, keep it; of the rest, 1%. The cost: the collector holds whole traces in memory, and all the spans of a trace must go to the same collector instance (load balancing by traceId).

In practice they're combined: a higher head sampling rate (or 100%) in the application, and tail sampling in the collector. Metrics and logs stay unsampled; traces are only for deep analysis.