Skip to content

feat(tracing): Use a backtrace field as the error event's stack trace - #1447

Closed
sentry-junior[bot] wants to merge 3 commits into
masterfrom
feat/tracing-backtrace-field
Closed

sentry-junior[bot] wants to merge 3 commits into
masterfrom
feat/tracing-backtrace-field

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Proof of concept.

Today the tracing layer can only attach the stack of the tracing::error! call site (with attach_stacktrace(true), as a thread stack on message events or a synthetic exception when an error field is recorded). A recorded &dyn Error can't expose its backtrace on stable Rust (Error::provide is unstable, rust-lang/rust#99301). When an error is logged away from where it was created, such as in a framework's error handler, the call-site stack doesn't show where the error happened.

With the backtrace feature, an error event's backtrace field, holding a formatted std::backtrace::Backtrace, now becomes the stack trace of the outermost recorded error. When the event records no error, the SDK makes an exception (type tracing::error!, value = the message) with that stack trace. The field is removed from the event's tracing fields once it parses.

Laravel for Rust's error handler logs failures in laravel_core::errors::report::log with only type, location, and causes string fields, so Sentry gets a message event. laravel::Error already captures a backtrace through anyhow (Error::backtrace(), set when RUST_BACKTRACE or RUST_LIB_BACKTRACE is on). Adding backtrace = error.backtrace().map(tracing::field::display) to that log line would give those failures the stack from where the error was created.

Open decisions:

  • Field name: backtrace is generic, so existing backtrace string fields in apps would start becoming stack traces.
  • Whether to use another field, such as Laravel's type, as the exception type instead of tracing::error!.
  • The Laravel side needs a change in laravel/rust-framework; this PR only adds the SDK half.

via @szokeasaurusrex.

--

View Session [Sentry]

The `tracing` layer can only attach the stack of the `tracing::error!` call site (with `attach_stacktrace`), and a recorded `&dyn Error` can't expose its backtrace on stable Rust. When an error is logged away from where it was created, such as in a framework's error handler, that stack trace doesn't show where the error happened.

With the `backtrace` feature, an error event's `backtrace` field, holding a formatted `std::backtrace::Backtrace`, now becomes the stack trace of the outermost recorded error. When the event records no error, the SDK makes an exception from the event's message with that stack trace. The field is removed from the event's tracing fields once it parses.

This would let Laravel for Rust report failures with the backtrace that `laravel::Error::backtrace()` already captures, by adding `backtrace = error.backtrace().map(tracing::field::display)` to the log line in `laravel_core::errors::report::log`.

Co-Authored-By: Daniel Szoke <[email protected]>
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 193c761

Comment on lines +348 to +359
if let Some(stacktrace) = take_backtrace(&mut json_values) {
if let Some(exception) = exceptions.last_mut() {
exception.stacktrace = Some(stacktrace);
} else {
exceptions.push(Exception {
ty: level_to_exception_type(event.metadata().level()).to_owned(),
value: message.take(),
module: event.metadata().module_path().map(str::to_owned),
stacktrace: Some(stacktrace),
..Exception::default()
});
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated README omits new backtrace field docs

src/lib.rs crate docs now describe the backtrace feature and field, but sentry-tracing/README.md was not regenerated and still ends the Errors section without that guidance.

Evidence
  • take_backtrace(&mut json_values) in event_from_event makes a formatted backtrace field the outermost exception stack trace (or synthesizes an exception from the message).
  • sentry-tracing/src/lib.rs documents this under ## Stack traces from where an error was created (feature gate, field, outermost-error/message behavior, example).
  • sentry-tracing/README.md Errors section stops after the ErrorLayer setup example and has no backtrace subsection.
  • Crate READMEs are produced from crate rustdoc via scripts/generate-readme.sh (cargo readme), so this change leaves the published README stale.
Also found at 1 additional location
  • sentry-tracing/src/lib.rs:130-142

Identified by Warden · docs-review · 3CJ-6QW

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated sentry-tracing/README.md with the new subsection, matching the cargo readme output format, in 193c7619.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant