diff --git a/examples/examples/opentelemetry-remote-context.rs b/examples/examples/opentelemetry-remote-context.rs index 347e8c73f8..d882ee9cbb 100644 --- a/examples/examples/opentelemetry-remote-context.rs +++ b/examples/examples/opentelemetry-remote-context.rs @@ -25,7 +25,7 @@ fn build_example_carrier() -> HashMap { fn main() { // Set a format for propagating context. This MUST be provided, as the default is a no-op. global::set_text_map_propagator(TraceContextPropagator::new()); - let subscriber = Registry::default().with(tracing_opentelemetry::layer()); + let subscriber = Registry::default().with(tracing_opentelemetry::subscriber()); tracing::collect::with_default(subscriber, || { // Extract context from request headers diff --git a/examples/examples/opentelemetry.rs b/examples/examples/opentelemetry.rs index b7e522ef5c..66231db593 100644 --- a/examples/examples/opentelemetry.rs +++ b/examples/examples/opentelemetry.rs @@ -18,7 +18,7 @@ fn main() -> Result<(), Box> { let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline() .with_service_name("report_example") .install()?; - let opentelemetry = tracing_opentelemetry::layer().with_tracer(tracer); + let opentelemetry = tracing_opentelemetry::subscriber().with_tracer(tracer); tracing_subscriber::registry() .with(opentelemetry) .try_init()?; diff --git a/tracing-opentelemetry/src/lib.rs b/tracing-opentelemetry/src/lib.rs index 262f1d4cae..4b19fe58f8 100644 --- a/tracing-opentelemetry/src/lib.rs +++ b/tracing-opentelemetry/src/lib.rs @@ -57,7 +57,7 @@ //! let (tracer, _uninstall) = stdout::new_pipeline().install(); //! //! // Create a tracing layer with the configured tracer -//! let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); +//! let telemetry = tracing_opentelemetry::subscriber().with_tracer(tracer); //! //! // Use the tracing subscriber `Registry`, or any other subscriber //! // that impls `LookupSpan` @@ -96,13 +96,13 @@ issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/" )] -/// Implementation of the trace::Subscriber as a source of OpenTelemetry data. -mod layer; /// Span extension which enables OpenTelemetry context management. mod span_ext; +/// Implementation of the trace::Subscriber as a source of OpenTelemetry data. +mod subscriber; /// Protocols for OpenTelemetry Tracers that are compatible with Tracing mod tracer; -pub use layer::{layer, OpenTelemetryLayer}; pub use span_ext::OpenTelemetrySpanExt; +pub use subscriber::{subscriber, OpenTelemetrySubscriber}; pub use tracer::PreSampledTracer; diff --git a/tracing-opentelemetry/src/span_ext.rs b/tracing-opentelemetry/src/span_ext.rs index 476c29f5c8..6fee50081f 100644 --- a/tracing-opentelemetry/src/span_ext.rs +++ b/tracing-opentelemetry/src/span_ext.rs @@ -1,4 +1,4 @@ -use crate::layer::WithContext; +use crate::subscriber::WithContext; use opentelemetry::Context; /// Utility functions to allow tracing [`Span`]s to accept and return diff --git a/tracing-opentelemetry/src/layer.rs b/tracing-opentelemetry/src/subscriber.rs similarity index 92% rename from tracing-opentelemetry/src/layer.rs rename to tracing-opentelemetry/src/subscriber.rs index 94b2281836..cb2b7aa4ce 100644 --- a/tracing-opentelemetry/src/layer.rs +++ b/tracing-opentelemetry/src/subscriber.rs @@ -15,28 +15,28 @@ use tracing_subscriber::Subscribe; static SPAN_NAME_FIELD: &str = "otel.name"; static SPAN_KIND_FIELD: &str = "otel.kind"; -/// An [OpenTelemetry] propagation layer for use in a project that uses +/// An [OpenTelemetry] propagation subscriber for use in a project that uses /// [tracing]. /// /// [OpenTelemetry]: https://opentelemetry.io /// [tracing]: https://github.com/tokio-rs/tracing -pub struct OpenTelemetryLayer { +pub struct OpenTelemetrySubscriber { tracer: T, tracked_inactivity: bool, get_context: WithContext, _registry: marker::PhantomData, } -impl Default for OpenTelemetryLayer +impl Default for OpenTelemetrySubscriber where S: Collect + for<'span> LookupSpan<'span>, { fn default() -> Self { - OpenTelemetryLayer::new(otel::NoopTracer::new()) + OpenTelemetrySubscriber::new(otel::NoopTracer::new()) } } -/// Construct a layer to track spans via [OpenTelemetry]. +/// Construct a subscriber to track spans via [OpenTelemetry]. /// /// [OpenTelemetry]: https://opentelemetry.io /// @@ -48,14 +48,14 @@ where /// /// // Use the tracing subscriber `Registry`, or any other subscriber /// // that impls `LookupSpan` -/// let subscriber = Registry::default().with(tracing_opentelemetry::layer()); +/// let subscriber = Registry::default().with(tracing_opentelemetry::subscriber()); /// # drop(subscriber); /// ``` -pub fn layer() -> OpenTelemetryLayer +pub fn subscriber() -> OpenTelemetrySubscriber where S: Collect + for<'span> LookupSpan<'span>, { - OpenTelemetryLayer::default() + OpenTelemetrySubscriber::default() } // this function "remembers" the types of the subscriber so that we @@ -234,12 +234,12 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> { } } -impl OpenTelemetryLayer +impl OpenTelemetrySubscriber where S: Collect + for<'span> LookupSpan<'span>, T: otel::Tracer + PreSampledTracer + 'static, { - /// Set the [`Tracer`] that this layer will use to produce and track + /// Set the [`Tracer`] that this subscriber will use to produce and track /// OpenTelemetry [`Span`]s. /// /// [`Tracer`]: opentelemetry::trace::Tracer @@ -248,7 +248,7 @@ where /// # Examples /// /// ```no_run - /// use tracing_opentelemetry::OpenTelemetryLayer; + /// use tracing_opentelemetry::OpenTelemetrySubscriber; /// use tracing_subscriber::subscribe::CollectExt; /// use tracing_subscriber::Registry; /// @@ -257,16 +257,16 @@ where /// .with_service_name("trace_demo") /// .install().expect("Error initializing Jaeger exporter"); /// - /// // Create a layer with the configured tracer - /// let otel_layer = OpenTelemetryLayer::new(tracer); + /// // Create a subscriber with the configured tracer + /// let otel_subscriber = OpenTelemetrySubscriber::new(tracer); /// /// // Use the tracing subscriber `Registry`, or any other subscriber /// // that impls `LookupSpan` - /// let subscriber = Registry::default().with(otel_layer); + /// let subscriber = Registry::default().with(otel_subscriber); /// # drop(subscriber); /// ``` pub fn new(tracer: T) -> Self { - OpenTelemetryLayer { + OpenTelemetrySubscriber { tracer, tracked_inactivity: true, get_context: WithContext(Self::get_context), @@ -274,7 +274,7 @@ where } } - /// Set the [`Tracer`] that this layer will use to produce and track + /// Set the [`Tracer`] that this subscriber will use to produce and track /// OpenTelemetry [`Span`]s. /// /// [`Tracer`]: opentelemetry::trace::Tracer @@ -291,22 +291,22 @@ where /// .with_service_name("trace_demo") /// .install().expect("Error initializing Jaeger exporter"); /// - /// // Create a layer with the configured tracer - /// let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer); + /// // Create a subscriber with the configured tracer + /// let otel_subscriber = tracing_opentelemetry::subscriber().with_tracer(tracer); /// /// // Use the tracing subscriber `Registry`, or any other subscriber /// // that impls `LookupSpan` - /// let subscriber = Registry::default().with(otel_layer); + /// let subscriber = Registry::default().with(otel_subscriber); /// # drop(subscriber); /// ``` - pub fn with_tracer(self, tracer: Tracer) -> OpenTelemetryLayer + pub fn with_tracer(self, tracer: Tracer) -> OpenTelemetrySubscriber where Tracer: otel::Tracer + PreSampledTracer + 'static, { - OpenTelemetryLayer { + OpenTelemetrySubscriber { tracer, tracked_inactivity: self.tracked_inactivity, - get_context: WithContext(OpenTelemetryLayer::::get_context), + get_context: WithContext(OpenTelemetrySubscriber::::get_context), _registry: self._registry, } } @@ -364,18 +364,18 @@ where let span = subscriber .span(id) .expect("registry should have a span for the current ID"); - let layer = dispatch - .downcast_ref::>() - .expect("layer should downcast to expected type; this is a bug!"); + let subscriber = dispatch + .downcast_ref::>() + .expect("subscriber should downcast to expected type; this is a bug!"); let mut extensions = span.extensions_mut(); if let Some(builder) = extensions.get_mut::() { - f(builder, &layer.tracer); + f(builder, &subscriber.tracer); } } } -impl Subscribe for OpenTelemetryLayer +impl Subscribe for OpenTelemetrySubscriber where S: Collect + for<'span> LookupSpan<'span>, T: otel::Tracer + PreSampledTracer + 'static, @@ -642,7 +642,8 @@ mod tests { fn dynamic_span_names() { let dynamic_name = "GET http://example.com".to_string(); let tracer = TestTracer(Arc::new(Mutex::new(None))); - let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone())); + let subscriber = + tracing_subscriber::registry().with(subscriber().with_tracer(tracer.clone())); tracing::collect::with_default(subscriber, || { tracing::debug_span!("static_name", otel.name = dynamic_name.as_str()); @@ -655,7 +656,8 @@ mod tests { #[test] fn span_kind() { let tracer = TestTracer(Arc::new(Mutex::new(None))); - let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone())); + let subscriber = + tracing_subscriber::registry().with(subscriber().with_tracer(tracer.clone())); tracing::collect::with_default(subscriber, || { tracing::debug_span!("request", otel.kind = %SpanKind::Server); @@ -668,7 +670,8 @@ mod tests { #[test] fn trace_id_from_existing_context() { let tracer = TestTracer(Arc::new(Mutex::new(None))); - let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone())); + let subscriber = + tracing_subscriber::registry().with(subscriber().with_tracer(tracer.clone())); let trace_id = otel::TraceId::from_u128(42); let existing_cx = OtelContext::current_with_span(TestSpan(otel::SpanContext::new( trace_id, @@ -702,7 +705,7 @@ mod tests { fn includes_timings() { let tracer = TestTracer(Arc::new(Mutex::new(None))); let subscriber = tracing_subscriber::registry().with( - layer() + subscriber() .with_tracer(tracer.clone()) .with_tracked_inactivity(true), ); diff --git a/tracing-opentelemetry/tests/trace_state_propagation.rs b/tracing-opentelemetry/tests/trace_state_propagation.rs index aeb631a3e4..0cb26be0b8 100644 --- a/tracing-opentelemetry/tests/trace_state_propagation.rs +++ b/tracing-opentelemetry/tests/trace_state_propagation.rs @@ -12,7 +12,7 @@ use opentelemetry::{ use std::collections::{HashMap, HashSet}; use std::sync::{Arc, Mutex}; use tracing::Collect; -use tracing_opentelemetry::{layer, OpenTelemetrySpanExt}; +use tracing_opentelemetry::{subscriber, OpenTelemetrySpanExt}; use tracing_subscriber::prelude::*; #[test] @@ -113,7 +113,7 @@ fn test_tracer() -> (Tracer, TracerProvider, TestExporter, impl Collect) { .with_simple_exporter(exporter.clone()) .build(); let tracer = provider.get_tracer("test", None); - let subscriber = tracing_subscriber::registry().with(layer().with_tracer(tracer.clone())); + let subscriber = tracing_subscriber::registry().with(subscriber().with_tracer(tracer.clone())); (tracer, provider, exporter, subscriber) }