use crate::sampler::Sampler;
use crate::span::{SharedSpanConsumer, SpanConsumer, SpanReceiver, StartSpanOptions};
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::mpsc;
#[derive(Debug)]
pub struct Tracer<S, T> {
sampler: Arc<S>,
span_tx: SharedSpanConsumer<T>,
}
impl<S: Sampler<T>, T: Send + 'static> Tracer<S, T> {
pub fn new(sampler: S) -> (Self, SpanReceiver<T>) {
let (span_tx, span_rx) = mpsc::unbounded_channel();
(Self::with_consumer(sampler, span_tx), span_rx)
}
}
impl<S: Sampler<T>, T> Tracer<S, T> {
pub fn with_consumer<C: SpanConsumer<T> + 'static>(sampler: S, consumer: C) -> Self {
Self {
sampler: Arc::new(sampler),
span_tx: SharedSpanConsumer::new(consumer),
}
}
pub fn span<N>(&self, operation_name: N) -> StartSpanOptions<'_, S, T>
where
N: Into<Cow<'static, str>>,
{
StartSpanOptions::new(operation_name, &self.span_tx, &self.sampler)
}
}
impl<S, T> Tracer<S, T> {
pub fn clone_with_sampler<U: Sampler<T>>(&self, sampler: U) -> Tracer<U, T> {
Tracer {
sampler: Arc::new(sampler),
span_tx: self.span_tx.clone(),
}
}
}
impl<S, T> Clone for Tracer<S, T> {
fn clone(&self) -> Self {
Tracer {
sampler: Arc::clone(&self.sampler),
span_tx: self.span_tx.clone(),
}
}
}