sentry-rust icon indicating copy to clipboard operation
sentry-rust copied to clipboard

Use `anyhow` backtrace if available when creating event out of `tracing` event

Open lcian opened this issue 6 months ago • 1 comments

Problem Statement

When attaching an anyhow as a tracing field, we don't use the Backtrace from it, while it would be more valuable to use it.

As an example:

                capture_anyhow(&e);
                tracing::error!(
                    error = e.into_boxed_dyn_error(),
                    "error doing something"
                );

The stacktrace obtained with capture_anyhow will be up to the place where the error was first created. Instead, the tracing backtrace will just indicate where the call to tracing was made which is way less useful.

This might be an improvement we can make in other places too.

Solution Brainstorm

We should still be able to retrieve the Backtrace even after we cast to boxed dyn error. From the anyhow docs:

let boxed_dyn_error = anyhow_error.into_boxed_dyn_error();
assert!(std::error::request_ref::<Backtrace>(&*boxed_dyn_error).is_some());  // has Backtrace

Create a special case to check if the Backtrace is available and use that one instead of thread::current_stacktrace().

lcian avatar Jul 08 '25 12:07 lcian

@lcian is uncertain whether this is possible to do, but we should try it

szokeasaurusrex avatar Oct 23 '25 09:10 szokeasaurusrex