Repository navigation
feat(tracing): Use a backtrace field as the error event's stack trace - #1447
Closed
sentry-junior[bot] wants to merge 3 commits into
Closed
sentry-junior[bot] wants to merge 3 commits into
sentry-junior[bot] wants to merge 3 commits into
Conversation
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]>
|
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() | ||
| }); | ||
| } |
There was a problem hiding this comment.
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)inevent_from_eventmakes a formattedbacktracefield the outermost exception stack trace (or synthesizes an exception from the message).sentry-tracing/src/lib.rsdocuments this under## Stack traces from where an error was created(feature gate, field, outermost-error/message behavior, example).sentry-tracing/README.mdErrors section stops after theErrorLayersetup 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
Contributor
Author
There was a problem hiding this comment.
Updated sentry-tracing/README.md with the new subsection, matching the cargo readme output format, in 193c7619.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proof of concept.
Today the
tracinglayer can only attach the stack of thetracing::error!call site (withattach_stacktrace(true), as a thread stack on message events or a synthetic exception when an error field is recorded). A recorded&dyn Errorcan't expose its backtrace on stable Rust (Error::provideis 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
backtracefeature, an error event'sbacktracefield, holding a formattedstd::backtrace::Backtrace, now becomes the stack trace of the outermost recorded error. When the event records no error, the SDK makes an exception (typetracing::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::logwith onlytype,location, andcausesstring fields, so Sentry gets a message event.laravel::Erroralready captures a backtrace through anyhow (Error::backtrace(), set whenRUST_BACKTRACEorRUST_LIB_BACKTRACEis on). Addingbacktrace = error.backtrace().map(tracing::field::display)to that log line would give those failures the stack from where the error was created.Open decisions:
backtraceis generic, so existingbacktracestring fields in apps would start becoming stack traces.type, as the exception type instead oftracing::error!.via @szokeasaurusrex.
--
View Session [Sentry]