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