Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record errors from routines in tracing spans #230

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}};
}