Skip to content
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 2 commits into from Apr 28, 2021
Merged

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Apr 27, 2021

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.

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.
@hawkw hawkw requested review from davidbarsky and a team as code owners April 27, 2021 17:26
Copy link
Member

@davidbarsky davidbarsky left a 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| {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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 want tracing 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 a tracing-vended panic hook is over std:panic::set_hook.
  • Failing/panicking if tracing::set_panic_hook (or another panic hook) is way too opinionated for tracing, IMO.

@hawkw hawkw merged commit 7fc6a06 into master Apr 28, 2021
@hawkw hawkw deleted the eliza/panic-hook branch April 28, 2021 22:15
hawkw added a commit that referenced this pull request Apr 30, 2021
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.
@hawkw hawkw mentioned this pull request Apr 30, 2021
hawkw added a commit that referenced this pull request Apr 30, 2021
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.
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.

None yet

3 participants