Skip to content

Commit e9661c7

Browse files
authored
fix(tracing): wrap error with synthetic mechanism only if attaching stacktrace (#755)
1 parent 3b73aac commit e9661c7

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

sentry-tracing/src/converters.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22
use std::error::Error;
33

4-
use sentry_core::protocol::{Event, Exception, Mechanism, Thread, Value};
4+
use sentry_core::protocol::{Event, Exception, Mechanism, Value};
55
use sentry_core::{event_from_error, Breadcrumb, Level, TransactionOrSpan};
66
use tracing_core::field::{Field, Visit};
77
use tracing_core::Subscriber;
@@ -21,6 +21,7 @@ fn convert_tracing_level(level: &tracing_core::Level) -> Level {
2121
}
2222
}
2323

24+
#[allow(unused)]
2425
fn level_to_exception_type(level: &tracing_core::Level) -> &'static str {
2526
match *level {
2627
tracing_core::Level::TRACE => "tracing::trace!",
@@ -248,41 +249,38 @@ where
248249
// proper grouping and issue metadata generation. tracing_core::Record does not contain sufficient
249250
// information for this. However, it may contain a serialized error which we can parse to emit
250251
// an exception record.
252+
#[allow(unused_mut)]
251253
let (mut message, visitor) = extract_event_data_with_context(event, ctx.into());
252254
let FieldVisitor {
253255
mut exceptions,
254256
mut json_values,
255257
} = visitor;
256258

257-
// If there are both a message and an exception, then add the message as synthetic wrapper
258-
// around the exception to support proper grouping. If configured, also add the current stack
259-
// trace to this exception directly, since it points to the place where the exception is
260-
// captured.
259+
// If there are a message, an exception, and we are capturing stack traces, then add the message
260+
// as synthetic wrapper around the exception to support proper grouping. The stack trace to
261+
// attach is the current one, since it points to the place where the exception is captured.
262+
// We should only do this if we're capturing stack traces, otherwise the issue title will be `<unknown>`
263+
// as Sentry will attempt to use missing stack trace to determine the title.
264+
#[cfg(feature = "backtrace")]
261265
if !exceptions.is_empty() && message.is_some() {
262-
#[allow(unused_mut)]
263-
let mut thread = Thread::default();
264-
265-
#[cfg(feature = "backtrace")]
266266
if let Some(client) = sentry_core::Hub::current().client() {
267267
if client.options().attach_stacktrace {
268-
thread = sentry_backtrace::current_thread(true);
268+
let thread = sentry_backtrace::current_thread(true);
269+
let exception = Exception {
270+
ty: level_to_exception_type(event.metadata().level()).to_owned(),
271+
value: message.take(),
272+
module: event.metadata().module_path().map(str::to_owned),
273+
stacktrace: thread.stacktrace,
274+
raw_stacktrace: thread.raw_stacktrace,
275+
thread_id: thread.id,
276+
mechanism: Some(Mechanism {
277+
synthetic: Some(true),
278+
..Mechanism::default()
279+
}),
280+
};
281+
exceptions.push(exception)
269282
}
270283
}
271-
272-
let exception = Exception {
273-
ty: level_to_exception_type(event.metadata().level()).to_owned(),
274-
value: message.take(),
275-
module: event.metadata().module_path().map(str::to_owned),
276-
stacktrace: thread.stacktrace,
277-
raw_stacktrace: thread.raw_stacktrace,
278-
thread_id: thread.id,
279-
mechanism: Some(Mechanism {
280-
synthetic: Some(true),
281-
..Mechanism::default()
282-
}),
283-
};
284-
285-
exceptions.push(exception);
286284
}
287285

288286
if let Some(exception) = exceptions.last_mut() {

0 commit comments

Comments
 (0)