diff --git a/src/conn/routines/exec.rs b/src/conn/routines/exec.rs index 81e7e61b..262a90c9 100644 --- a/src/conn/routines/exec.rs +++ b/src/conn/routines/exec.rs @@ -4,7 +4,7 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use mysql_common::{packets::ComStmtExecuteRequestBuilder, params::Params}; #[cfg(feature = "tracing")] -use tracing::{field, info_span, Instrument, Level, Span}; +use tracing::{field, info_span, Level, Span}; use crate::{BinaryProtocol, Conn, DriverError, Statement}; @@ -104,7 +104,7 @@ impl Routine<()> for ExecRoutine<'_> { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/conn/routines/next_set.rs b/src/conn/routines/next_set.rs index 2f381cff..ecb2784a 100644 --- a/src/conn/routines/next_set.rs +++ b/src/conn/routines/next_set.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use futures_core::future::BoxFuture; use futures_util::FutureExt; #[cfg(feature = "tracing")] -use tracing::{debug_span, Instrument}; +use tracing::debug_span; use crate::{queryable::Protocol, Conn}; @@ -36,7 +36,7 @@ where }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/conn/routines/ping.rs b/src/conn/routines/ping.rs index e6d7910f..5f9d017e 100644 --- a/src/conn/routines/ping.rs +++ b/src/conn/routines/ping.rs @@ -2,7 +2,7 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use mysql_common::constants::Command; #[cfg(feature = "tracing")] -use tracing::{debug_span, Instrument}; +use tracing::debug_span; use crate::Conn; @@ -24,7 +24,7 @@ impl Routine<()> for PingRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/conn/routines/prepare.rs b/src/conn/routines/prepare.rs index 3e41bf93..33970e58 100644 --- a/src/conn/routines/prepare.rs +++ b/src/conn/routines/prepare.rs @@ -4,7 +4,7 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use mysql_common::constants::Command; #[cfg(feature = "tracing")] -use tracing::{field, info_span, Instrument, Level, Span}; +use tracing::{field, info_span, Level, Span}; use crate::{queryable::stmt::StmtInner, Conn}; @@ -48,6 +48,7 @@ impl Routine> for PrepareRoutine { let packet = conn.read_packet().await?; let mut inner_stmt = StmtInner::from_payload(&*packet, conn.id(), self.query.clone())?; + #[cfg(feature = "tracing")] Span::current().record("mysql_async.statement.id", inner_stmt.id()); @@ -65,7 +66,7 @@ impl Routine> for PrepareRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/conn/routines/query.rs b/src/conn/routines/query.rs index 60e5f4bf..a82864f6 100644 --- a/src/conn/routines/query.rs +++ b/src/conn/routines/query.rs @@ -4,7 +4,7 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use mysql_common::constants::Command; #[cfg(feature = "tracing")] -use tracing::{field, span_enabled, Instrument, Level}; +use tracing::{field, span_enabled, Level}; use crate::tracing_utils::TracingLevel; use crate::{Conn, TextProtocol}; @@ -54,7 +54,7 @@ impl Routine<()> for QueryRoutine<'_, L> { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/conn/routines/reset.rs b/src/conn/routines/reset.rs index f48e9ef3..b52beb8d 100644 --- a/src/conn/routines/reset.rs +++ b/src/conn/routines/reset.rs @@ -2,7 +2,7 @@ use futures_core::future::BoxFuture; use futures_util::FutureExt; use mysql_common::constants::Command; #[cfg(feature = "tracing")] -use tracing::{debug_span, Instrument}; +use tracing::debug_span; use crate::Conn; @@ -25,7 +25,7 @@ impl Routine<()> for ResetRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = instrument_result!(fut, span); fut.boxed() } diff --git a/src/tracing_utils.rs b/src/tracing_utils.rs index 4321fbb0..b32170c0 100644 --- a/src/tracing_utils.rs +++ b/src/tracing_utils.rs @@ -38,3 +38,16 @@ macro_rules! create_span { } } } + +#[cfg(feature = "tracing")] +macro_rules! instrument_result { + ($fut:expr, $span:expr) => {{ + let fut = async { + $fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + }; + <_ as tracing::Instrument>::instrument(fut, $span) + }}; +}