-
Notifications
You must be signed in to change notification settings - Fork 745
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
Conversation
It turns out that when using the global dispatcher, emitting tracing events in a panic hook will capture the span in which the program panicked. This is very handy for debugging panics! Thanks a lot to @nate_mara for pointing this out to me on twitter --- I hadn't even thought of it! Since it isn't necessarily immediately obvious that this works, I thought it would be nice to add an example.
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.
woah, this is rad!
// 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| { |
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?
tracing::set_panic_hook()
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:
- Users might want to handle panics in different ways, with different formatting, levels, and so forth. They might not want to involve tracing in the first place!
- Other users might not want
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
. - Failing/panicking if
tracing::set_panic_hook
(or another panic hook) is way too opinionated fortracing
, IMO.
It turns out that when using the global dispatcher, emitting tracing events in a panic hook will capture the span in which the program panicked. This is very handy for debugging panics! Thanks a lot to @nate_mara for pointing this out to me on twitter --- I hadn't even thought of it! Since it isn't necessarily immediately obvious that this works, I thought it would be nice to add an example.
It turns out that when using the global dispatcher, emitting tracing events in a panic hook will capture the span in which the program panicked. This is very handy for debugging panics! Thanks a lot to @nate_mara for pointing this out to me on twitter --- I hadn't even thought of it! Since it isn't necessarily immediately obvious that this works, I thought it would be nice to add an example.
It turns out that when using the global dispatcher, emitting tracing
events in a panic hook will capture the span in which the program
panicked. This is very handy for debugging panics! Thanks a lot to
@natemara for pointing this out to me on twitter --- I hadn't even
thought of it!
Since it isn't necessarily immediately obvious that this works, I
thought it would be nice to add an example.