sentry_tracing/
converters.rs

1use std::collections::BTreeMap;
2use std::error::Error;
3
4use sentry_core::protocol::{Event, Exception, Mechanism, Value};
5#[cfg(feature = "logs")]
6use sentry_core::protocol::{Log, LogAttribute, LogLevel};
7use sentry_core::{event_from_error, Breadcrumb, Level, TransactionOrSpan};
8#[cfg(feature = "logs")]
9use std::time::SystemTime;
10use tracing_core::field::{Field, Visit};
11use tracing_core::Subscriber;
12use tracing_subscriber::layer::Context;
13use tracing_subscriber::registry::LookupSpan;
14
15use super::layer::SentrySpanData;
16use crate::TAGS_PREFIX;
17
18/// Converts a [`tracing_core::Level`] to a Sentry [`Level`], used for events and breadcrumbs.
19fn level_to_sentry_level(level: &tracing_core::Level) -> Level {
20    match *level {
21        tracing_core::Level::TRACE | tracing_core::Level::DEBUG => Level::Debug,
22        tracing_core::Level::INFO => Level::Info,
23        tracing_core::Level::WARN => Level::Warning,
24        tracing_core::Level::ERROR => Level::Error,
25    }
26}
27
28/// Converts a [`tracing_core::Level`] to a Sentry [`LogLevel`], used for logs.
29#[cfg(feature = "logs")]
30fn level_to_log_level(level: &tracing_core::Level) -> LogLevel {
31    match *level {
32        tracing_core::Level::TRACE => LogLevel::Trace,
33        tracing_core::Level::DEBUG => LogLevel::Debug,
34        tracing_core::Level::INFO => LogLevel::Info,
35        tracing_core::Level::WARN => LogLevel::Warn,
36        tracing_core::Level::ERROR => LogLevel::Error,
37    }
38}
39
40/// Converts a [`tracing_core::Level`] to the corresponding Sentry [`Exception::ty`] entry.
41#[allow(unused)]
42fn level_to_exception_type(level: &tracing_core::Level) -> &'static str {
43    match *level {
44        tracing_core::Level::TRACE => "tracing::trace!",
45        tracing_core::Level::DEBUG => "tracing::debug!",
46        tracing_core::Level::INFO => "tracing::info!",
47        tracing_core::Level::WARN => "tracing::warn!",
48        tracing_core::Level::ERROR => "tracing::error!",
49    }
50}
51
52/// Extracts the message and metadata from an event.
53fn extract_event_data(
54    event: &tracing_core::Event,
55    store_errors_in_values: bool,
56) -> (Option<String>, FieldVisitor) {
57    // Find message of the event, if any
58    let mut visitor = FieldVisitor {
59        store_errors_in_values,
60        ..Default::default()
61    };
62    event.record(&mut visitor);
63    let message = visitor
64        .json_values
65        .remove("message")
66        // When #[instrument(err)] is used the event does not have a message attached to it.
67        // the error message is attached to the field "error".
68        .or_else(|| visitor.json_values.remove("error"))
69        .and_then(|v| match v {
70            Value::String(s) => Some(s),
71            _ => None,
72        });
73
74    (message, visitor)
75}
76
77/// Extracts the message and metadata from an event, including the data in the current span.
78fn extract_event_data_with_context<S>(
79    event: &tracing_core::Event,
80    ctx: Option<&Context<S>>,
81    store_errors_in_values: bool,
82) -> (Option<String>, FieldVisitor)
83where
84    S: Subscriber + for<'a> LookupSpan<'a>,
85{
86    let (message, mut visitor) = extract_event_data(event, store_errors_in_values);
87
88    // Add the context fields of every parent span.
89    let current_span = ctx.as_ref().and_then(|ctx| {
90        event
91            .parent()
92            .and_then(|id| ctx.span(id))
93            .or_else(|| ctx.lookup_current())
94    });
95    if let Some(span) = current_span {
96        for span in span.scope() {
97            let name = span.name();
98            let ext = span.extensions();
99
100            if let Some(span_data) = ext.get::<SentrySpanData>() {
101                match &span_data.sentry_span {
102                    TransactionOrSpan::Span(span) => {
103                        for (key, value) in span.data().iter() {
104                            if is_sentry_span_attribute(key) {
105                                continue;
106                            }
107                            if key != "message" {
108                                let key = format!("{name}:{key}");
109                                visitor.json_values.insert(key, value.clone());
110                            }
111                        }
112                    }
113                    TransactionOrSpan::Transaction(transaction) => {
114                        for (key, value) in transaction.data().iter() {
115                            if is_sentry_span_attribute(key) {
116                                continue;
117                            }
118                            if key != "message" {
119                                let key = format!("{name}:{key}");
120                                visitor.json_values.insert(key, value.clone());
121                            }
122                        }
123                    }
124                }
125            }
126        }
127    }
128
129    (message, visitor)
130}
131
132/// Checks whether the given attribute name is one of those set on a span by the Sentry layer.
133/// In that case, we want to skip materializing it when propagating attributes, as it would mostly create noise.
134fn is_sentry_span_attribute(name: &str) -> bool {
135    matches!(
136        name,
137        "sentry.tracing.target" | "code.module.name" | "code.file.path" | "code.line.number"
138    )
139}
140
141/// Records the fields of a [`tracing_core::Event`].
142#[derive(Default)]
143pub(crate) struct FieldVisitor {
144    pub(crate) json_values: BTreeMap<String, Value>,
145    pub(crate) exceptions: Vec<Exception>,
146    /// If `true`, stringify and store errors in `self.json_values` under the original field name
147    /// else (default), convert to `Exception`s and store in `self.exceptions`.
148    store_errors_in_values: bool,
149}
150
151impl FieldVisitor {
152    fn record<T: Into<Value>>(&mut self, field: &Field, value: T) {
153        self.json_values
154            .insert(field.name().to_owned(), value.into());
155    }
156}
157
158impl Visit for FieldVisitor {
159    fn record_i64(&mut self, field: &Field, value: i64) {
160        self.record(field, value);
161    }
162
163    fn record_u64(&mut self, field: &Field, value: u64) {
164        self.record(field, value);
165    }
166
167    fn record_bool(&mut self, field: &Field, value: bool) {
168        self.record(field, value);
169    }
170
171    fn record_str(&mut self, field: &Field, value: &str) {
172        self.record(field, value);
173    }
174
175    fn record_error(&mut self, field: &Field, value: &(dyn Error + 'static)) {
176        let event = event_from_error(value);
177        if self.store_errors_in_values {
178            let error_chain = event
179                .exception
180                .iter()
181                .rev()
182                .filter_map(|x| x.value.as_ref().map(|v| format!("{}: {}", x.ty, *v)))
183                .collect::<Vec<String>>();
184            self.record(field, error_chain);
185        } else {
186            for exception in event.exception {
187                self.exceptions.push(exception);
188            }
189        }
190    }
191
192    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
193        self.record(field, format!("{value:?}"));
194    }
195}
196
197/// Creates a [`Breadcrumb`] from a given [`tracing_core::Event`].
198pub fn breadcrumb_from_event<'context, S>(
199    event: &tracing_core::Event,
200    ctx: impl Into<Option<&'context Context<'context, S>>>,
201) -> Breadcrumb
202where
203    S: Subscriber + for<'a> LookupSpan<'a>,
204{
205    let (message, visitor) = extract_event_data_with_context(event, ctx.into(), true);
206
207    Breadcrumb {
208        category: Some(event.metadata().target().to_owned()),
209        ty: "log".into(),
210        level: level_to_sentry_level(event.metadata().level()),
211        message,
212        data: visitor.json_values,
213        ..Default::default()
214    }
215}
216
217/// Convert `tracing` fields to the corresponding Sentry tags, removing them from `fields`.
218fn extract_and_remove_tags(fields: &mut BTreeMap<String, Value>) -> BTreeMap<String, String> {
219    let mut tags = BTreeMap::new();
220
221    fields.retain(|key, value| {
222        let Some(key) = key.strip_prefix(TAGS_PREFIX) else {
223            return true;
224        };
225        let string = match value {
226            Value::Bool(b) => b.to_string(),
227            Value::Number(n) => n.to_string(),
228            Value::String(s) => std::mem::take(s),
229            // remove null entries since empty tags are not allowed
230            Value::Null => return false,
231            // keep entries that cannot be represented as simple string
232            Value::Array(_) | Value::Object(_) => return true,
233        };
234
235        tags.insert(key.to_owned(), string);
236
237        false
238    });
239
240    tags
241}
242
243/// Create Sentry Contexts out of the `tracing` event and fields.
244fn contexts_from_event(
245    event: &tracing_core::Event,
246    fields: BTreeMap<String, Value>,
247) -> BTreeMap<String, sentry_core::protocol::Context> {
248    let event_meta = event.metadata();
249    let mut location_map = BTreeMap::new();
250    if let Some(module_path) = event_meta.module_path() {
251        location_map.insert("module_path".to_string(), module_path.into());
252    }
253    if let Some(file) = event_meta.file() {
254        location_map.insert("file".to_string(), file.into());
255    }
256    if let Some(line) = event_meta.line() {
257        location_map.insert("line".to_string(), line.into());
258    }
259
260    let mut context = BTreeMap::new();
261    if !fields.is_empty() {
262        context.insert(
263            "Rust Tracing Fields".to_string(),
264            sentry_core::protocol::Context::Other(fields),
265        );
266    }
267    if !location_map.is_empty() {
268        context.insert(
269            "Rust Tracing Location".to_string(),
270            sentry_core::protocol::Context::Other(location_map),
271        );
272    }
273    context
274}
275
276/// Creates an [`Event`] (possibly carrying exceptions) from a given [`tracing_core::Event`].
277pub fn event_from_event<'context, S>(
278    event: &tracing_core::Event,
279    ctx: impl Into<Option<&'context Context<'context, S>>>,
280) -> Event<'static>
281where
282    S: Subscriber + for<'a> LookupSpan<'a>,
283{
284    // Exception records in Sentry need a valid type, value and full stack trace to support
285    // proper grouping and issue metadata generation. tracing_core::Record does not contain sufficient
286    // information for this. However, it may contain a serialized error which we can parse to emit
287    // an exception record.
288    #[allow(unused_mut)]
289    let (mut message, visitor) = extract_event_data_with_context(event, ctx.into(), false);
290    let FieldVisitor {
291        mut exceptions,
292        mut json_values,
293        store_errors_in_values: _,
294    } = visitor;
295
296    // If there are a message, an exception, and we are capturing stack traces, then add the message
297    // as synthetic wrapper around the exception to support proper grouping. The stack trace to
298    // attach is the current one, since it points to the place where the exception is captured.
299    // We should only do this if we're capturing stack traces, otherwise the issue title will be `<unknown>`
300    // as Sentry will attempt to use missing stack trace to determine the title.
301    #[cfg(feature = "backtrace")]
302    if !exceptions.is_empty() && message.is_some() {
303        if let Some(client) = sentry_core::Hub::current().client() {
304            if client.options().attach_stacktrace {
305                let thread = sentry_backtrace::current_thread(true);
306                let exception = Exception {
307                    ty: level_to_exception_type(event.metadata().level()).to_owned(),
308                    value: message.take(),
309                    module: event.metadata().module_path().map(str::to_owned),
310                    stacktrace: thread.stacktrace,
311                    raw_stacktrace: thread.raw_stacktrace,
312                    thread_id: thread.id,
313                    mechanism: Some(Mechanism {
314                        synthetic: Some(true),
315                        ..Mechanism::default()
316                    }),
317                };
318                exceptions.push(exception)
319            }
320        }
321    }
322
323    if let Some(exception) = exceptions.last_mut() {
324        "tracing".clone_into(
325            &mut exception
326                .mechanism
327                .get_or_insert_with(Mechanism::default)
328                .ty,
329        );
330    }
331
332    Event {
333        logger: Some(event.metadata().target().to_owned()),
334        level: level_to_sentry_level(event.metadata().level()),
335        message,
336        exception: exceptions.into(),
337        tags: extract_and_remove_tags(&mut json_values),
338        contexts: contexts_from_event(event, json_values),
339        ..Default::default()
340    }
341}
342
343/// Creates a [`Log`] from a given [`tracing_core::Event`]
344#[cfg(feature = "logs")]
345pub fn log_from_event<'context, S>(
346    event: &tracing_core::Event,
347    ctx: impl Into<Option<&'context Context<'context, S>>>,
348) -> Log
349where
350    S: Subscriber + for<'a> LookupSpan<'a>,
351{
352    let (message, visitor) = extract_event_data_with_context(event, ctx.into(), true);
353
354    let mut attributes: BTreeMap<String, LogAttribute> = visitor
355        .json_values
356        .into_iter()
357        .map(|(key, val)| (key, val.into()))
358        .collect();
359
360    let event_meta = event.metadata();
361    if let Some(module_path) = event_meta.module_path() {
362        attributes.insert("code.module.name".to_owned(), module_path.into());
363    }
364    if let Some(file) = event_meta.file() {
365        attributes.insert("code.file.path".to_owned(), file.into());
366    }
367    if let Some(line) = event_meta.line() {
368        attributes.insert("code.line.number".to_owned(), line.into());
369    }
370
371    attributes.insert("sentry.origin".to_owned(), "auto.tracing".into());
372
373    Log {
374        level: level_to_log_level(event.metadata().level()),
375        body: message.unwrap_or_default(),
376        trace_id: None,
377        timestamp: SystemTime::now(),
378        severity_number: None,
379        attributes,
380    }
381}