pub fn setup_with_sampler<S: Fn(&SpanCtxt) -> bool + Send + Sync + 'static>(
sampler: S,
) -> Setup<DefaultEmitter, TraceparentFilter<S>, TraceparentCtxt<DefaultCtxt>>
Expand description
Start a builder for a distributed-trace-aware pipeline with a sampler.
The sampler will be called once for each trace when it’s started.
If the sampler returns true
, the trace will be emitted.
If the sampler returns false
, the trace will be discarded.
use std::sync::atomic::{AtomicUsize, Ordering};
fn main() {
let rt = emit_traceparent::setup_with_sampler({
let counter = AtomicUsize::new(0);
move |_| {
// Sample 1 in every 10 traces
counter.fetch_add(1, Ordering::Relaxed) % 10 == 0
}
})
.emit_to(emit_term::stdout())
.init();
// Your app code goes here
rt.blocking_flush(std::time::Duration::from_secs(30));
}
Note that other events emitted within an unsampled trace will still be emitted.
You can add the in_sampled_trace_filter
filter to filter out events in unsampled traces:
use std::sync::atomic::{AtomicUsize, Ordering};
fn main() {
let rt = emit_traceparent::setup_with_sampler({
let counter = AtomicUsize::new(0);
move |_| {
// Sample 1 in every 10 traces
counter.fetch_add(1, Ordering::Relaxed) % 10 == 0
}
})
// The `true` here tells us to include events outside traces
// If we pass `false` then any event outside a trace will be discarded
.and_emit_when(emit_traceparent::in_sampled_trace_filter(true))
.emit_to(emit_term::stdout())
.init();
// Your app code goes here
rt.blocking_flush(std::time::Duration::from_secs(30));
}
The true
parameter passed to in_sampled_trace_filter
here includes events that aren’t in any trace.
Passing false
will also filter out events that aren’t part of any trace.