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
use std::error::Error;

use tide::{Middleware, Next, Request};
use tracing::{error, error_span, info, info_span, span, warn, Level};
use tracing_futures::Instrument;

/// Log all incoming requests and responses with tracing spans.
///
/// ```
/// let mut app = tide::Server::new();
/// app.middleware(tide_tracing::TraceMiddleware::new());
/// ```
#[derive(Debug, Default, Clone)]
pub struct TraceMiddleware;

impl TraceMiddleware {
    /// Create a new instance of `TraceMiddleware`.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Log a request and a response.
    async fn log<'a, State: Clone + Send + Sync + 'static>(
        &'a self,
        ctx: Request<State>,
        next: Next<'a, State>,
    ) -> tide::Result {
        let path = ctx.url().path().to_owned();
        let method = ctx.method().to_string();

        let response = async {
            info!(method = method.as_str());

            let start = std::time::Instant::now();
            let response = next.run(ctx).await;
            let status = response.status();

            async {
                info!(status = status as u16);
                info!(duration = &format!("{:?}", start.elapsed()).as_str());

                if status.is_server_error() {
                    async {
                        if let Some(error) = response.error() {
                            let error: &(dyn Error) = error.as_ref();
                            error!(error);
                        }
                        error!("Response sent");
                    }
                    .instrument(error_span!("Internal error"))
                    .await;
                } else if status.is_client_error() {
                    let span = span!(Level::WARN, "Client Error");
                    let _enter = span.enter();

                    warn!("Client Error: Response sent");
                } else {
                    info!("Response sent");
                }
            }
            .instrument(info_span!("Response"))
            .await;
            response
        }
        .instrument(info_span!("Request", path = path.as_str()))
        .await;
        Ok(response)
    }
}

#[async_trait::async_trait]
impl<State: Clone + Send + Sync + 'static> Middleware<State> for TraceMiddleware {
    async fn handle(&self, req: Request<State>, next: Next<'_, State>) -> tide::Result {
        self.log(req, next).await
    }
}