Skip to main content

tracing_prof/
sampler.rs

1//! Customization of sampling behavior.
2
3use tracing_subscriber::registry::{LookupSpan, SpanRef};
4
5#[cfg(feature = "sampler-probabilistic")]
6pub mod probabilistic;
7
8/// A trait that allows customizing which spans are sampled.
9pub trait HeadSampler {
10    /// Whether sampling is enabled.
11    ///
12    /// This serves as a compile-time optimization to
13    /// skip sampling logic when not needed.
14    const ENABLED: bool = true;
15
16    /// Determines whether the span and its children should be sampled.
17    fn should_sample<S>(&self, span: &SpanRef<S>) -> bool
18    where
19        S: for<'a> LookupSpan<'a>;
20}
21
22/// A default sampler that samples all spans.
23#[derive(Debug, Clone, Copy)]
24pub struct AlwaysSample;
25
26impl HeadSampler for AlwaysSample {
27    const ENABLED: bool = false;
28
29    #[inline]
30    fn should_sample<S>(&self, _span: &SpanRef<S>) -> bool
31    where
32        S: for<'a> LookupSpan<'a>,
33    {
34        true
35    }
36}