diff --git a/src/conn/routines/exec.rs b/src/conn/routines/exec.rs index 22864737..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,13 +104,7 @@ impl Routine<()> for ExecRoutine<'_> { }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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 425c1a4c..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,13 +36,7 @@ where }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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 c4141ff5..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,13 +24,7 @@ impl Routine<()> for PingRoutine { }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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 f38bd381..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}; @@ -66,13 +66,7 @@ impl Routine> for PrepareRoutine { }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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 6b53f224..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,13 +54,7 @@ impl Routine<()> for QueryRoutine<'_, L> { }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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 bd3e0d7d..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,13 +25,7 @@ impl Routine<()> for ResetRoutine { }; #[cfg(feature = "tracing")] - let fut = async { - fut.await.or_else(|e| { - tracing::error!(error = %e); - Err(e) - }) - } - .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) + }}; +}