From 32071e1833bade23308bd733ac3590047c8a526a Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 27 Feb 2025 10:24:28 +0100 Subject: [PATCH] Make sure tracing spans are propagated in `tokio::task::spawn` --- crates/syn2mas/src/mas_writer/mod.rs | 29 +++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/syn2mas/src/mas_writer/mod.rs b/crates/syn2mas/src/mas_writer/mod.rs index 8193047f3..6fd9e5df4 100644 --- a/crates/syn2mas/src/mas_writer/mod.rs +++ b/crates/syn2mas/src/mas_writer/mod.rs @@ -22,7 +22,7 @@ use sqlx::{Executor, PgConnection, query, query_as}; use thiserror::Error; use thiserror_ext::{Construct, ContextInto}; use tokio::sync::mpsc::{self, Receiver, Sender}; -use tracing::{Level, error, info, warn}; +use tracing::{Instrument, Level, error, info, warn}; use uuid::{NonNilUuid, Uuid}; use self::{ @@ -119,18 +119,21 @@ impl WriterConnectionPool { match self.connection_rx.recv().await { Some(Ok(mut connection)) => { let connection_tx = self.connection_tx.clone(); - tokio::task::spawn(async move { - let to_return = match task(&mut connection).await { - Ok(()) => Ok(connection), - Err(error) => { - error!("error in writer: {error}"); - Err(error) - } - }; - // This should always succeed in sending unless we're already shutting - // down for some other reason. - let _: Result<_, _> = connection_tx.send(to_return).await; - }); + tokio::task::spawn( + async move { + let to_return = match task(&mut connection).await { + Ok(()) => Ok(connection), + Err(error) => { + error!("error in writer: {error}"); + Err(error) + } + }; + // This should always succeed in sending unless we're already shutting + // down for some other reason. + let _: Result<_, _> = connection_tx.send(to_return).await; + } + .instrument(tracing::debug_span!("spawn_with_connection")), + ); Ok(()) }