Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: rename Layer to Subscriber #1226

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/examples/opentelemetry-remote-context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn build_example_carrier() -> HashMap<String, String> {
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
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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()?;
Expand Down
22 changes: 11 additions & 11 deletions tracing-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ Utilities for adding [OpenTelemetry] interoperability to [`tracing`].
## Overview

[`tracing`] is a framework for instrumenting Rust programs to collect
structured, event-based diagnostic information. This crate provides a layer
that connects spans from multiple systems into a trace and emits them to
[OpenTelemetry]-compatible distributed tracing systems for processing and
visualization.
structured, event-based diagnostic information. This crate provides a
subscriber that connects spans from multiple systems into a trace and
emits them to [OpenTelemetry]-compatible distributed tracing systems
for processing and visualization.

The crate provides the following types:

* [`OpenTelemetryLayer`] adds OpenTelemetry context to all `tracing` [span]s.
* [`OpenTelemetrySubscriber`] adds OpenTelemetry context to all `tracing` [span]s.
* [`OpenTelemetrySpanExt`] allows OpenTelemetry parent trace information to be
injected and extracted from a `tracing` [span].

[`OpenTelemetryLayer`]: https://docs.rs/tracing-opentelemetry/latest/tracing_opentelemetry/struct.OpenTelemetryLayer.html
[`OpenTelemetrySubscriber`]: https://docs.rs/tracing-opentelemetry/latest/tracing_opentelemetry/struct.OpenTelemetrySubscriber.html
[`OpenTelemetrySpanExt`]: https://docs.rs/tracing-opentelemetry/latest/tracing_opentelemetry/trait.OpenTelemetrySpanExt.html
[span]: https://docs.rs/tracing/latest/tracing/span/index.html
[`tracing`]: https://crates.io/crates/tracing
Expand All @@ -61,22 +61,22 @@ The crate provides the following types:
```rust
use opentelemetry::exporter::trace::stdout;
use tracing::{error, span};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::subscriber::SubscriberExt;
use tracing_subscriber::Registry;

fn main() {
// Install a new OpenTelemetry trace pipeline
let (tracer, _uninstall) = stdout::new_pipeline().install();

// Create a tracing layer with the configured tracer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
// Create a tracing subscriber with the configured tracer
let telemetry = tracing_opentelemetry::subscriber().with_tracer(tracer);

// Use the tracing subscriber `Registry`, or any other subscriber
// that impls `LookupSpan`
let subscriber = Registry::default().with(telemetry);
let collector = Registry::default().with(telemetry);

// Trace executed code
tracing::subscriber::with_default(subscriber, || {
tracing::collect::with_default(collector, || {
// Spans will be sent to the configured OpenTelemetry exporter
let root = span!(tracing::Level::TRACE, "app_start", work_units = 2);
let _enter = root.enter();
Expand Down
8 changes: 4 additions & 4 deletions tracing-opentelemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion tracing-opentelemetry/src/span_ext.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, T> {
pub struct OpenTelemetrySubscriber<S, T> {
tracer: T,
tracked_inactivity: bool,
get_context: WithContext,
_registry: marker::PhantomData<S>,
}

impl<S> Default for OpenTelemetryLayer<S, otel::NoopTracer>
impl<S> Default for OpenTelemetrySubscriber<S, otel::NoopTracer>
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
///
Expand All @@ -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<S>() -> OpenTelemetryLayer<S, otel::NoopTracer>
pub fn subscriber<S>() -> OpenTelemetrySubscriber<S, otel::NoopTracer>
where
S: Collect + for<'span> LookupSpan<'span>,
{
OpenTelemetryLayer::default()
OpenTelemetrySubscriber::default()
}

// this function "remembers" the types of the subscriber so that we
Expand Down Expand Up @@ -234,12 +234,12 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> {
}
}

impl<S, T> OpenTelemetryLayer<S, T>
impl<S, T> OpenTelemetrySubscriber<S, T>
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
Expand All @@ -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;
///
Expand All @@ -257,24 +257,24 @@ 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),
_registry: marker::PhantomData,
}
}

/// 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
Expand All @@ -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<Tracer>(self, tracer: Tracer) -> OpenTelemetryLayer<S, Tracer>
pub fn with_tracer<Tracer>(self, tracer: Tracer) -> OpenTelemetrySubscriber<S, Tracer>
where
Tracer: otel::Tracer + PreSampledTracer + 'static,
{
OpenTelemetryLayer {
OpenTelemetrySubscriber {
tracer,
tracked_inactivity: self.tracked_inactivity,
get_context: WithContext(OpenTelemetryLayer::<S, Tracer>::get_context),
get_context: WithContext(OpenTelemetrySubscriber::<S, Tracer>::get_context),
_registry: self._registry,
}
}
Expand Down Expand Up @@ -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::<OpenTelemetryLayer<S, T>>()
.expect("layer should downcast to expected type; this is a bug!");
let subscriber = dispatch
.downcast_ref::<OpenTelemetrySubscriber<S, T>>()
.expect("subscriber should downcast to expected type; this is a bug!");

let mut extensions = span.extensions_mut();
if let Some(builder) = extensions.get_mut::<otel::SpanBuilder>() {
f(builder, &layer.tracer);
f(builder, &subscriber.tracer);
}
}
}

impl<S, T> Subscribe<S> for OpenTelemetryLayer<S, T>
impl<S, T> Subscribe<S> for OpenTelemetrySubscriber<S, T>
where
S: Collect + for<'span> LookupSpan<'span>,
T: otel::Tracer + PreSampledTracer + 'static,
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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),
);
Expand Down
4 changes: 2 additions & 2 deletions tracing-opentelemetry/tests/trace_state_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}
Expand Down