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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! # OpenTelemetry Jaeger Exporter
//!
//! This exporter currently delegates to the [rustracing_jaeger library]
//! which implements the [OpenTracing API].
//!
//! [rustracing_jaeger library]: https://github.com/sile/rustracing_jaeger
//! [OpenTracing API]: https://opentracing.io/
use crate::exporter::trace;
use crate::{api, sdk};
use std::any;
use std::net;
use std::str::FromStr;
use std::thread;

pub use rustracing::{
    sampler::{AllSampler, NullSampler, ProbabilisticSampler},
    tag::{Tag, TagValue},
};
pub use rustracing_jaeger::{
    reporter,
    span::{SpanContext, SpanSender},
    Span, Tracer,
};

/// Default service name no service is configured.
static DEFAULT_SERVICE_NAME: &str = "OpenTelemetry";
static DEFAULT_COLLECTOR_ENDPOINT: &str = "127.0.0.1:6831";

/// Jaeger exporter
#[derive(Debug)]
pub struct Exporter {
    collector_endpoint: net::SocketAddr,
    process: Process,
    pub(crate) span_sender: SpanSender,
}

impl Exporter {
    /// Create a new exporter builder.
    pub fn builder() -> Builder {
        Builder::default()
    }

    /// Default `Exporter` with initialized sender.
    pub fn init_default() -> Self {
        Exporter::builder().init()
    }
}

impl trace::SpanExporter for Exporter {
    type Span = sdk::Span;

    /// Ignored because spans export themselves on drop currently.
    fn export(&self, _batch: Vec<Self::Span>) -> Result<(), ()> {
        Ok(())
    }

    /// Ignored for now.
    fn shutdown(&self) {}

    /// Allows `Exporter` to be downcast from trait object.
    fn as_any(&self) -> &dyn any::Any {
        self
    }
}

/// Jaeger process configuration
#[derive(Debug)]
pub struct Process {
    /// The name of the traced service that all spans will be reported as belonging to.
    pub service_name: &'static str,
    /// Metadata about the service that will appear in all `Span`s.
    pub tags: Vec<api::KeyValue>,
}

impl Default for Process {
    /// Default `Process` config
    fn default() -> Self {
        Process {
            service_name: DEFAULT_SERVICE_NAME,
            tags: Default::default(),
        }
    }
}

/// Jaeger Exporter Builder
#[derive(Debug)]
pub struct Builder {
    collector_endpoint: net::SocketAddr,
    process: Process,
}

impl Default for Builder {
    fn default() -> Self {
        Builder {
            collector_endpoint: DEFAULT_COLLECTOR_ENDPOINT.parse().unwrap(),
            process: Default::default(),
        }
    }
}

impl Builder {
    /// Assign the collector endpoint. Uses `DEFAULT_COLLECTOR_ENDPOINT` if unset.
    pub fn with_collector_endpoint(self, collector_endpoint: net::SocketAddr) -> Self {
        Builder {
            collector_endpoint,
            ..self
        }
    }

    /// Assign the exporter process config.
    pub fn with_process(self, process: Process) -> Self {
        Builder { process, ..self }
    }

    /// Create a new exporter from the builder
    pub fn init(self) -> Exporter {
        let Builder {
            collector_endpoint,
            process: Process { service_name, tags },
        } = self;
        let reporter_tags = tags.clone();
        let (span_tx, span_rx) = crossbeam_channel::bounded(10);

        // Spin up thread to report finished spans
        let _ = thread::Builder::new()
            .name("Jaeger span reporter".to_string())
            .spawn(move || {
                let mut reporter = reporter::JaegerCompactReporter::new(service_name)
                    .expect("Can't initialize jaeger reporter");

                reporter
                    .set_agent_addr(collector_endpoint)
                    .expect("Can't set socket jaeger reporter socket");
                for tag in reporter_tags {
                    let api::KeyValue { key, value } = tag;
                    let value = match value {
                        api::Value::Bool(b) => TagValue::Boolean(b),
                        api::Value::I64(i) => TagValue::Integer(i),
                        api::Value::F64(float) => TagValue::Float(float),
                        v => TagValue::String(v.into()),
                    };
                    reporter.add_service_tag(Tag::new(key, value))
                }
                for span in span_rx {
                    let _ = reporter.report(&[span]);
                }
            });

        Exporter {
            collector_endpoint,
            process: Process { service_name, tags },
            span_sender: span_tx,
        }
    }
}

impl From<api::SpanContext> for rustracing_jaeger::span::SpanContext {
    /// Convert from `api::SpanContext` instances to `rustracing_jaeger`'s `SpanContext` type.
    fn from(context: api::SpanContext) -> Self {
        let jaeger_trace_str = format!(
            "{:x}:{:x}:0:{:x}",
            context.trace_id(),
            context.span_id(),
            context.trace_flags()
        );
        let span_context_state =
            rustracing_jaeger::span::SpanContextState::from_str(&jaeger_trace_str)
                .expect("should always parse");

        rustracing::span::SpanContext::new(span_context_state, Vec::new())
    }
}