Skip to content

Commit

Permalink
Merge pull request #230 from cloneable/issue223-record-errors-spans
Browse files Browse the repository at this point in the history
Record errors from routines in tracing spans
  • Loading branch information
blackbeam authored Feb 15, 2023
2 parents 043d81a + 9effa91 commit 287dd86
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 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,7 +104,7 @@ impl Routine<()> for ExecRoutine<'_> {
};

#[cfg(feature = "tracing")]
let fut = fut.instrument(span);
let fut = instrument_result!(fut, span);

fut.boxed()
}
Expand Down
4 changes: 2 additions & 2 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,7 +36,7 @@ where
};

#[cfg(feature = "tracing")]
let fut = fut.instrument(span);
let fut = instrument_result!(fut, span);

fut.boxed()
}
Expand Down
4 changes: 2 additions & 2 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,7 +24,7 @@ impl Routine<()> for PingRoutine {
};

#[cfg(feature = "tracing")]
let fut = fut.instrument(span);
let fut = instrument_result!(fut, span);

fut.boxed()
}
Expand Down
5 changes: 3 additions & 2 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 @@ -48,6 +48,7 @@ impl Routine<Arc<StmtInner>> 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());

Expand All @@ -65,7 +66,7 @@ impl Routine<Arc<StmtInner>> for PrepareRoutine {
};

#[cfg(feature = "tracing")]
let fut = fut.instrument(span);
let fut = instrument_result!(fut, span);

fut.boxed()
}
Expand Down
4 changes: 2 additions & 2 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,7 +54,7 @@ impl<L: TracingLevel> Routine<()> for QueryRoutine<'_, L> {
};

#[cfg(feature = "tracing")]
let fut = fut.instrument(span);
let fut = instrument_result!(fut, span);

fut.boxed()
}
Expand Down
4 changes: 2 additions & 2 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,7 +25,7 @@ impl Routine<()> for ResetRoutine {
};

#[cfg(feature = "tracing")]
let fut = fut.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 287dd86

Please sign in to comment.