loveyu-kit 0.2.0

面向 Axum 项目的可组合 API 工具库
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! 默认记录 SQLx 执行失败的连接池包装器。

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()
    }
}

/// 为 SQLx 连接池增加默认失败日志策略。
///
/// 包装器截取 SQLx 最终交给数据库驱动的 [`Execute::sql`] 语句模板;成功查询不会处理
/// `sqlx::query` tracing 事件。显式 `.logger_sql(...)` 和 `.logger_err()` 策略仍由
/// [`crate::observability::SqlxQueryLayer`] 捕获 SQLx 的执行事件。
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 }
    }

    /// 返回底层 SQLx 连接池。
    pub const fn inner(&self) -> &Pool<DB> {
        &self.inner
    }

    /// 取出底层 SQLx 连接池。
    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();
        // 请求日志上下文和显式 SQL 策略都以实际 poll 时的作用域为准。
        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));
    }
}