Skip to content

Commit

Permalink
backend/tracing: add fields to span again for error spantrace
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Dec 11, 2024
1 parent 033a002 commit 7baef23
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
1 change: 0 additions & 1 deletion nghe-backend/src/integration/informant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl Informant {
None
};

tracing::debug!(?spotify);
self.upsert_artist(database, config, id, spotify.as_ref()).await
}

Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/integration/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Artist>, Error> {
Ok(
if let SearchResult::Artists(artists) =
Expand Down
2 changes: 1 addition & 1 deletion nghe-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions nghe-backend/src/scan/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions nghe-backend/src/transcode/transcoder.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -219,14 +219,13 @@ impl<'graph> Filter<'graph> {
}

impl Transcoder {
#[instrument(skip_all, err(Debug))]
#[instrument(err(Debug))]
pub fn spawn(
input: impl Into<String> + Display,
input: impl Into<String> + Debug,
sink: Sink,
bitrate: u32,
offset: u32,
) -> Result<tokio::task::JoinHandle<Result<(), Error>>, Error> {
tracing::debug!(%input, ?sink, %bitrate, %offset);
let mut transcoder = Self::new(&CString::new(input.into())?, sink, bitrate, offset)?;

let span = tracing::Span::current();
Expand Down Expand Up @@ -296,7 +295,7 @@ mod test {

impl Transcoder {
pub async fn spawn_collect(
input: impl Into<String> + Display,
input: impl Into<String> + Debug,
config: &config::Transcode,
format: format::Transcode,
bitrate: u32,
Expand Down
33 changes: 13 additions & 20 deletions nghe-proc-macro/src/backend/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ impl Arg {
}
}

fn to_tracing(&self) -> Option<TokenStream> {
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,
}
}
Expand Down Expand Up @@ -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::<syn::Meta, syn::Token![,]>::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<TokenStream, syn::Token![,]> =
self.args.value.iter().filter_map(Arg::to_tracing).collect();
let traced_expr_macro: Option<TokenStream> = 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))
Expand All @@ -372,10 +368,7 @@ impl Handler {
#[coverage(off)]
#[inline(always)]
#[tracing::instrument(#tracing_args)]
#sig {
#traced_expr_macro
#handler_expr_call
}
#sig { #source }
})
}

Expand Down

0 comments on commit 7baef23

Please sign in to comment.