From 7baef238d5a58c7a41b115806940d02cedc451ff Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Wed, 11 Dec 2024 07:46:31 +0100 Subject: [PATCH] backend/tracing: add fields to span again for error spantrace --- nghe-backend/src/integration/informant.rs | 1 - nghe-backend/src/integration/spotify.rs | 1 + nghe-backend/src/lib.rs | 2 +- nghe-backend/src/scan/scanner.rs | 8 +++--- nghe-backend/src/transcode/transcoder.rs | 9 +++---- nghe-proc-macro/src/backend/handler.rs | 33 +++++++++-------------- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/nghe-backend/src/integration/informant.rs b/nghe-backend/src/integration/informant.rs index 1860b55cb..6aca33dc8 100644 --- a/nghe-backend/src/integration/informant.rs +++ b/nghe-backend/src/integration/informant.rs @@ -89,7 +89,6 @@ impl Informant { None }; - tracing::debug!(?spotify); self.upsert_artist(database, config, id, spotify.as_ref()).await } diff --git a/nghe-backend/src/integration/spotify.rs b/nghe-backend/src/integration/spotify.rs index fe7d1ecd9..b723cf7b6 100644 --- a/nghe-backend/src/integration/spotify.rs +++ b/nghe-backend/src/integration/spotify.rs @@ -40,6 +40,7 @@ impl Client { } } + #[tracing::instrument(skip_all, name = "spotify:search_artist", ret(level = "debug"))] pub async fn search_artist(&self, name: &str) -> Result, Error> { Ok( if let SearchResult::Artists(artists) = diff --git a/nghe-backend/src/lib.rs b/nghe-backend/src/lib.rs index b8bbf8097..56ad2c27d 100644 --- a/nghe-backend/src/lib.rs +++ b/nghe-backend/src/lib.rs @@ -63,7 +63,7 @@ pub fn init_tracing() -> Result<(), Error> { if cfg!(test) { tracing.with(tracing_subscriber::fmt::layer().with_test_writer()).try_init()?; } else { - tracing.with(tracing_subscriber::fmt::layer().with_target(false).compact()).try_init()?; + tracing.with(tracing_subscriber::fmt::layer().with_target(false)).try_init()?; } Ok(()) diff --git a/nghe-backend/src/scan/scanner.rs b/nghe-backend/src/scan/scanner.rs index 37a333492..13443692c 100644 --- a/nghe-backend/src/scan/scanner.rs +++ b/nghe-backend/src/scan/scanner.rs @@ -237,16 +237,16 @@ impl<'db, 'fs, 'mf> Scanner<'db, 'fs, 'mf> { Ok(()) } - #[instrument(skip_all, err(Debug))] + #[instrument(skip_all, fields(started_at), err(Debug))] pub async fn run(&self) -> Result<(), Error> { - tracing::info!(music_folder = ?self.music_folder); + let span = tracing::Span::current(); let started_at = crate::time::now().await; - tracing::info!(?started_at); + span.record("started_at", tracing::field::display(&started_at)); + tracing::info!(music_folder = ?self.music_folder); let (scan_handle, permit, rx) = self.init(); let mut join_set = tokio::task::JoinSet::new(); - let span = tracing::Span::current(); while let Ok(entry) = rx.recv_async().await { let permit = permit.clone().acquire_owned().await?; let scan = self.clone().into_owned(); diff --git a/nghe-backend/src/transcode/transcoder.rs b/nghe-backend/src/transcode/transcoder.rs index db3f6b7e6..b621709cf 100644 --- a/nghe-backend/src/transcode/transcoder.rs +++ b/nghe-backend/src/transcode/transcoder.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::ffi::{CStr, CString}; -use std::fmt::Display; +use std::fmt::Debug; use concat_string::concat_string; use rsmpeg::avcodec::{AVCodec, AVCodecContext}; @@ -219,14 +219,13 @@ impl<'graph> Filter<'graph> { } impl Transcoder { - #[instrument(skip_all, err(Debug))] + #[instrument(err(Debug))] pub fn spawn( - input: impl Into + Display, + input: impl Into + Debug, sink: Sink, bitrate: u32, offset: u32, ) -> Result>, Error> { - tracing::debug!(%input, ?sink, %bitrate, %offset); let mut transcoder = Self::new(&CString::new(input.into())?, sink, bitrate, offset)?; let span = tracing::Span::current(); @@ -296,7 +295,7 @@ mod test { impl Transcoder { pub async fn spawn_collect( - input: impl Into + Display, + input: impl Into + Debug, config: &config::Transcode, format: format::Transcode, bitrate: u32, diff --git a/nghe-proc-macro/src/backend/handler.rs b/nghe-proc-macro/src/backend/handler.rs index 5c401af0e..79db9f643 100644 --- a/nghe-proc-macro/src/backend/handler.rs +++ b/nghe-proc-macro/src/backend/handler.rs @@ -106,14 +106,16 @@ impl Arg { } } - fn to_tracing(&self) -> Option { + fn to_skip_debug(&self) -> Option<&syn::Ident> { match self { - Arg::User(ident) => { - let field = format_ident!("user_{ident}"); - Some(parse_quote!(user.#ident = ?#field)) + Arg::Database { ident, use_database } => { + if *use_database { + Some(ident) + } else { + None + } } - Arg::Header { ident, .. } => Some(parse_quote!(#ident = ?#ident)), - Arg::Request => Some(parse_quote!(request = ?request)), + Arg::Extension { ident, .. } => Some(ident), _ => None, } } @@ -345,24 +347,18 @@ impl Handler { }) .try_collect()?; + let skip_debugs: Punctuated<&syn::Ident, syn::Token![,]> = + self.args.value.iter().filter_map(Arg::to_skip_debug).collect(); let mut tracing_args = Punctuated::::default(); tracing_args.push(parse_quote!(name = #tracing_name)); - tracing_args.push(parse_quote!(skip_all)); + tracing_args.push(parse_quote!(skip(#skip_debugs))); if self.is_result_binary.is_some() { tracing_args.push(parse_quote!(ret(level = "trace"))); tracing_args.push(parse_quote!(err(Debug))); } - let traced_args: Punctuated = - self.args.value.iter().filter_map(Arg::to_tracing).collect(); - let traced_expr_macro: Option = if traced_args.is_empty() { - None - } else { - Some(quote!(tracing::debug!(#traced_args);)) - }; - let handler_ident = &self.item.sig.ident; - let handler_expr_call: syn::Expr = if sig.asyncness.is_some() { + let source: syn::Expr = if sig.asyncness.is_some() { parse_quote!(#handler_ident(#args).await) } else { parse_quote!(#handler_ident(#args)) @@ -372,10 +368,7 @@ impl Handler { #[coverage(off)] #[inline(always)] #[tracing::instrument(#tracing_args)] - #sig { - #traced_expr_macro - #handler_expr_call - } + #sig { #source } }) }