sentry_tracing/layer/error.rs
1use sentry_core::protocol::Event as SentryEvent;
2use tracing_core::{Event, Subscriber};
3use tracing_subscriber::layer::{Context, Layer};
4use tracing_subscriber::registry::LookupSpan;
5
6use crate::converters::event_from_event_impl;
7
8/// Captures tracing events as Sentry error events.
9///
10/// Using this layer alongside the legacy [`SentryLayer`](super::SentryLayer) is unsupported.
11/// Use either the legacy layer or the individual layers, not both.
12#[must_use]
13#[derive(Default)]
14pub struct ErrorLayer {
15 with_span_attributes: bool,
16}
17
18impl ErrorLayer {
19 /// Creates a layer that captures tracing events as Sentry error events.
20 ///
21 /// # Filtering
22 ///
23 /// Configure a level filter to limit error-event volume. We recommend capturing only `ERROR`
24 /// events, as shown below.
25 ///
26 /// <div class="warning">
27 ///
28 /// **Do not use this layer without a filter.** An unfiltered layer captures every tracing
29 /// event, including `DEBUG` and `TRACE`, as a Sentry error event. This can quickly exhaust
30 /// your Sentry error quota.
31 ///
32 /// </div>
33 ///
34 /// ```rust
35 /// # use tracing_subscriber::prelude::*;
36 /// use sentry::integrations::tracing::ErrorLayer;
37 /// use tracing_subscriber::filter::LevelFilter;
38 ///
39 /// tracing_subscriber::registry()
40 /// .with(ErrorLayer::new().with_filter(LevelFilter::ERROR))
41 /// .init();
42 ///
43 /// // This will be captured ...
44 /// tracing::error!("ERROR event");
45 ///
46 /// // ... but this will not be, due to LevelFilter::ERROR being set.
47 /// tracing::warn!("WARN event");
48 /// ```
49 ///
50 /// # Event conversion
51 ///
52 /// With the default converter:
53 ///
54 /// - String, numeric, and boolean fields prefixed with `tags.` become Sentry tags, with the
55 /// prefix removed.
56 /// - Other data fields are stored in the `Rust Tracing Fields` context on the Sentry error event.
57 /// - Record an error as `&dyn std::error::Error` to capture its exception type and source chain.
58 pub fn new() -> Self {
59 Self::default()
60 }
61
62 /// Include attributes of parent spans in error events.
63 ///
64 /// We can only include spans captured by a [`super::SpanLayer`] on the same subscriber. So,
65 /// this option only has any effect when the `SpanLayer` is installed.
66 ///
67 /// # Filtering
68 ///
69 /// Parent-span attributes are included only when the span is accepted by both `SpanLayer`
70 /// and `ErrorLayer`. A level filter such as `LevelFilter::ERROR` therefore omits attributes
71 /// from less severe spans.
72 ///
73 /// To include attributes from all spans captured by `SpanLayer`, we recommend accepting all
74 /// spans in this layer's filter, while filtering events to the desired level, like so:
75 ///
76 /// ```rust
77 /// # use tracing_subscriber::prelude::*;
78 /// use sentry::integrations::tracing::{ErrorLayer, SpanLayer};
79 /// use tracing_subscriber::filter::{filter_fn, LevelFilter, FilterExt as _};
80 ///
81 /// tracing_subscriber::registry()
82 /// .with(SpanLayer::new().with_filter(LevelFilter::INFO))
83 /// .with(
84 /// ErrorLayer::new()
85 /// .enable_span_attributes()
86 /// .with_filter(LevelFilter::ERROR.or(filter_fn(|m| m.is_span()))),
87 /// )
88 /// .init();
89 /// ```
90 ///
91 /// Including all spans in the error layer filter is safe, because this layer does not send
92 /// any spans to Sentry.
93 pub fn enable_span_attributes(mut self) -> Self {
94 self.with_span_attributes = true;
95 self
96 }
97
98 /// Capture an error event on the current `Hub`.
99 pub(super) fn capture(&self, event: SentryEvent<'static>) {
100 sentry_core::capture_event(event);
101 }
102
103 /// Capture a tracing event as a Sentry error event.
104 pub(super) fn capture_tracing_event<S>(&self, event: &Event, ctx: Context<'_, S>)
105 where
106 S: Subscriber + for<'a> LookupSpan<'a>,
107 {
108 let sentry_event = event_from_event_impl(event, self.with_span_attributes.then_some(&ctx));
109 self.capture(sentry_event);
110 }
111}
112
113impl<S> Layer<S> for ErrorLayer
114where
115 S: Subscriber + for<'a> LookupSpan<'a>,
116{
117 fn on_event(&self, event: &Event, ctx: Context<'_, S>) {
118 self.capture_tracing_event(event, ctx);
119 }
120}