From 122b5b4e6c3eeba12384b6ac55cf75bab915ef20 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 24 Oct 2024 15:40:04 +0200 Subject: [PATCH] Send server panics to Axiom Co-Authored-By: Thorsten Co-Authored-By: Bennet Co-Authored-By: Kirill --- crates/collab/src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/collab/src/main.rs b/crates/collab/src/main.rs index ee95b6d41f..9e5c3dd048 100644 --- a/crates/collab/src/main.rs +++ b/crates/collab/src/main.rs @@ -84,6 +84,8 @@ async fn main() -> Result<()> { let config = envy::from_env::().expect("error loading config"); init_tracing(&config); + init_panic_hook(); + let mut app = Router::new() .route("/", get(handle_root)) .route("/healthz", get(handle_liveness_probe)) @@ -378,3 +380,20 @@ pub fn init_tracing(config: &Config) -> Option<()> { None } + +fn init_panic_hook() { + std::panic::set_hook(Box::new(move |panic_info| { + let panic_message = match panic_info.payload().downcast_ref::<&'static str>() { + Some(message) => *message, + None => match panic_info.payload().downcast_ref::() { + Some(message) => message.as_str(), + None => "Box", + }, + }; + let backtrace = std::backtrace::Backtrace::force_capture(); + let location = panic_info + .location() + .map(|loc| format!("{}:{}", loc.file(), loc.line())); + tracing::error!(panic = true, ?location, %panic_message, %backtrace, "Server Panic"); + })); +}