Skip to content

Commit

Permalink
Introduce instrument_result! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeam committed Feb 15, 2023
1 parent c851de7 commit 9effa91
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 48 deletions.
10 changes: 2 additions & 8 deletions src/conn/routines/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}
Expand Down
10 changes: 2 additions & 8 deletions src/conn/routines/next_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()
}
Expand Down
10 changes: 2 additions & 8 deletions src/conn/routines/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
}
Expand Down
10 changes: 2 additions & 8 deletions src/conn/routines/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -66,13 +66,7 @@ impl Routine<Arc<StmtInner>> 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()
}
Expand Down
10 changes: 2 additions & 8 deletions src/conn/routines/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -54,13 +54,7 @@ impl<L: TracingLevel> 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()
}
Expand Down
10 changes: 2 additions & 8 deletions src/conn/routines/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
}
Expand Down
13 changes: 13 additions & 0 deletions src/tracing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}};
}

0 comments on commit 9effa91

Please sign in to comment.