tracing_jaeger/
lib.rs

1// #![deny(
2//     warnings,
3//     missing_debug_implementations,
4//     missing_copy_implementations,
5//     missing_docs
6// )]
7
8//! This crate provides:
9//! - A tracing layer, `TelemetryLayer`, that can be used to publish trace data to honeycomb.io
10//! - Utilities for implementing distributed tracing against the honeycomb.io backend
11//!
12//! As a tracing layer, `TelemetryLayer` can be composed with other layers to provide stdout logging, filtering, etc.
13
14mod opentelemetry;
15mod visitor;
16
17pub use crate::opentelemetry::OpenTelemetry;
18pub use crate::visitor::OpenTelemetryVisitor;
19pub use ::opentelemetry::api::trace::span_context::{SpanId, TraceId};
20use ::opentelemetry::exporter::trace::SpanExporter;
21use ::opentelemetry::sdk::Config;
22use rand::Rng;
23use std::collections::HashMap;
24use std::sync::Mutex;
25#[doc(no_inline)]
26pub use tracing_distributed::{TelemetryLayer, TraceCtxError};
27
28/// Register the current span as the local root of a distributed trace.
29///
30/// Specialized to the opentelemetry-specific SpanId and TraceId provided by this crate.
31pub fn register_dist_tracing_root(
32    trace_id: TraceId,
33    remote_parent_span: Option<SpanId>,
34) -> Result<(), TraceCtxError> {
35    tracing_distributed::register_dist_tracing_root(trace_id, remote_parent_span)
36}
37
38/// Retrieve the distributed trace context associated with the current span.
39///
40/// Returns the `TraceId`, if any, that the current span is associated with along with
41/// the `SpanId` belonging to the current span.
42///
43/// Specialized to the opentelemetry-specific SpanId and TraceId provided by this crate.
44pub fn current_dist_trace_ctx() -> Result<(TraceId, SpanId), TraceCtxError> {
45    tracing_distributed::current_dist_trace_ctx()
46}
47
48/// Construct a TelemetryLayer that does not publish telemetry to any backend.
49///
50/// Specialized to the opentelemetry-specific SpanId and TraceId provided by this crate.
51pub fn new_blackhole_telemetry_layer(
52) -> TelemetryLayer<tracing_distributed::BlackholeTelemetry<SpanId, TraceId>, SpanId, TraceId> {
53    TelemetryLayer::new(
54        "opentelemetry_blackhole_tracing_layer",
55        tracing_distributed::BlackholeTelemetry::default(),
56        |tracing_id| SpanId::from_u64(tracing_id.into_u64()),
57    )
58}
59
60/// Construct a TelemetryLayer that publishes telemetry to honeycomb.io using the provided honeycomb config.
61///
62/// Specialized to the honeycomb.io-specific SpanId and TraceId provided by this crate.
63pub fn new_opentelemetry_layer(
64    service_name: &'static str,
65    exporter: Box<dyn SpanExporter>,
66    config: Config,
67) -> TelemetryLayer<OpenTelemetry, SpanId, TraceId> {
68    // used to keep nodes in a multiprocess scenario from generating the same sequence of span ids
69    let r: u64 = rand::thread_rng().gen();
70    TelemetryLayer::new(
71        service_name,
72        OpenTelemetry {
73            exporter,
74            events: Mutex::new(HashMap::new()),
75            config,
76        },
77        move |tracing_id| SpanId::from_u64(tracing_id.into_u64() ^ r),
78    )
79}