Control which traces are collected using sampling functions in tctx
Sampling determines whether a trace should be collected and recorded by your tracing system. The sampled flag is a single bit in the traceparent flags field that controls this behavior.
Use is_sampled() to check if a trace is currently being sampled:
import * as traceparent from 'tctx/traceparent';const parent = traceparent.parse(request.headers.traceparent);if (traceparent.is_sampled(parent)) { // Trace is being recorded console.log('This trace will be collected');} else { // Trace is not being recorded console.log('This trace will be dropped');}
According to the W3C specification, you should create a child span (using .child()) before changing the sampling decision to properly reflect the decision point in the trace hierarchy.
Use unsample() to disable sampling on a traceparent:
import * as traceparent from 'tctx/traceparent';const parent = traceparent.parse(request.headers.traceparent);// Decide not to sample this tracetraceparent.unsample(parent);fetch('/downstream', { headers: { traceparent: parent.child(), },});
This clears the sampled flag bit using bitwise AND with NOT: