sui_faucet/
metrics_layer.rs

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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use futures::Future;
use http::StatusCode;
use prometheus::{HistogramTimer, Registry};
use tower::{load_shed::error::Overloaded, BoxError, Layer, Service, ServiceExt};
use tracing::{error, info, warn};

use crate::metrics::{is_path_tracked, normalize_path, RequestMetrics};
use http::Request;

/// Tower Layer for tracking metrics in Prometheus related to number, success-rate and latency of
/// requests running through service.
#[derive(Clone)]
pub struct RequestMetricsLayer {
    metrics: Arc<RequestMetrics>,
}

#[derive(Clone)]
pub struct RequestMetricsService<Inner> {
    inner: Inner,
    metrics: Arc<RequestMetrics>,
}

pub struct RequestMetricsFuture<Res> {
    future: Pin<Box<dyn Future<Output = Result<Res, BoxError>> + Send>>,
}

struct MetricsGuard {
    timer: Option<HistogramTimer>,
    metrics: Arc<RequestMetrics>,
    path: String,
    user_agent: String,
}

impl RequestMetricsLayer {
    pub fn new(registry: &Registry) -> Self {
        Self {
            metrics: Arc::new(RequestMetrics::new(registry)),
        }
    }
}

impl<Inner> Layer<Inner> for RequestMetricsLayer {
    type Service = RequestMetricsService<Inner>;
    fn layer(&self, inner: Inner) -> Self::Service {
        RequestMetricsService {
            inner,
            metrics: self.metrics.clone(),
        }
    }
}

impl<Inner, Body> Service<Request<Body>> for RequestMetricsService<Inner>
where
    Inner: Service<Request<Body>, Response = http::Response<Body>, Error = BoxError>
        + Clone
        + Send
        + 'static,
    Inner::Future: Send,
    Body: Send + 'static,
{
    type Response = Inner::Response;
    type Error = BoxError;
    type Future = RequestMetricsFuture<Self::Response>;

    fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(ctx)
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let path = req.uri().path().to_string();
        let user_agent = req
            .headers()
            .get(http::header::USER_AGENT)
            .and_then(|val| val.to_str().ok());
        let metrics = MetricsGuard::new(self.metrics.clone(), &path, user_agent);
        let inner = self.inner.clone();

        let future = Box::pin(async move {
            let resp = inner.oneshot(req).await;

            if let Some(metrics) = metrics {
                match &resp {
                    Ok(resp) if !resp.status().is_success() => {
                        metrics.failed(None, Some(resp.status()))
                    }
                    Ok(_) => metrics.succeeded(),
                    Err(err) => {
                        if err.is::<Overloaded>() {
                            metrics.shed();
                        } else {
                            metrics.failed(Some(err), None);
                        }
                    }
                }
            }

            resp
        });

        RequestMetricsFuture { future }
    }
}

impl<Res> Future for RequestMetricsFuture<Res> {
    type Output = Result<Res, BoxError>;
    fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
        Future::poll(self.future.as_mut(), ctx)
    }
}

impl MetricsGuard {
    fn new(metrics: Arc<RequestMetrics>, path: &str, user_agent: Option<&str>) -> Option<Self> {
        let normalized_path = normalize_path(path);
        let user_agent = user_agent.unwrap_or("unknown");

        if !is_path_tracked(normalized_path) {
            return None;
        }

        metrics
            .total_requests_received
            .with_label_values(&[normalized_path, user_agent])
            .inc();
        metrics
            .current_requests_in_flight
            .with_label_values(&[normalized_path, user_agent])
            .inc();
        Some(MetricsGuard {
            timer: Some(
                metrics
                    .process_latency
                    .with_label_values(&[normalized_path, user_agent])
                    .start_timer(),
            ),
            metrics,
            path: normalized_path.to_string(),
            user_agent: user_agent.to_string(),
        })
    }

    fn succeeded(mut self) {
        if let Some(timer) = self.timer.take() {
            let elapsed = timer.stop_and_record();
            self.metrics
                .total_requests_succeeded
                .with_label_values(&[&self.path, &self.user_agent])
                .inc();
            info!(
                "Request succeeded for path {} in {:.2}s",
                self.path, elapsed
            );
        }
    }

    fn failed(mut self, error: Option<&BoxError>, status: Option<StatusCode>) {
        if let Some(timer) = self.timer.take() {
            let elapsed = timer.stop_and_record();
            self.metrics
                .total_requests_failed
                .with_label_values(&[&self.path, &self.user_agent])
                .inc();

            if let Some(err) = error {
                error!(
                    "Request failed for path {} in {:.2}s, error {:?}",
                    self.path, elapsed, err
                );
            } else if let Some(status) = status {
                // don't log too many requests as an error, as we're flooding the logs
                if status != StatusCode::TOO_MANY_REQUESTS {
                    error!(
                        "Request failed for path {} in {:.2}s with status: {}",
                        self.path, elapsed, status
                    );
                }
            } else {
                warn!("Request failed for path {} in {:.2}s", self.path, elapsed);
            }
        }
    }

    fn shed(mut self) {
        if let Some(timer) = self.timer.take() {
            let elapsed = timer.stop_and_record();
            self.metrics
                .total_requests_shed
                .with_label_values(&[&self.path, &self.user_agent])
                .inc();
            info!("Request shed for path {} in {:.2}s", self.path, elapsed);
        }
    }
}

impl Drop for MetricsGuard {
    fn drop(&mut self) {
        if self
            .metrics
            .current_requests_in_flight
            .get_metric_with_label_values(&[&self.path])
            .is_ok()
        {
            self.metrics
                .current_requests_in_flight
                .with_label_values(&[&self.path])
                .dec();

            if let Some(timer) = self.timer.take() {
                let elapsed = timer.stop_and_record();
                self.metrics
                    .total_requests_disconnected
                    .with_label_values(&[&self.path, &self.user_agent])
                    .inc();
                info!(
                    "Request disconnected for path {} in {:.2}s",
                    self.path, elapsed
                );
            }
        }
    }
}