1use crate::sampler::Sampler;
2use crate::span::{SharedSpanConsumer, SpanConsumer, SpanReceiver, StartSpanOptions};
3use std::borrow::Cow;
4use std::sync::Arc;
5use tokio::sync::mpsc;
6
7#[derive(Debug)]
26pub struct Tracer<S, T> {
27 sampler: Arc<S>,
28 span_tx: SharedSpanConsumer<T>,
29}
30impl<S: Sampler<T>, T: Send + 'static> Tracer<S, T> {
31 pub fn new(sampler: S) -> (Self, SpanReceiver<T>) {
33 let (span_tx, span_rx) = mpsc::unbounded_channel();
34 (Self::with_consumer(sampler, span_tx), span_rx)
35 }
36}
37impl<S: Sampler<T>, T> Tracer<S, T> {
38 pub fn with_consumer<C: SpanConsumer<T> + 'static>(sampler: S, consumer: C) -> Self {
40 Self {
41 sampler: Arc::new(sampler),
42 span_tx: SharedSpanConsumer::new(consumer),
43 }
44 }
45
46 pub fn span<N>(&self, operation_name: N) -> StartSpanOptions<'_, S, T>
48 where
49 N: Into<Cow<'static, str>>,
50 {
51 StartSpanOptions::new(operation_name, &self.span_tx, &self.sampler)
52 }
53}
54impl<S, T> Tracer<S, T> {
55 pub fn clone_with_sampler<U: Sampler<T>>(&self, sampler: U) -> Tracer<U, T> {
57 Tracer {
58 sampler: Arc::new(sampler),
59 span_tx: self.span_tx.clone(),
60 }
61 }
62}
63impl<S, T> Clone for Tracer<S, T> {
64 fn clone(&self) -> Self {
65 Tracer {
66 sampler: Arc::clone(&self.sampler),
67 span_tx: self.span_tx.clone(),
68 }
69 }
70}