Skip to main content

sentry_tracing/layer/
breadcrumb.rs

1use sentry_core::Breadcrumb;
2use tracing_core::{Event, Subscriber};
3use tracing_subscriber::layer::{Context, Layer};
4use tracing_subscriber::registry::LookupSpan;
5
6use crate::converters::breadcrumb_from_event_impl;
7
8/// Adds tracing events as Sentry breadcrumbs.
9///
10/// Without a filter, events at every level become breadcrumbs. Configure a filter to limit
11/// breadcrumb volume; see [`BreadcrumbLayer::new`] for the recommended levels.
12///
13/// Using this layer alongside the legacy [`SentryLayer`](super::SentryLayer) is unsupported.
14/// Use either the legacy layer or the individual layers, not both.
15#[must_use]
16#[derive(Default)]
17pub struct BreadcrumbLayer {
18    with_span_attributes: bool,
19}
20
21impl BreadcrumbLayer {
22    /// Creates a layer that adds tracing events as Sentry breadcrumbs.
23    ///
24    /// This layer primarily exists to support legacy workflows. For most users, we would recommend
25    /// using the [`LogLayer`](super::LogLayer) instead, as logs are sent regardless of whether an
26    /// error occurs.
27    ///
28    /// Breadcrumbs are only sent attached to Sentry error events. Users using this layer may therefore
29    /// also wish to enable the [`ErrorLayer`](super::ErrorLayer).
30    ///
31    /// # Filtering
32    ///
33    /// Without a filter, events at every level become breadcrumbs. Configure a filter to limit
34    /// breadcrumb volume. We recommend capturing `WARN` and `INFO` events. If you also use the error layer, exclude `ERROR`
35    /// events, which would otherwise duplicate the error event as a breadcrumb. If a custom filter
36    /// sends the same event to both layers, install the breadcrumb layer before the error layer so
37    /// that breadcrumb is included in the error.
38    ///
39    /// ```rust
40    /// # use tracing_subscriber::prelude::*;
41    /// use sentry::integrations::tracing::{BreadcrumbLayer, ErrorLayer};
42    /// use tracing::Level;
43    /// use tracing_subscriber::filter::{filter_fn, LevelFilter};
44    ///
45    /// tracing_subscriber::registry()
46    ///     .with(
47    ///         BreadcrumbLayer::new().with_filter(filter_fn(|metadata| {
48    ///             matches!(*metadata.level(), Level::WARN | Level::INFO)
49    ///         })),
50    ///     )
51    ///     .with(ErrorLayer::new().with_filter(LevelFilter::ERROR))
52    ///     .init();
53    ///
54    /// // These will be captured as breadcrumbs ...
55    /// tracing::info!("INFO breadcrumb");
56    /// tracing::warn!("WARN breadcrumb");
57    ///
58    /// // ... but this will not be.
59    /// tracing::debug!("DEBUG breadcrumb");
60    ///
61    /// // This will not be captured as a breadcrumb, but as an error event by the error layer. The
62    /// // breadcrumbs above will be contained in that error event.
63    /// tracing::error!("ERROR event");
64    /// ```
65    pub fn new() -> Self {
66        Self::default()
67    }
68
69    /// Include attributes of parent spans in breadcrumbs.
70    ///
71    /// We can only include spans captured by a [`super::SpanLayer`] on the same subscriber. So,
72    /// this option only has any effect when the `SpanLayer` is installed.
73    ///
74    /// # Filtering
75    ///
76    /// Parent-span attributes are included only when the span is accepted by both `SpanLayer`
77    /// and `BreadcrumbLayer`. A filter that only accepts `WARN` and `INFO` metadata therefore
78    /// omits attributes from spans at other levels.
79    ///
80    /// To include attributes from all spans captured by `SpanLayer`, we recommend accepting all
81    /// spans in this layer's filter, while filtering events to the desired levels, like so:
82    ///
83    /// ```rust
84    /// # use tracing_subscriber::prelude::*;
85    /// use sentry::integrations::tracing::{BreadcrumbLayer, SpanLayer};
86    /// use tracing::Level;
87    /// use tracing_subscriber::filter::{filter_fn, LevelFilter};
88    ///
89    /// tracing_subscriber::registry()
90    ///     .with(SpanLayer::new().with_filter(LevelFilter::INFO))
91    ///     .with(
92    ///         BreadcrumbLayer::new()
93    ///             .enable_span_attributes()
94    ///             .with_filter(filter_fn(|metadata| {
95    ///                 // Accept all spans, but only `WARN` and `INFO` events.
96    ///                 metadata.is_span() || matches!(*metadata.level(), Level::WARN | Level::INFO)
97    ///             })),
98    ///     )
99    ///     .init();
100    /// ```
101    ///
102    /// Including all spans in the breadcrumb layer filter is safe, because this layer does not send
103    /// any spans to Sentry.
104    pub fn enable_span_attributes(mut self) -> Self {
105        self.with_span_attributes = true;
106        self
107    }
108
109    /// Add a breadcrumb to the current `Hub`'s scope.
110    pub(super) fn capture(&self, breadcrumb: Breadcrumb) {
111        sentry_core::add_breadcrumb(breadcrumb);
112    }
113}
114
115impl BreadcrumbLayer {
116    /// Capture a tracing event as a breadcrumb.
117    pub(super) fn capture_tracing_event<S>(&self, event: &Event, ctx: Context<'_, S>)
118    where
119        S: Subscriber + for<'a> LookupSpan<'a>,
120    {
121        self.capture(breadcrumb_from_event_impl(
122            event,
123            self.with_span_attributes.then_some(&ctx),
124        ));
125    }
126}
127
128impl<S> Layer<S> for BreadcrumbLayer
129where
130    S: Subscriber + for<'a> LookupSpan<'a>,
131{
132    fn on_event(&self, event: &Event, ctx: Context<'_, S>) {
133        self.capture_tracing_event(event, ctx);
134    }
135}