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
use crossbeam_channel::unbounded;

use opentracingrust::ExtractFormat;
use opentracingrust::ImplContextBox;
use opentracingrust::InjectFormat;
use opentracingrust::Result;
use opentracingrust::Span;
use opentracingrust::SpanContext;
use opentracingrust::SpanReceiver;
use opentracingrust::SpanSender;
use opentracingrust::StartOptions;
use opentracingrust::Tracer;
use opentracingrust::TracerInterface;

mod context;
mod error;
mod extract;
mod inject;
mod trace_id;

pub use self::context::ZipkinContext;
pub use self::context::ZipkinContextOptions;

/// A Zipkin backed OpenTracingRust tracer.
///
/// Use a collector to send the finished spans to Zipkin.
///
/// Sampling policy:
///
///   * Any span inherits the sampling state from its references.
///   * Root spans are always sampled: currently sampling policies are not supported.
pub struct ZipkinTracer {
    sender: SpanSender,
}

impl ZipkinTracer {
    /// Creates a new zipkin tracer.
    pub fn new() -> (Tracer, SpanReceiver) {
        let (sender, receiver) = unbounded();
        let tracer = Tracer::new(ZipkinTracer { sender });
        (tracer, receiver)
    }
}

impl TracerInterface for ZipkinTracer {
    fn extract(&self, fmt: ExtractFormat) -> Result<Option<SpanContext>> {
        match fmt {
            ExtractFormat::Binary(carrier) => extract::binary(carrier),
            ExtractFormat::HttpHeaders(carrier) => extract::http_headers(carrier),
            ExtractFormat::TextMap(carrier) => extract::http_headers(carrier),
        }
    }

    fn inject(&self, context: &SpanContext, fmt: InjectFormat) -> Result<()> {
        match fmt {
            InjectFormat::Binary(carrier) => inject::binary(context, carrier),
            InjectFormat::HttpHeaders(carrier) => inject::http_headers(context, carrier),
            InjectFormat::TextMap(carrier) => inject::http_headers(context, carrier),
        }
    }

    fn span(&self, name: &str, options: StartOptions) -> Span {
        let context = ZipkinContext::new();
        let context = SpanContext::new(ImplContextBox::new(context));
        Span::new(name, context, options, self.sender.clone())
    }
}