-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[ENH] add panics to tracing #2382
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,28 @@ pub(crate) fn init_otel_tracing(service_name: &String, otel_endpoint: &String) { | |
tracing::subscriber::set_global_default(subscriber) | ||
.expect("Set global default subscriber failed"); | ||
println!("Set global subscriber for {}", service_name); | ||
|
||
// Add panics to tracing | ||
let prev_hook = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |panic_info| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for my learning: does this panic hook get set system wide after this call? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it's a global hook for all threads (https://doc.rust-lang.org/std/panic/fn.set_hook.html) |
||
let payload = panic_info.payload(); | ||
|
||
#[allow(clippy::manual_map)] | ||
let payload = if let Some(s) = payload.downcast_ref::<&str>() { | ||
Some(&**s) | ||
} else if let Some(s) = payload.downcast_ref::<String>() { | ||
Some(s.as_str()) | ||
} else { | ||
None | ||
}; | ||
|
||
tracing::error!( | ||
panic.payload = payload, | ||
panic.location = panic_info.location().map(|l| l.to_string()), | ||
panic.backtrace = tracing::field::display(std::backtrace::Backtrace::capture()), | ||
"A panic occurred" | ||
); | ||
|
||
prev_hook(panic_info); | ||
})); | ||
} |
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.
makes incremental builds slightly faster