Configure Sampling (Head vs Tail)
mainSampling controls which traces are stored to manage costs and volume.
Head Sampling
Performed at the start of a trace. It signals to downstream systems whether a trace should be sampled.
- Configuration: Use a standard OpenTelemetry
Sampleror an object withratio(0 to 1) andacceptRemote(boolean). - Default:
AlwaysOnSampler(samples everything).
Tail Sampling
Performed at the end of a trace. This allows you to capture traces based on their outcome (e.g., only if an error occurred), even if they weren't head-sampled.
- Default: Samples traces that were head-sampled OR if the local root span is marked as an error.
// Head Sampling configuration object
const headSampler = {
acceptRemote: false,
ratio: 0.5 // Samples 50% of requests
}
// Tail Sampling logic example
const tailSampler = (traceInfo: LocalTrace): boolean => {
const localRootSpan = traceInfo.localRootSpan as unknown as ReadableSpan
return (localRootSpan.spanContext().traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED
}