tracing_opentelemetry

Struct OpenTelemetryLayer

Source
pub struct OpenTelemetryLayer<S, T> { /* private fields */ }
Expand description

An OpenTelemetry propagation layer for use in a project that uses tracing.

Implementations§

Source§

impl<S, T> OpenTelemetryLayer<S, T>
where S: Subscriber + for<'span> LookupSpan<'span>, T: Tracer + PreSampledTracer + 'static,

Source

pub fn new(tracer: T) -> Self

Set the Tracer that this layer will use to produce and track OpenTelemetry Spans.

§Examples
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::layer::SubscriberExt;
use opentelemetry::trace::TracerProvider as _;
use tracing_subscriber::Registry;

// Create an OTLP pipeline exporter for a `trace_demo` service.

let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
    .with_tonic()
    .build()
    .unwrap();

let tracer = opentelemetry_sdk::trace::TracerProvider::builder()
    .with_simple_exporter(otlp_exporter)
    .build()
    .tracer("trace_demo");

// Create a layer with the configured tracer
let otel_layer = OpenTelemetryLayer::new(tracer);

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

pub fn with_tracer<Tracer>( self, tracer: Tracer, ) -> OpenTelemetryLayer<S, Tracer>
where Tracer: Tracer + PreSampledTracer + 'static,

Set the Tracer that this layer will use to produce and track OpenTelemetry Spans.

§Examples
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
use opentelemetry::trace::TracerProvider;

// Create an OTLP pipeline exporter for a `trace_demo` service.

let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
    .with_tonic()
    .build()
    .unwrap();

let tracer = opentelemetry_sdk::trace::TracerProvider::builder()
    .with_simple_exporter(otlp_exporter)
    .build()
    .tracer("trace_demo");

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

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

pub fn with_error_fields_to_exceptions( self, error_fields_to_exceptions: bool, ) -> Self

Sets whether or not span and event metadata should include OpenTelemetry exception fields such as exception.message and exception.backtrace when an Error value is recorded. If multiple error values are recorded on the same span/event, only the most recently recorded error value will show up under these fields.

These attributes follow the OpenTelemetry semantic conventions for exceptions.

By default, these attributes are recorded. Note that this only works for (dyn Error + 'static). See Implementations on Foreign Types of tracing::Value or OpenTelemetryLayer::with_error_events_to_exceptions

Source

pub fn with_error_events_to_status(self, error_events_to_status: bool) -> Self

Sets whether or not an event considered for exception mapping (see [OpenTelemetryLayer::with_error_recording]) should be propagated to the span status error description.

By default, these events do set the span status error description.

Source

pub fn with_error_events_to_exceptions( self, error_events_to_exceptions: bool, ) -> Self

Sets whether or not a subset of events following the described schema are mapped to events following the OpenTelemetry semantic conventions for exceptions.

  • Only events without a message field (unnamed events) and at least one field with the name error are considered for mapping.

By default, these events are mapped.

Source

pub fn with_error_records_to_exceptions( self, error_records_to_exceptions: bool, ) -> Self

Sets whether or not reporting an Error value on an event will propagate the OpenTelemetry exception fields such as exception.message and exception.backtrace to the corresponding span. You do not need to enable with_exception_fields in order to enable this. If multiple error values are recorded on the same span/event, only the most recently recorded error value will show up under these fields.

These attributes follow the OpenTelemetry semantic conventions for exceptions.

By default, these attributes are propagated to the span. Note that this only works for (dyn Error + 'static). See Implementations on Foreign Types of tracing::Value or OpenTelemetryLayer::with_error_events_to_exceptions

Source

pub fn with_location(self, location: bool) -> Self

Sets whether or not span and event metadata should include OpenTelemetry attributes with location information, such as the file, module and line number.

These attributes follow the OpenTelemetry semantic conventions for source locations.

By default, locations are enabled.

Source

pub fn with_tracked_inactivity(self, tracked_inactivity: bool) -> Self

Sets whether or not spans metadata should include the busy time (total time for which it was entered), and idle time (total time the span existed but was not entered).

Source

pub fn with_threads(self, threads: bool) -> Self

Sets whether or not spans record additional attributes for the thread name and thread ID of the thread they were created on, following the OpenTelemetry semantic conventions for threads.

By default, thread attributes are enabled.

Source

pub fn with_level(self, level: bool) -> Self

Sets whether or not span metadata should include the tracing verbosity level information as a level field.

The level is always added to events, and based on OpenTelemetryLayer::with_error_events_to_status error-level events will mark the span status as an error.

By default, level information is disabled.

Trait Implementations§

Source§

impl<S> Default for OpenTelemetryLayer<S, NoopTracer>
where S: Subscriber + for<'span> LookupSpan<'span>,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S, T> Layer<S> for OpenTelemetryLayer<S, T>
where S: Subscriber + for<'span> LookupSpan<'span>, T: Tracer + PreSampledTracer + 'static,

Source§

fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)

Creates an OpenTelemetry Span for the corresponding tracing Span.

Source§

fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>)

Record OpenTelemetry attributes for the given values.

Source§

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)

Records OpenTelemetry Event data on event.

Note: an ERROR-level event will also set the OpenTelemetry span status code to Error, signaling that an error has occurred.

Source§

fn on_close(&self, id: Id, ctx: Context<'_, S>)

Exports an OpenTelemetry Span on close.

Source§

fn on_enter(&self, id: &Id, ctx: Context<'_, S>)

Notifies this layer that a span with the given ID was entered.
Source§

fn on_exit(&self, id: &Id, ctx: Context<'_, S>)

Notifies this layer that the span with the given ID was exited.
Source§

fn on_follows_from(&self, id: &Id, follows: &Id, ctx: Context<'_, S>)

Notifies this layer that a span with the ID span recorded that it follows from the span with the ID follows.
Source§

fn on_register_dispatch(&self, subscriber: &Dispatch)

Performs late initialization when installing this layer as a Subscriber. Read more
Source§

fn on_layer(&mut self, subscriber: &mut S)

Performs late initialization when attaching a Layer to a Subscriber. Read more
Source§

fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest

Registers a new callsite with this layer, returning whether or not the layer is interested in being notified about the callsite, similarly to Subscriber::register_callsite. Read more
Source§

fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool

Returns true if this layer is interested in a span or event with the given metadata in the current Context, similarly to Subscriber::enabled. Read more
Source§

fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, S>) -> bool

Called before on_event, to determine if on_event should be called. Read more
Source§

fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span ID has been cloned, and that the subscriber returned a different ID.
Source§

fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
where L: Layer<S>, Self: Sized,

Composes this layer around the given Layer, returning a Layered struct implementing Layer. Read more
Source§

fn with_subscriber(self, inner: S) -> Layered<Self, S>
where Self: Sized,

Composes this Layer with the given Subscriber, returning a Layered struct that implements Subscriber. Read more
Source§

fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S>
where Self: Sized, F: Filter<S>,

Available on crate features registry and std only.
Combines self with a Filter, returning a Filtered layer. Read more
Source§

fn boxed(self) -> Box<dyn Layer<S> + Sync + Send>
where Self: Sized + Layer<S> + Send + Sync + 'static, S: Subscriber,

Available on crate features alloc or std only.
Erases the type of this Layer, returning a Boxed dyn Layer trait object. Read more

Auto Trait Implementations§

§

impl<S, T> Freeze for OpenTelemetryLayer<S, T>
where T: Freeze,

§

impl<S, T> RefUnwindSafe for OpenTelemetryLayer<S, T>

§

impl<S, T> Send for OpenTelemetryLayer<S, T>
where T: Send, S: Send,

§

impl<S, T> Sync for OpenTelemetryLayer<S, T>
where T: Sync, S: Sync,

§

impl<S, T> Unpin for OpenTelemetryLayer<S, T>
where T: Unpin, S: Unpin,

§

impl<S, T> UnwindSafe for OpenTelemetryLayer<S, T>
where T: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more