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
use mysten_network::metrics::MetricsCallbackProvider;
use prometheus::{
default_registry, register_histogram_vec_with_registry, register_int_counter_vec_with_registry,
HistogramVec, IntCounterVec, Registry,
};
use std::time::Duration;
use tonic::Code;
#[derive(Clone)]
pub struct EndpointMetrics {
requests_by_route: IntCounterVec,
req_latency_by_route: HistogramVec,
}
impl EndpointMetrics {
pub fn new(registry: &Registry) -> Self {
Self {
requests_by_route: register_int_counter_vec_with_registry!(
"requests_by_route",
"Number of requests by route",
&["route", "status", "grpc_status_code"],
registry
)
.unwrap(),
req_latency_by_route: register_histogram_vec_with_registry!(
"req_latency_by_route",
"Latency of a request by route",
&["route", "status", "grpc_status_code"],
registry
)
.unwrap(),
}
}
}
impl MetricsCallbackProvider for EndpointMetrics {
fn on_request(&self, _path: String) {
}
fn on_response(&self, path: String, latency: Duration, status: u16, grpc_status_code: Code) {
let code: i32 = grpc_status_code.into();
let labels = [path.as_str(), &status.to_string(), &code.to_string()];
self.requests_by_route.with_label_values(&labels).inc();
let req_latency_secs = latency.as_secs_f64();
self.req_latency_by_route
.with_label_values(&labels)
.observe(req_latency_secs);
}
}
impl Default for EndpointMetrics {
fn default() -> Self {
Self::new(default_registry())
}
}