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
//! Middlewares for reporting Metrics in Merino.

use crate::errors::HandlerError;
use actix_web::{
    body::MessageBody,
    dev::{Service, ServiceRequest, ServiceResponse, Transform},
    Error as ActixError,
};
use cadence::{StatsdClient, Timed};
use std::{
    fmt,
    future::{ready, Future, Ready},
    pin::Pin,
    task::Context,
    time::Instant,
};

/// Factory for [`MetricsMiddleware`].
pub struct Metrics;

/// Middleware to record request metrics.
pub struct MetricsMiddleware<S> {
    /// The wrapped service.
    service: S,
}

impl<S, B> Transform<S, ServiceRequest> for Metrics
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>>,
    B: 'static + MessageBody,
    S::Future: 'static,
    S::Error: fmt::Debug,
{
    type Response = S::Response;

    type Error = ActixError;

    type Transform = MetricsMiddleware<S>;

    type InitError = ();

    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ready(Ok(MetricsMiddleware { service }))
    }
}

impl<S, B> Service<ServiceRequest> for MetricsMiddleware<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>>,
    B: 'static + MessageBody,
    S::Future: 'static,
    S::Error: fmt::Debug,
{
    type Response = S::Response;

    type Error = ActixError;

    #[allow(clippy::type_complexity)]
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    fn poll_ready(&self, ctx: &mut Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
        self.service.poll_ready(ctx).map_err(|error| {
            tracing::error!(
                r#type = "web.metrics.polling-error",
                ?error,
                "Error polling service from metrics middleware"
            );
            HandlerError::internal().into()
        })
    }

    fn call(&self, req: ServiceRequest) -> Self::Future {
        let start = Instant::now();
        let path = req.path().to_string();
        let metrics_client = req.app_data::<StatsdClient>().cloned();
        let fut = self.service.call(req);

        Box::pin(async move {
            let response = fut.await.map_err(|_err| HandlerError::internal())?;
            if let Some(metrics_client) = metrics_client {
                let lapsed = Instant::now().duration_since(start);
                metrics_client
                    .time_with_tags("request.duration", lapsed)
                    .with_tag("path", &path)
                    .send();
            } else if cfg!(debug) {
                panic!("No metrics client configured, but metrics middleware attached");
            }
            Ok(response)
        })
    }
}