Skip to content

Commit

Permalink
Move QueryRoutine tracing level to compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeam committed Feb 13, 2023
1 parent 0fd5853 commit 72dded0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ impl Conn {
async fn read_socket(&mut self) -> Result<()> {
if self.inner.opts.prefer_socket() && self.inner.socket.is_none() {
let row_opt = self.query_internal("SELECT @@socket").await?;
self.inner.socket = row_opt.unwrap_or((None,)).0;
self.inner.socket = row_opt.unwrap_or(None);
}
Ok(())
}
Expand Down
40 changes: 19 additions & 21 deletions src/conn/routines/query.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
use std::marker::PhantomData;

use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use mysql_common::constants::Command;
#[cfg(feature = "tracing")]
use tracing::{field, info_span, span_enabled, trace_span, Instrument, Level};
use tracing::{field, span_enabled, Instrument, Level};

use crate::tracing_utils::TracingLevel;
use crate::{Conn, TextProtocol};

use super::Routine;

/// A routine that performs `COM_QUERY`.
#[derive(Debug, Copy, Clone)]
pub struct QueryRoutine<'a> {
pub struct QueryRoutine<'a, L: TracingLevel> {
data: &'a [u8],
#[cfg_attr(not(feature = "tracing"), allow(dead_code))]
internal: bool,
_phantom: PhantomData<L>,
}

impl<'a> QueryRoutine<'a> {
pub fn new(data: &'a [u8], internal: bool) -> Self {
Self { data, internal }
impl<'a, L: TracingLevel> QueryRoutine<'a, L> {
pub fn new(data: &'a [u8]) -> Self {
Self {
data,
_phantom: PhantomData,
}
}
}

impl Routine<()> for QueryRoutine<'_> {
impl<L: TracingLevel> Routine<()> for QueryRoutine<'_, L> {
fn call<'a>(&'a mut self, conn: &'a mut Conn) -> BoxFuture<'a, crate::Result<()>> {
#[cfg(feature = "tracing")]
let span = if self.internal {
trace_span!(
"mysql_async::query",
mysql_async.connection.id = conn.id(),
mysql_async.query.sql = field::Empty
)
} else {
info_span!(
"mysql_async::query",
mysql_async.connection.id = conn.id(),
mysql_async.query.sql = field::Empty
)
};
let span = create_span!(
L::LEVEL,
"mysql_async::query",
mysql_async.connection.id = conn.id(),
mysql_async.query.sql = field::Empty,
);

#[cfg(feature = "tracing")]
if span_enabled!(Level::DEBUG) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ use std::sync::Arc;

mod buffer_pool;

#[macro_use]
mod tracing_utils;

#[macro_use]
mod macros;
mod conn;
Expand Down
3 changes: 2 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
connection_like::ToConnectionResult,
from_row,
prelude::{FromRow, StatementLike, ToConnection},
tracing_utils::LevelInfo,
BinaryProtocol, BoxFuture, Params, QueryResult, ResultSetStream, TextProtocol,
};

Expand Down Expand Up @@ -220,7 +221,7 @@ impl<Q: AsQuery> Query for Q {
ToConnectionResult::Immediate(conn) => conn,
ToConnectionResult::Mediate(fut) => fut.await?,
};
conn.raw_query(self).await?;
conn.raw_query::<'_, _, LevelInfo>(self).await?;
Ok(QueryResult::new(conn))
}
.boxed()
Expand Down
23 changes: 9 additions & 14 deletions src/queryable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
prelude::{FromRow, StatementLike},
query::AsQuery,
queryable::query_result::ResultSetMeta,
tracing_utils::{LevelInfo, LevelTrace, TracingLevel},
BoxFuture, Column, Conn, Params, ResultSetStream, Row,
};

Expand Down Expand Up @@ -102,11 +103,11 @@ impl Conn {
}

/// Low level function that performs a text query.
pub(crate) async fn raw_query<'a, Q>(&'a mut self, query: Q) -> Result<()>
pub(crate) async fn raw_query<'a, Q, L: TracingLevel>(&'a mut self, query: Q) -> Result<()>
where
Q: AsQuery + 'a,
{
self.routine(QueryRoutine::new(query.as_query().as_ref(), false))
self.routine(QueryRoutine::<'_, L>::new(query.as_query().as_ref()))
.await
}

Expand All @@ -120,16 +121,11 @@ impl Conn {
T: FromRow + Send + 'static,
{
async move {
self.routine(QueryRoutine::new(query.as_query().as_ref(), true))
.await?;
let mut result: QueryResult<'a, 'static, TextProtocol> = QueryResult::new(self);
let output = if result.is_empty() {
None
} else {
result.next().await?.map(crate::from_row)
};
result.drop_result().await?;
Ok(output)
self.raw_query::<'_, _, LevelTrace>(query).await?;
Ok(QueryResult::<'_, '_, TextProtocol>::new(self)
.collect_and_drop::<T>()
.await?
.pop())
}
.boxed()
}
Expand Down Expand Up @@ -480,8 +476,7 @@ impl Queryable for Conn {
Q: AsQuery + 'a,
{
async move {
self.routine(QueryRoutine::new(query.as_query().as_ref(), false))
.await?;
self.raw_query::<'_, _, LevelInfo>(query).await?;
Ok(QueryResult::new(self))
}
.boxed()
Expand Down
40 changes: 40 additions & 0 deletions src/tracing_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// Compile-time tracing level.
pub trait TracingLevel: Send + Sync + 'static {
#[cfg(feature = "tracing")]
const LEVEL: tracing::Level;
}

/// INFO tracing level.
pub struct LevelInfo;

impl TracingLevel for LevelInfo {
#[cfg(feature = "tracing")]
const LEVEL: tracing::Level = tracing::Level::INFO;
}

/// TRACE tracing level.
pub struct LevelTrace;

impl TracingLevel for LevelTrace {
#[cfg(feature = "tracing")]
const LEVEL: tracing::Level = tracing::Level::TRACE;
}

#[cfg(feature = "tracing")]
macro_rules! create_span {
($s:expr, $($field:tt)*) => {
if $s == tracing::Level::TRACE {
tracing::trace_span!($($field)*)
} else if $s == tracing::Level::DEBUG {
tracing::debug_span!($($field)*)
} else if $s == tracing::Level::INFO {
tracing::info_span!($($field)*)
} else if $s == tracing::Level::WARN {
tracing::warn_span!($($field)*)
} else if $s == tracing::Level::ERROR {
tracing::error_span!($($field)*)
} else {
unreachable!();
}
}
}

0 comments on commit 72dded0

Please sign in to comment.