1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::sync::Arc;
use std::time::Duration;

use opentracingrust::Tracer;
use slog::Logger;

use replicante_util_upkeep::Upkeep;

mod backends;
pub mod carriers;
mod config;
mod error;

pub use self::config::Config;
pub use self::error::fail_span;
pub use self::error::Error;
pub use self::error::ErrorKind;
pub use self::error::Result;

/// Wrapper for easier optional `Tracer`s.
#[derive(Clone)]
pub struct MaybeTracer(Option<Arc<Tracer>>);

impl MaybeTracer {
    pub fn new<T>(tracer: T) -> MaybeTracer
    where
        T: Into<Option<Arc<Tracer>>>,
    {
        MaybeTracer(tracer.into())
    }

    /// Execute the block if a `Tracer` is available.
    pub fn with<B, T>(&self, block: B) -> Option<T>
    where
        B: FnOnce(&Tracer) -> T,
    {
        match self.0.as_ref() {
            None => None,
            Some(tracer) => Some(block(tracer)),
        }
    }
}

/// Additional options passed to tracer configuration.
pub struct Opts<'a> {
    flush_timeout: Duration,
    logger: Logger,
    service_name: &'a str,
    upkeep: &'a mut Upkeep,
}

impl<'a> Opts<'a> {
    pub fn new<S>(service_name: S, logger: Logger, upkeep: &'a mut Upkeep) -> Opts<'a>
    where
        S: Into<&'a str>,
    {
        Opts {
            flush_timeout: Duration::from_secs(1),
            logger,
            service_name: service_name.into(),
            upkeep,
        }
    }

    /// Set the muximum delay between span flushes.
    ///
    /// Some tracers' collectors allow this option to be set through the configuration.
    /// In that case, the value from the user configuration overrides this option.
    pub fn flush_timeout(mut self, timeout: Duration) -> Opts<'a> {
        self.flush_timeout = timeout;
        self
    }
}

/// Creates a new tracer based on the given configuration.
pub fn tracer(config: Config, opts: Opts) -> Result<Tracer> {
    match config {
        Config::Noop => self::backends::noop(opts),
        Config::Zipkin(config) => self::backends::zipkin(config, opts),
    }
}