use std::{
fmt,
future::Future,
ops::Deref,
pin::Pin,
sync::{Arc, OnceLock},
task::{Context, Poll},
time::Instant,
};
use futures_core::{
future::BoxFuture,
stream::{BoxStream, Stream},
};
use sqlx::{Database, Either, Error, Execute, Executor, Pool, SqlStr, error::BoxDynError};
use crate::observability::{
RequestLogContext, SqlExecutionMethod, SqlxQueryEvent, is_sqlx_capture_active,
record_default_sqlx_failure,
};
type SqlResultStream<'a, DB> =
BoxStream<'a, Result<Either<<DB as Database>::QueryResult, <DB as Database>::Row>, Error>>;
#[derive(Clone, Default)]
struct ExecutedQueryCapture {
state: Arc<OnceLock<ExecutedQuery>>,
}
struct ExecutedQuery {
statement: SqlStr,
started_at: Instant,
}
impl ExecutedQueryCapture {
fn new() -> Self {
Self::default()
}
fn wrap<E>(&self, query: E) -> CapturingExecute<E> {
CapturingExecute {
inner: query,
capture: self.clone(),
}
}
fn record(&self, statement: &SqlStr) {
let _ = self.state.set(ExecutedQuery {
statement: statement.clone(),
started_at: Instant::now(),
});
}
fn event(&self, rows_returned: Option<u64>) -> Option<SqlxQueryEvent> {
self.state.get().map(|query| SqlxQueryEvent {
statement: Some(query.statement.as_str().to_owned()),
rows_affected: None,
rows_returned,
elapsed_secs: Some(query.started_at.elapsed().as_secs_f64()),
})
}
}
struct CapturingExecute<E> {
inner: E,
capture: ExecutedQueryCapture,
}
impl<'query, DB, E> Execute<'query, DB> for CapturingExecute<E>
where
DB: Database,
E: Execute<'query, DB>,
{
fn sql(self) -> SqlStr {
let Self { inner, capture } = self;
let statement = inner.sql();
capture.record(&statement);
statement
}
fn statement(&self) -> Option<&DB::Statement> {
self.inner.statement()
}
fn take_arguments(&mut self) -> Result<Option<DB::Arguments>, BoxDynError> {
self.inner.take_arguments()
}
fn persistent(&self) -> bool {
self.inner.persistent()
}
}
pub struct SqlLoggingPool<DB>
where
DB: Database,
{
inner: Pool<DB>,
}
impl<DB> Clone for SqlLoggingPool<DB>
where
DB: Database,
{
fn clone(&self) -> Self {
Self::new(self.inner.clone())
}
}
impl<DB> SqlLoggingPool<DB>
where
DB: Database,
{
pub(crate) const fn new(inner: Pool<DB>) -> Self {
Self { inner }
}
pub const fn inner(&self) -> &Pool<DB> {
&self.inner
}
pub fn into_inner(self) -> Pool<DB> {
self.inner
}
}
impl<DB> fmt::Debug for SqlLoggingPool<DB>
where
DB: Database,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("SqlLoggingPool")
.field("inner", &self.inner)
.finish()
}
}
impl<DB> Deref for SqlLoggingPool<DB>
where
DB: Database,
{
type Target = Pool<DB>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<DB> From<Pool<DB>> for SqlLoggingPool<DB>
where
DB: Database,
{
fn from(inner: Pool<DB>) -> Self {
Self::new(inner)
}
}
fn auto_log_future<'a, T>(
future: BoxFuture<'a, Result<T, Error>>,
capture: ExecutedQueryCapture,
method: SqlExecutionMethod,
) -> BoxFuture<'a, Result<T, Error>>
where
T: Send + 'a,
{
if is_sqlx_capture_active() {
return future;
}
Box::pin(AutoLogFuture {
inner: future,
capture,
method,
started_at: None,
})
}
struct AutoLogFuture<'a, T> {
inner: BoxFuture<'a, Result<T, Error>>,
capture: ExecutedQueryCapture,
method: SqlExecutionMethod,
started_at: Option<Instant>,
}
impl<T> Future for AutoLogFuture<'_, T> {
type Output = Result<T, Error>;
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if is_sqlx_capture_active() || !RequestLogContext::is_current() {
return this.inner.as_mut().poll(context);
}
let started_at = this.started_at.get_or_insert_with(Instant::now);
let poll = this.inner.as_mut().poll(context);
if let Poll::Ready(Err(error)) = &poll {
record_default_sqlx_failure(
this.capture.event(None),
this.method,
started_at.elapsed(),
error,
);
}
poll
}
}
struct AutoLogStream<'a, DB>
where
DB: Database,
{
inner: Option<SqlResultStream<'a, DB>>,
capture: ExecutedQueryCapture,
method: SqlExecutionMethod,
started_at: Option<Instant>,
rows_returned: u64,
}
impl<DB> Unpin for AutoLogStream<'_, DB> where DB: Database {}
impl<DB> Stream for AutoLogStream<'_, DB>
where
DB: Database,
{
type Item = Result<Either<DB::QueryResult, DB::Row>, Error>;
fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
let Some(inner) = this.inner.as_mut() else {
return Poll::Ready(None);
};
if is_sqlx_capture_active() || !RequestLogContext::is_current() {
return inner.as_mut().poll_next(context);
}
let started_at = this.started_at.get_or_insert_with(Instant::now);
let poll = inner.as_mut().poll_next(context);
match poll {
Poll::Ready(Some(Ok(Either::Right(_)))) => {
this.rows_returned = this.rows_returned.saturating_add(1);
poll
}
Poll::Ready(Some(Err(error))) => {
this.inner.take();
record_default_sqlx_failure(
this.capture.event(Some(this.rows_returned)),
this.method,
started_at.elapsed(),
&error,
);
Poll::Ready(Some(Err(error)))
}
Poll::Ready(None) => {
this.inner.take();
Poll::Ready(None)
}
other => other,
}
}
}
impl<'pool, DB> Executor<'pool> for &'pool SqlLoggingPool<DB>
where
DB: Database,
for<'connection> &'connection mut DB::Connection: Executor<'connection, Database = DB>,
{
type Database = DB;
fn execute<'execute, 'query: 'execute, E>(
self,
query: E,
) -> BoxFuture<'execute, Result<DB::QueryResult, Error>>
where
'pool: 'execute,
E: 'query + Execute<'query, DB>,
{
let capture = ExecutedQueryCapture::new();
auto_log_future(
self.inner().execute(capture.wrap(query)),
capture,
SqlExecutionMethod::Execute,
)
}
fn fetch_many<'execute, 'query: 'execute, E>(
self,
query: E,
) -> BoxStream<'execute, Result<Either<DB::QueryResult, DB::Row>, Error>>
where
'pool: 'execute,
E: 'query + Execute<'query, DB>,
{
let capture = ExecutedQueryCapture::new();
let stream = self.inner().fetch_many(capture.wrap(query));
if is_sqlx_capture_active() {
return stream;
}
Box::pin(AutoLogStream::<DB> {
inner: Some(stream),
capture,
method: SqlExecutionMethod::FetchAll,
started_at: None,
rows_returned: 0,
})
}
fn fetch_all<'execute, 'query: 'execute, E>(
self,
query: E,
) -> BoxFuture<'execute, Result<Vec<DB::Row>, Error>>
where
'pool: 'execute,
E: 'query + Execute<'query, DB>,
{
let capture = ExecutedQueryCapture::new();
auto_log_future(
self.inner().fetch_all(capture.wrap(query)),
capture,
SqlExecutionMethod::FetchAll,
)
}
fn fetch_one<'execute, 'query: 'execute, E>(
self,
query: E,
) -> BoxFuture<'execute, Result<DB::Row, Error>>
where
'pool: 'execute,
E: 'query + Execute<'query, DB>,
{
let capture = ExecutedQueryCapture::new();
auto_log_future(
self.inner().fetch_one(capture.wrap(query)),
capture,
SqlExecutionMethod::FetchOne,
)
}
fn fetch_optional<'execute, 'query: 'execute, E>(
self,
query: E,
) -> BoxFuture<'execute, Result<Option<DB::Row>, Error>>
where
'pool: 'execute,
E: 'query + Execute<'query, DB>,
{
let capture = ExecutedQueryCapture::new();
auto_log_future(
self.inner().fetch_optional(capture.wrap(query)),
capture,
SqlExecutionMethod::FetchOptional,
)
}
fn prepare_with<'execute>(
self,
sql: SqlStr,
parameters: &'execute [DB::TypeInfo],
) -> BoxFuture<'execute, Result<DB::Statement, Error>>
where
'pool: 'execute,
{
self.inner().prepare_with(sql, parameters)
}
}
#[cfg(all(test, feature = "connection-mysql"))]
mod tests {
use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};
use super::*;
use crate::{
observability::{RequestLogContext, SqlLoggerExt, context::scope_request_log},
web::response::ApiError,
};
async fn closed_pool() -> SqlLoggingPool<sqlx::MySql> {
let pool = MySqlPoolOptions::new().connect_lazy_with(MySqlConnectOptions::new());
pool.close().await;
SqlLoggingPool::new(pool)
}
#[tokio::test]
async fn default_failure_logging_follows_the_poll_request_context() {
let context = RequestLogContext::new();
let future = auto_log_future(
Box::pin(async { Err::<(), _>(sqlx::Error::PoolClosed) }),
ExecutedQueryCapture::new(),
SqlExecutionMethod::FetchOne,
);
let error = scope_request_log(context.clone(), future)
.await
.unwrap_err();
assert!(matches!(error, sqlx::Error::PoolClosed));
assert_eq!(context.structured_snapshot().sql_executions.len(), 1);
}
#[test]
fn capturing_execute_preserves_the_exact_sqlx_statement() {
let capture = ExecutedQueryCapture::new();
let query = capture.wrap(
sqlx::query::<sqlx::MySql>("SELECT id FROM students WHERE id = ?")
.bind("不应出现在 SQL 模板中的敏感值"),
);
let statement = query.sql();
assert_eq!(statement.as_str(), "SELECT id FROM students WHERE id = ?");
assert_eq!(
capture.event(None).unwrap().statement.as_deref(),
Some("SELECT id FROM students WHERE id = ?")
);
}
#[tokio::test]
async fn raw_query_failure_is_recorded_by_default() {
let pool = closed_pool().await;
let context = RequestLogContext::new();
scope_request_log(context.clone(), async {
let error = sqlx::query("SELECT 1").execute(&pool).await.unwrap_err();
assert!(matches!(error, sqlx::Error::PoolClosed));
let error = sqlx::query_as::<_, (u64,)>("SELECT 1")
.fetch_all(&pool)
.await
.unwrap_err();
assert!(matches!(error, sqlx::Error::PoolClosed));
})
.await;
let snapshot = context.structured_snapshot();
assert_eq!(snapshot.sql_executions.len(), 2);
assert_eq!(snapshot.sql_executions[0].query_name, "sqlx.query");
assert_eq!(
snapshot.sql_executions[0].error.as_ref().unwrap().class,
"pool_unavailable"
);
assert_eq!(
snapshot.sql_executions[1].method,
SqlExecutionMethod::FetchAll
);
}
#[tokio::test]
async fn explicit_logger_sql_failure_is_not_recorded_twice() {
let pool = closed_pool().await;
let context = RequestLogContext::new();
scope_request_log(context.clone(), async {
let error = sqlx::query("SELECT 1")
.logger_sql("student.page")
.execute(&pool)
.await
.unwrap_err();
assert_eq!(error.code, 500_000);
let error = sqlx::query_as::<_, (u64,)>("SELECT 1")
.logger_sql("student.list")
.fetch_all(&pool)
.await
.unwrap_err();
assert_eq!(error.code, 500_000);
let error = sqlx::query("SELECT 1")
.api_err(ApiError::err_database("查询失败"))
.execute(&pool)
.await
.unwrap_err();
assert_eq!(error.code, 500_001);
})
.await;
let snapshot = context.structured_snapshot();
assert_eq!(snapshot.sql_executions.len(), 3);
assert_eq!(snapshot.sql_executions[0].query_name, "student.page");
assert_eq!(snapshot.sql_executions[1].query_name, "student.list");
assert_eq!(snapshot.sql_executions[2].api_error_code, Some(500_001));
}
}