-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tracing: add an example of tracing
in a panic hook
#1375
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! This example demonstrates how `tracing` events can be recorded from within a | ||
//! panic hook, capturing the span context in which the program panicked. | ||
|
||
fn main() { | ||
let collector = tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::TRACE) | ||
.finish(); | ||
|
||
// NOTE: Using `tracing` in a panic hook requires the use of the *global* | ||
// trace dispatcher (`tracing::collect::set_global_default`), rather than | ||
// the per-thread scoped dispatcher | ||
// (`tracing::collect::with_default`/`set_default`). With the scoped trace | ||
// dispatcher, the collector's thread-local context may already have been | ||
// torn down by unwinding by the time the panic handler is reached. | ||
tracing::collect::set_global_default(collector).unwrap(); | ||
|
||
// Set a panic hook that records the panic as a `tracing` event at the | ||
// `ERROR` verbosity level. | ||
// | ||
// If we are currently in a span when the panic occurred, the logged event | ||
// will include the current span, allowing the context in which the panic | ||
// occurred to be recorded. | ||
std::panic::set_hook(Box::new(|panic| { | ||
// If the panic has a source location, record it as structured fields. | ||
if let Some(location) = panic.location() { | ||
// On nightly Rust, where the `PanicInfo` type also exposes a | ||
// `message()` method returning just the message, we could record | ||
// just the message instead of the entire `fmt::Display` | ||
// implementation, avoiding the duplciated location | ||
tracing::error!( | ||
message = %panic, | ||
panic.file = location.file(), | ||
panic.line = location.line(), | ||
panic.column = location.column(), | ||
); | ||
} else { | ||
tracing::error!(message = %panic); | ||
} | ||
})); | ||
|
||
for i in 0..10 { | ||
check_number(i); | ||
} | ||
} | ||
|
||
#[tracing::instrument] | ||
fn check_number(x: i32) { | ||
if x % 2 == 0 { | ||
panic!("I don't work with even numbers!"); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this built-in?
and could fail if no global one is set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, might be worth a follow-up change to add something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest opening an issue to centralize discussion: https://github.com/tokio-rs/tracing/issues/new?assignees=&labels=&template=feature_request.md.
However, I'm personally not inclined to have this in
tracing
for the following reasons:tracing
to change their panic hooks or they might wanttracing
and some other functionality in their panic hooks. If the data passed to the panic hook is paramaterized, then I'm not sure what the benefit of atracing
-vended panic hook is overstd:panic::set_hook
.tracing::set_panic_hook
(or another panic hook) is way too opinionated fortracing
, IMO.