rustracing_jaeger/
tracer.rs

1use rustracing::sampler::{BoxSampler, Sampler};
2use rustracing::Tracer as InnerTracer;
3use std::borrow::Cow;
4use std::fmt;
5
6use crate::span::{SpanContextState, SpanReceiver, SpanSender, StartSpanOptions};
7
8/// Tracer.
9#[derive(Clone)]
10pub struct Tracer {
11    inner: InnerTracer<BoxSampler<SpanContextState>, SpanContextState>,
12}
13impl Tracer {
14    /// Makes a new `Tracer` instance with an unbounded channel.
15    ///
16    /// This constructor is mainly for backward compatibility, it has the same interface
17    /// as in previous versions except the type of `SpanReceiver`.
18    /// It builds an unbounded channel which may cause memory issues if there is no reader,
19    /// prefer `with_sender()` alternative with a bounded one.
20    pub fn new<S>(sampler: S) -> (Self, SpanReceiver)
21    where
22        S: Sampler<SpanContextState> + Send + Sync + 'static,
23    {
24        let (inner, rx) = InnerTracer::new(sampler.boxed());
25        (Tracer { inner }, rx)
26    }
27
28    /// Makes a new `Tracer` instance.
29    pub fn with_sender<S>(sampler: S, span_tx: SpanSender) -> Self
30    where
31        S: Sampler<SpanContextState> + Send + Sync + 'static,
32    {
33        let inner = InnerTracer::with_sender(sampler.boxed(), span_tx);
34        Tracer { inner }
35    }
36
37    /// Clone with the given `sampler`.
38    pub fn clone_with_sampler<T>(&self, sampler: T) -> Self
39    where
40        T: Sampler<SpanContextState> + Send + Sync + 'static,
41    {
42        let inner = self.inner.clone_with_sampler(sampler.boxed());
43        Tracer { inner }
44    }
45
46    /// Returns `StartSpanOptions` for starting a span which has the name `operation_name`.
47    pub fn span<N>(&self, operation_name: N) -> StartSpanOptions
48    where
49        N: Into<Cow<'static, str>>,
50    {
51        self.inner.span(operation_name)
52    }
53}
54impl fmt::Debug for Tracer {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        write!(f, "Tracer {{ .. }}")
57    }
58}
59
60#[cfg(test)]
61mod test {
62    use rustracing::sampler::NullSampler;
63
64    use super::*;
65
66    #[test]
67    fn is_tracer_sendable() {
68        fn is_send<T: Send>(_: T) {}
69
70        let (span_tx, _span_rx) = crossbeam_channel::bounded(10);
71        let tracer = Tracer::with_sender(NullSampler, span_tx);
72        is_send(tracer);
73    }
74}