use crate::config::TracingConfig;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SamplingDecision {
Drop,
Sample,
}
pub struct Sampler {
sample_rate: f64,
error_promote: bool,
latency_promote_threshold_ns: u64,
}
impl Sampler {
pub fn new(config: &TracingConfig) -> Self {
Self {
sample_rate: config.sample_rate,
error_promote: config.error_promote,
latency_promote_threshold_ns: config.latency_promote_threshold_ms * 1_000_000,
}
}
pub fn should_sample_head(&self, trace_id_bytes: &[u8; 16]) -> SamplingDecision {
if self.sample_rate >= 1.0 {
return SamplingDecision::Sample;
}
if self.sample_rate <= 0.0 {
return SamplingDecision::Drop;
}
let hash = u64::from_le_bytes([
trace_id_bytes[8],
trace_id_bytes[9],
trace_id_bytes[10],
trace_id_bytes[11],
trace_id_bytes[12],
trace_id_bytes[13],
trace_id_bytes[14],
trace_id_bytes[15],
]);
let threshold = (self.sample_rate * u64::MAX as f64) as u64;
if hash <= threshold {
SamplingDecision::Sample
} else {
SamplingDecision::Drop
}
}
#[inline]
pub fn should_promote_error(&self) -> bool {
self.error_promote
}
#[inline]
pub fn should_promote_latency(&self, latency_ns: u64) -> bool {
self.latency_promote_threshold_ns > 0 && latency_ns > self.latency_promote_threshold_ns
}
}
#[cfg(test)]
mod tests {
use super::*;
fn default_config() -> TracingConfig {
TracingConfig::default()
}
#[test]
fn test_sampler_rate_zero_drops_all() {
let mut config = default_config();
config.sample_rate = 0.0;
let sampler = Sampler::new(&config);
for i in 0u8..100 {
let trace_id = [i; 16];
assert_eq!(
sampler.should_sample_head(&trace_id),
SamplingDecision::Drop
);
}
}
#[test]
fn test_sampler_rate_one_samples_all() {
let mut config = default_config();
config.sample_rate = 1.0;
let sampler = Sampler::new(&config);
for i in 0u8..100 {
let trace_id = [i; 16];
assert_eq!(
sampler.should_sample_head(&trace_id),
SamplingDecision::Sample
);
}
}
#[test]
fn test_sampler_partial_rate() {
let mut config = default_config();
config.sample_rate = 0.5;
let sampler = Sampler::new(&config);
let mut sampled = 0;
let total = 10000;
for i in 0u64..total {
let val = i.wrapping_mul(0x9E37_79B9_7F4A_7C15);
let trace_id = val.to_le_bytes();
let mut full = [0u8; 16];
full[8..].copy_from_slice(&trace_id);
if sampler.should_sample_head(&full) == SamplingDecision::Sample {
sampled += 1;
}
}
assert!(
sampled > 3000 && sampled < 7000,
"sampled {sampled} out of {total}"
);
}
#[test]
fn test_promote_error() {
let config = default_config(); let sampler = Sampler::new(&config);
assert!(sampler.should_promote_error());
}
#[test]
fn test_promote_error_disabled() {
let mut config = default_config();
config.error_promote = false;
let sampler = Sampler::new(&config);
assert!(!sampler.should_promote_error());
}
#[test]
fn test_promote_latency() {
let config = default_config(); let sampler = Sampler::new(&config);
assert!(!sampler.should_promote_latency(5_000_000)); assert!(sampler.should_promote_latency(15_000_000)); }
#[test]
fn test_promote_latency_zero_threshold() {
let mut config = default_config();
config.latency_promote_threshold_ms = 0;
let sampler = Sampler::new(&config);
assert!(!sampler.should_promote_latency(100_000_000));
}
}