Skip to main content

wae_https/middleware/
tracing.rs

1//! 追踪中间件
2//!
3//! 为请求添加追踪日志。
4
5use axum::{body::Body, extract::Request, http::Response, middleware::Next};
6use tracing::{Instrument, info, info_span};
7
8/// 追踪中间件层
9pub struct TracingLayer;
10
11impl TracingLayer {
12    /// 创建追踪中间件
13    pub async fn middleware(request: Request, next: Next) -> Response<Body> {
14        let method = request.method().to_string();
15        let uri = request.uri().to_string();
16        let span = info_span!("http_request", method = %method, uri = %uri);
17
18        async move {
19            info!("Request started");
20            let response = next.run(request).await;
21            info!("Request completed with status: {}", response.status());
22            response
23        }
24        .instrument(span)
25        .await
26    }
27}