From d28135f7ca88d706ef424674ee0cf0145b3dcc9a Mon Sep 17 00:00:00 2001 From: Olivia Lee Date: Fri, 13 Dec 2024 22:30:48 -0800 Subject: [PATCH] add helper macros to log error source chains The specific thing that prompted this is that Error::Reqwest only logs the Display impl of reqwest::Error, while the actual information you need to determine what went wrong is usually buried deeper in the source chain. This makes debugging federation networking issues super frustrating. Instead of just fixing this one case, let's just log the source chains everywhere. What could go wrong? --- src/observability/prelude.rs | 113 +++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/src/observability/prelude.rs b/src/observability/prelude.rs index a991e8f7..80e328f6 100644 --- a/src/observability/prelude.rs +++ b/src/observability/prelude.rs @@ -11,3 +11,116 @@ pub(crate) use tracing as t; pub(crate) use tracing::Instrument; + +/// [`tracing::event!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +#[allow(unused)] +macro_rules! event_err { + ($level:expr, $error:expr, $($rest:tt)+) => { + ::tracing::event!( + $level, + error=%::wee_woo::ErrorExt::display_with_sources(&$error, " -> "), + $($rest)+ + ) + } +} + +/// [`tracing::trace!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +macro_rules! trace_err { + ($error:expr, $($rest:tt)+) => { + $crate::observability::prelude::event_err!( + ::tracing::Level::TRACE, + $error, + $($rest)+ + ) + } +} + +/// [`tracing::debug!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +macro_rules! debug_err { + ($error:expr, $($rest:tt)+) => { + $crate::observability::prelude::event_err!( + ::tracing::Level::DEBUG, + $error, + $($rest)+ + ) + } +} + +/// [`tracing::info!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +macro_rules! info_err { + ($error:expr, $($rest:tt)+) => { + $crate::observability::prelude::event_err!( + ::tracing::Level::INFO, + $error, + $($rest)+ + ) + } +} + +/// [`tracing::warn!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +macro_rules! warn_err { + ($error:expr, $($rest:tt)+) => { + $crate::observability::prelude::event_err!( + ::tracing::Level::WARN, + $error, + $($rest)+ + ) + } +} + +/// [`tracing::error!`] but takes an error and formats its source chain as an +/// `error` field. +/// +/// The `parent:` and `target:` arguments from the original macro are not +/// supported. +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +macro_rules! error_err { + ($error:expr, $($rest:tt)+) => { + $crate::observability::prelude::event_err!( + ::tracing::Level::ERROR, + $error, + $($rest)+ + ) + } +} + +// Allowed because all of these variants exist for completeness, even if they +// aren't currently used. +#[allow(unused)] +pub(crate) use { + debug_err, error_err, event_err, info_err, trace_err, warn_err, +};