use crate::span::CandidateSpan;
use crate::{ErrorKind, Result};
use rand::{self, Rng};
pub trait Sampler<T> {
fn is_sampled(&self, span: &CandidateSpan<T>) -> bool;
fn or<U>(self, other: U) -> OrSampler<Self, U>
where
Self: Sized,
U: Sampler<T>,
{
OrSampler(self, other)
}
fn and<U>(self, other: U) -> AndSampler<Self, U>
where
Self: Sized,
U: Sampler<T>,
{
AndSampler(self, other)
}
fn boxed(self) -> BoxSampler<T>
where
Self: Sized + Send + Sync + 'static,
{
Box::new(self)
}
}
impl<T> Sampler<T> for BoxSampler<T> {
fn is_sampled(&self, span: &CandidateSpan<T>) -> bool {
(**self).is_sampled(span)
}
fn boxed(self) -> BoxSampler<T>
where
Self: Sized + Send + 'static,
{
self
}
}
pub type BoxSampler<T> = Box<dyn Sampler<T> + Send + Sync + 'static>;
#[derive(Debug, Clone)]
pub struct ProbabilisticSampler {
sampling_rate: f64,
}
impl ProbabilisticSampler {
pub fn new(sampling_rate: f64) -> Result<Self> {
track_assert!(0.0 <= sampling_rate, ErrorKind::InvalidInput);
track_assert!(sampling_rate <= 1.0, ErrorKind::InvalidInput);
Ok(ProbabilisticSampler { sampling_rate })
}
}
impl<T> Sampler<T> for ProbabilisticSampler {
fn is_sampled(&self, _span: &CandidateSpan<T>) -> bool {
rand::thread_rng().gen_range(0.0..1.0) < self.sampling_rate
}
}
#[derive(Debug, Clone)]
pub struct PassiveSampler;
impl<T> Sampler<T> for PassiveSampler {
fn is_sampled(&self, span: &CandidateSpan<T>) -> bool {
!span.references().is_empty()
}
}
#[derive(Debug, Clone)]
pub struct NullSampler;
impl<T> Sampler<T> for NullSampler {
fn is_sampled(&self, _span: &CandidateSpan<T>) -> bool {
false
}
}
#[derive(Debug, Clone)]
pub struct AllSampler;
impl<T> Sampler<T> for AllSampler {
fn is_sampled(&self, _span: &CandidateSpan<T>) -> bool {
true
}
}
#[derive(Debug, Clone)]
pub struct OrSampler<A, B>(A, B);
impl<A, B, T> Sampler<T> for OrSampler<A, B>
where
A: Sampler<T>,
B: Sampler<T>,
{
fn is_sampled(&self, span: &CandidateSpan<T>) -> bool {
self.0.is_sampled(span) || self.1.is_sampled(span)
}
}
#[derive(Debug, Clone)]
pub struct AndSampler<A, B>(A, B);
impl<A, B, T> Sampler<T> for AndSampler<A, B>
where
A: Sampler<T>,
B: Sampler<T>,
{
fn is_sampled(&self, span: &CandidateSpan<T>) -> bool {
self.0.is_sampled(span) && self.1.is_sampled(span)
}
}