-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.rs
133 lines (124 loc) · 3.88 KB
/
metrics.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
use lazy_static::lazy_static;
use prometheus::{
core::{MetricVec, MetricVecBuilder},
register_gauge, register_histogram, register_histogram_vec, register_int_counter,
register_int_counter_vec, register_int_gauge_vec, Gauge, Histogram, HistogramVec, IntCounter,
IntCounterVec, IntGaugeVec,
};
lazy_static! {
pub static ref METRICS: Metrics = Metrics::new();
}
pub struct Metrics {
pub client_query: ResponseMetrics,
pub avg_query_fees: Gauge,
pub indexer_query: ResponseMetricVecs,
pub collect_receipts: ResponseMetrics,
pub partial_voucher: ResponseMetrics,
pub voucher: ResponseMetrics,
pub blocks_per_minute: IntGaugeVec,
}
impl Metrics {
fn new() -> Self {
Self {
client_query: ResponseMetrics::new("gw_client_query", "client query"),
avg_query_fees: register_gauge!(
"gw_avg_query_fees",
"average indexer fees per query, in USD"
)
.unwrap(),
indexer_query: ResponseMetricVecs::new(
"gw_indexer_query",
"indexer query",
&["deployment", "indexer"],
),
collect_receipts: ResponseMetrics::new(
"gw_collect_receipts",
"collect-receipts request",
),
partial_voucher: ResponseMetrics::new("gw_partial_voucher", "partial-voucher request"),
voucher: ResponseMetrics::new("gw_voucher", "requests for voucher"),
blocks_per_minute: register_int_gauge_vec!(
"gw_blocks_per_minute",
"chain blocks per minute",
&["chain"]
)
.unwrap(),
}
}
}
#[derive(Clone)]
pub struct ResponseMetrics {
pub ok: IntCounter,
pub err: IntCounter,
pub duration: Histogram,
}
impl ResponseMetrics {
pub fn new(prefix: &str, description: &str) -> Self {
let metrics = Self {
ok: register_int_counter!(
&format!("{prefix}_ok"),
&format!("{description} success count"),
)
.unwrap(),
err: register_int_counter!(
&format!("{prefix}_err"),
&format!("{description} error count"),
)
.unwrap(),
duration: register_histogram!(
&format!("{prefix}_duration"),
&format!("{description} duration"),
)
.unwrap(),
};
metrics.ok.inc();
metrics.err.inc();
metrics
}
}
#[derive(Clone)]
pub struct ResponseMetricVecs {
pub ok: IntCounterVec,
pub err: IntCounterVec,
pub duration: HistogramVec,
}
impl ResponseMetricVecs {
pub fn new(prefix: &str, description: &str, labels: &[&str]) -> Self {
Self {
ok: register_int_counter_vec!(
&format!("{prefix}_ok"),
&format!("{description} success count"),
labels,
)
.unwrap(),
err: register_int_counter_vec!(
&format!("{prefix}_err"),
&format!("{description} error count"),
labels,
)
.unwrap(),
duration: register_histogram_vec!(
&format!("{prefix}_duration"),
&format!("{description} duration"),
labels,
)
.unwrap(),
}
}
pub fn check<T, E>(&self, label_values: &[&str], result: &Result<T, E>) {
match &result {
Ok(_) => with_metric(&self.ok, label_values, |c| c.inc()),
Err(_) => with_metric(&self.err, label_values, |c| c.inc()),
};
}
}
pub fn with_metric<T, F, B>(metric_vec: &MetricVec<B>, label_values: &[&str], f: F) -> Option<T>
where
B: MetricVecBuilder,
F: Fn(B::M) -> T,
{
metric_vec
.get_metric_with_label_values(label_values)
.ok()
.map(f)
}