Skip to main content

SentryLayer

Struct SentryLayer 

Source
pub struct SentryLayer<S> { /* private fields */ }
๐Ÿ‘ŽDeprecated:

Prefer SpanLayer::new() and LogLayer::new(); see SentryLayer docs for migration

Expand description

Legacy combined tracing layer.

For most applications, use SpanLayer and LogLayer with explicit filters instead. Add ErrorLayer if tracing events should also be captured as Sentry error events, and BreadcrumbLayer if errors need breadcrumb context.

Mixing this legacy layer with any individual Sentry layer is unsupported. Combining it with SpanLayer can panic when a span is created. When migrating, replace the legacy layer rather than adding individual layers alongside it.

ยงMigrating the default configuration

The individual layers do not filter by level internally. The following configuration reproduces the default level selection of SentryLayer with the logs feature enabled:

  • ERROR, WARN, and INFO spans become Sentry transactions and spans.
  • ERROR, WARN, and INFO events become structured logs.
  • ERROR events also become Sentry error events.
  • WARN and INFO events also become breadcrumbs on the current scope, sent with subsequent captured errors.
use sentry::integrations::tracing::{BreadcrumbLayer, ErrorLayer, LogLayer, SpanLayer};
use tracing::Level;
use tracing_subscriber::filter::{filter_fn, LevelFilter};
use tracing_subscriber::prelude::*;

tracing_subscriber::registry()
    .with(SpanLayer::new().with_filter(LevelFilter::INFO))
    .with(
        BreadcrumbLayer::new().with_filter(filter_fn(|metadata| {
            matches!(*metadata.level(), Level::WARN | Level::INFO)
        })),
    )
    .with(ErrorLayer::new().with_filter(LevelFilter::ERROR))
    .with(LogLayer::new().with_filter(LevelFilter::INFO))
    .init();

Without the logs feature, omit the log layer. Client configuration, including trace sampling, is unchanged.

If the legacy layer used enable_span_attributes, enable span attributes on each individual event layer that needs them. See the option docs for breadcrumbs, errors, and logs for filtering requirements.

ยงMigrating custom configuration

  • Replace span_filter with a filter on the span layer.
  • Split event_filter into filters on the individual event layers. An event can be accepted by multiple layers, replacing combined EventFilter flags.
  • Individual layers do not provide mapper callbacks. Some SentryLayer::event_mapper usages may be addressed by other existing APIs, such as filter callbacks on the layers or before_send* callbacks provided by the Sentry SDK. However, we recognize it may not be possible to migrate all existing usages. If you are unable to migrate to the new API due to this change, we would love to hear from you. Please open a GitHub issue explaining your use case, and we will work with you to find a solution.

If a custom configuration sends the same event to both the breadcrumb and error layers, install the breadcrumb layer before the error layer to include that breadcrumb in the error. The default configuration above excludes ERROR events from breadcrumbs, so the order does not affect their inclusion.

Implementationsยง

Sourceยง

impl<S> SentryLayer<S>

Source

pub fn event_filter<F>(self, filter: F) -> Self
where F: Fn(&Metadata<'_>) -> EventFilter + Send + Sync + 'static,

Sets a custom event filter function.

The filter classifies how sentry should handle Events based on their Metadata.

Source

pub fn event_mapper<F>(self, mapper: F) -> Self
where F: Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync + 'static,

Sets a custom event mapper function.

The mapper is responsible for creating either breadcrumbs or events from Events.

Source

pub fn span_filter<F>(self, filter: F) -> Self
where F: Fn(&Metadata<'_>) -> bool + Send + Sync + 'static,

Sets a custom span filter function.

The filter classifies whether sentry should handle tracing::Spans based on their Metadata.

Source

pub fn enable_span_attributes(self) -> Self

Include attributes of parent spans in breadcrumbs, error events, and logs.

Only spans accepted by this layerโ€™s span filter provide attributes. Including attributes does not require the transaction to be sampled.

When migrating, enable span attributes on each individual event layer that needs them; see the migration guide.

Trait Implementationsยง

Sourceยง

impl<S> Default for SentryLayer<S>
where S: Subscriber + for<'a> LookupSpan<'a>,

Sourceยง

fn default() -> Self

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<S> Layer<S> for SentryLayer<S>
where S: Subscriber + for<'a> LookupSpan<'a>,

Sourceยง

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

Notifies this layer that an event has occurred.
Sourceยง

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

Notifies this layer that a new span was constructed with the given Attributes and Id.
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_close(&self, id: Id, ctx: Context<'_, S>)

Notifies this layer that the span with the given ID has been closed.
Sourceยง

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

Notifies this layer that a span with the given Id recorded the given values.
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 on_follows_from(&self, _span: &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 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 boxed(self) -> Box<dyn Layer<S> + Send + Sync>
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> !RefUnwindSafe for SentryLayer<S>

ยง

impl<S> !UnwindSafe for SentryLayer<S>

ยง

impl<S> Freeze for SentryLayer<S>
where Option<Box<dyn Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync>>: Freeze,

ยง

impl<S> Send for SentryLayer<S>
where Option<Box<dyn Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync>>: Send,

ยง

impl<S> Sync for SentryLayer<S>
where Option<Box<dyn Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync>>: Sync,

ยง

impl<S> Unpin for SentryLayer<S>
where Option<Box<dyn Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync>>: Unpin,

ยง

impl<S> UnsafeUnpin for SentryLayer<S>
where Option<Box<dyn Fn(&Event<'_>, Context<'_, S>) -> EventMapping + Send + Sync>>: UnsafeUnpin,

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, 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.