use crate::TraceId;
use std::sync::Arc;
pub trait Sample {
fn sample(&self, trace_id: TraceId) -> bool;
}
impl<T> Sample for Arc<T>
where
T: ?Sized + Sample,
{
fn sample(&self, trace_id: TraceId) -> bool {
(**self).sample(trace_id)
}
}
impl<T> Sample for Box<T>
where
T: ?Sized + Sample,
{
fn sample(&self, trace_id: TraceId) -> bool {
(**self).sample(trace_id)
}
}
pub struct AlwaysSampler;
impl Sample for AlwaysSampler {
fn sample(&self, _: TraceId) -> bool {
true
}
}
pub struct NeverSampler;
impl Sample for NeverSampler {
fn sample(&self, _: TraceId) -> bool {
false
}
}
pub struct RandomSampler {
rate: f32,
}
impl RandomSampler {
pub fn new(rate: f32) -> RandomSampler {
assert!(rate >= 0. && rate <= 1.);
RandomSampler { rate }
}
}
impl Sample for RandomSampler {
fn sample(&self, _: TraceId) -> bool {
rand::random::<f32>() < self.rate
}
}