From c851de74a6746f881ff180c8ae95e800d72e23e2 Mon Sep 17 00:00:00 2001 From: Folke Behrens Date: Sat, 11 Feb 2023 23:00:25 +0100 Subject: [PATCH 1/2] Record errors from routines in tracing spans Updates #223 --- src/conn/routines/exec.rs | 8 +++++++- src/conn/routines/next_set.rs | 8 +++++++- src/conn/routines/ping.rs | 8 +++++++- src/conn/routines/prepare.rs | 9 ++++++++- src/conn/routines/query.rs | 8 +++++++- src/conn/routines/reset.rs | 8 +++++++- 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/conn/routines/exec.rs b/src/conn/routines/exec.rs index 81e7e61b..22864737 100644 --- a/src/conn/routines/exec.rs +++ b/src/conn/routines/exec.rs @@ -104,7 +104,13 @@ impl Routine<()> for ExecRoutine<'_> { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } diff --git a/src/conn/routines/next_set.rs b/src/conn/routines/next_set.rs index 2f381cff..425c1a4c 100644 --- a/src/conn/routines/next_set.rs +++ b/src/conn/routines/next_set.rs @@ -36,7 +36,13 @@ where }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } diff --git a/src/conn/routines/ping.rs b/src/conn/routines/ping.rs index e6d7910f..c4141ff5 100644 --- a/src/conn/routines/ping.rs +++ b/src/conn/routines/ping.rs @@ -24,7 +24,13 @@ impl Routine<()> for PingRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } diff --git a/src/conn/routines/prepare.rs b/src/conn/routines/prepare.rs index 3e41bf93..f38bd381 100644 --- a/src/conn/routines/prepare.rs +++ b/src/conn/routines/prepare.rs @@ -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,13 @@ impl Routine> for PrepareRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } diff --git a/src/conn/routines/query.rs b/src/conn/routines/query.rs index 60e5f4bf..6b53f224 100644 --- a/src/conn/routines/query.rs +++ b/src/conn/routines/query.rs @@ -54,7 +54,13 @@ impl Routine<()> for QueryRoutine<'_, L> { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } diff --git a/src/conn/routines/reset.rs b/src/conn/routines/reset.rs index f48e9ef3..bd3e0d7d 100644 --- a/src/conn/routines/reset.rs +++ b/src/conn/routines/reset.rs @@ -25,7 +25,13 @@ impl Routine<()> for ResetRoutine { }; #[cfg(feature = "tracing")] - let fut = fut.instrument(span); + let fut = async { + fut.await.or_else(|e| { + tracing::error!(error = %e); + Err(e) + }) + } + .instrument(span); fut.boxed() } From 9effa9148801f528994fbbe3584f7b6de0dcb496 Mon Sep 17 00:00:00 2001 From: Anatoly Ikorsky Date: Tue, 14 Feb 2023 21:47:19 +0300 Subject: [PATCH 2/2] Introduce instrument_result! macro --- src/conn/routines/exec.rs | 10 ++-------- src/conn/routines/next_set.rs | 10 ++-------- src/conn/routines/ping.rs | 10 ++-------- src/conn/routines/prepare.rs | 10 ++-------- src/conn/routines/query.rs | 10 ++-------- src/conn/routines/reset.rs | 10 ++-------- src/tracing_utils.rs | 13 +++++++++++++ 7 files changed, 25 insertions(+), 48 deletions(-) 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) + }}; +}