mithril_aggregator/metrics/
service.rs1use std::collections::HashMap;
2
3use mithril_metric::{build_metrics_service, MetricCounterWithLabels, MetricsServiceExporter};
4
5use mithril_metric::metric::{MetricCollector, MetricCounter};
6use prometheus::proto::{LabelPair, Metric, MetricFamily};
7
8static CLIENT_ORIGIN_TAG_LABEL: &str = "origin_tag";
10static SIGNER_REGISTRATION_ORIGIN_TAG_LABEL: &str = "origin_tag";
11static SIGNER_SIGNATURE_ORIGIN_TAG_LABEL: &str = "origin_tag";
12static CLIENT_TYPE_LABEL: &str = "client_type";
13
14build_metrics_service!(
15 MetricsService,
16
17 certificate_detail_total_served_since_startup:MetricCounterWithLabels(
18 "certificate_detail_total_served_since_startup",
19 "Number of certificate details served since startup on a Mithril aggregator node",
20 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
21 ),
22 artifact_detail_cardano_immutable_files_full_total_served_since_startup:MetricCounterWithLabels(
23 "mithril_aggregator_artifact_detail_cardano_db_total_served_since_startup",
24 "Number of Cardano immutable files full artifact details served since startup on a Mithril aggregator node",
25 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
26 ),
27 cardano_immutable_files_full_total_restoration_since_startup:MetricCounterWithLabels(
28 "mithril_aggregator_cardano_db_total_restoration_since_startup",
29 "Number of Cardano immutable files full restorations since startup on a Mithril aggregator node",
30 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
31 ),
32 cardano_database_immutable_files_restored_since_startup:MetricCounterWithLabels(
33 "mithril_aggregator_cardano_db_immutable_files_restored_since_startup",
34 "Number of Cardano immutable files restored since startup on a Mithril aggregator node",
35 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
36 ),
37 cardano_database_ancillary_files_restored_since_startup:MetricCounterWithLabels(
38 "mithril_aggregator_cardano_db_ancillary_files_restored_since_startup",
39 "Number of Cardano ancillary files restored since startup on a Mithril aggregator node",
40 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
41 ),
42 cardano_database_complete_restoration_since_startup:MetricCounterWithLabels(
43 "mithril_aggregator_cardano_db_complete_restoration_since_startup",
44 "Number of complete Cardano database restoration since startup on a Mithril aggregator node",
45 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
46 ),
47 cardano_database_partial_restoration_since_startup:MetricCounterWithLabels(
48 "mithril_aggregator_cardano_db_partial_restoration_since_startup",
49 "Number of partial Cardano database restoration since startup on a Mithril aggregator node",
50 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
51 ),
52 artifact_detail_cardano_database_total_served_since_startup:MetricCounterWithLabels(
53 "mithril_aggregator_artifact_detail_cardano_database_total_served_since_startup",
54 "Number of Cardano database artifact details served since startup on a Mithril aggregator node",
55 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
56 ),
57 artifact_detail_mithril_stake_distribution_total_served_since_startup:MetricCounterWithLabels(
58 "mithril_aggregator_artifact_detail_mithril_stake_distribution_total_served_since_startup",
59 "Number of Mithril stake distribution artifact details served since startup on a Mithril aggregator node",
60 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
61 ),
62 artifact_detail_cardano_stake_distribution_total_served_since_startup:MetricCounterWithLabels(
63 "mithril_aggregator_artifact_detail_cardano_stake_distribution_total_served_since_startup",
64 "Number of Cardano stake distribution artifact details served since startup on a Mithril aggregator node",
65 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
66 ),
67 artifact_detail_cardano_transaction_total_served_since_startup:MetricCounterWithLabels(
68 "mithril_aggregator_artifact_detail_cardano_transaction_total_served_since_startup",
69 "Number of Cardano transaction artifact details served since startup on a Mithril aggregator node",
70 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
71 ),
72 proof_cardano_transaction_total_proofs_served_since_startup:MetricCounterWithLabels(
73 "mithril_aggregator_proof_cardano_transaction_total_proofs_served_since_startup",
74 "Number of Cardano transaction proofs served since startup on a Mithril aggregator node",
75 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
76 ),
77 proof_cardano_transaction_total_transactions_served_since_startup:MetricCounterWithLabels(
78 "mithril_aggregator_proof_cardano_transaction_total_transactions_served_since_startup",
79 "Number of Cardano transaction hashes requested for proof since startup on a Mithril aggregator node",
80 &[CLIENT_ORIGIN_TAG_LABEL, CLIENT_TYPE_LABEL]
81 ),
82 signer_registration_total_received_since_startup:MetricCounterWithLabels(
83 "mithril_aggregator_signer_registration_total_received_since_startup",
84 "Number of signer registrations received since startup on a Mithril aggregator node",
85 &[SIGNER_REGISTRATION_ORIGIN_TAG_LABEL]
86 ),
87 signature_registration_total_received_since_startup:MetricCounterWithLabels(
88 "mithril_aggregator_signature_registration_total_received_since_startup",
89 "Number of signature registrations received since startup on a Mithril aggregator node",
90 &[SIGNER_SIGNATURE_ORIGIN_TAG_LABEL]
91 ),
92 certificate_total_produced_since_startup:MetricCounter(
93 "mithril_aggregator_certificate_total_produced_since_startup",
94 "Number of certificates produced since startup on a Mithril aggregator node"
95 ),
96 artifact_cardano_immutable_files_full_total_produced_since_startup:MetricCounter(
97 "mithril_aggregator_artifact_cardano_db_total_produced_since_startup",
98 "Number of Cardano immutable files full artifacts produced since startup on a Mithril aggregator node"
99 ),
100 artifact_cardano_database_total_produced_since_startup:MetricCounter(
101 "mithril_aggregator_artifact_cardano_database_total_produced_since_startup",
102 "Number of Cardano database artifacts produced since startup on a Mithril aggregator node"
103 ),
104 artifact_mithril_stake_distribution_total_produced_since_startup:MetricCounter(
105 "mithril_aggregator_artifact_mithril_stake_distribution_total_produced_since_startup",
106 "Number of Mithril stake distribution artifacts produced since startup on a Mithril aggregator node"
107 ),
108 artifact_cardano_stake_distribution_total_produced_since_startup:MetricCounter(
109 "mithril_aggregator_artifact_cardano_stake_distribution_total_produced_since_startup",
110 "Number of Cardano stake distribution artifacts produced since startup on a Mithril aggregator node"
111 ),
112 artifact_cardano_transaction_total_produced_since_startup:MetricCounter(
113 "mithril_aggregator_artifact_cardano_transaction_total_produced_since_startup",
114 "Number of Cardano transaction artifacts produced since startup on a Mithril aggregator node"
115 ),
116 runtime_cycle_success_since_startup:MetricCounter(
117 "mithril_aggregator_runtime_cycle_success_since_startup",
118 "Number of successful runtime cycles since startup on a Mithril aggregator"
119 ),
120 runtime_cycle_total_since_startup:MetricCounter(
121 "mithril_aggregator_runtime_cycle_total_since_startup",
122 "Number of runtime cycles since startup on a Mithril aggregator"
123 )
124
125);
126
127impl MetricsService {
128 pub fn export_metrics_map(&self) -> HashMap<String, HashMap<String, MetricLabelValueMap>> {
130 self.registry
131 .gather()
132 .iter()
133 .map(|metric_family| {
134 (
135 metric_family.name().to_string(),
136 self.build_metric_map(metric_family),
137 )
138 })
139 .collect()
140 }
141
142 fn build_label_key(&self, labels: &[LabelPair]) -> String {
143 labels
144 .iter()
145 .map(|p| p.value())
146 .collect::<Vec<_>>()
147 .join(",")
148 }
149
150 fn build_metric_map(
151 &self,
152 metric_family: &MetricFamily,
153 ) -> HashMap<String, MetricLabelValueMap> {
154 metric_family
155 .get_metric()
156 .iter()
157 .map(|m| {
158 (
159 self.build_label_key(m.get_label()),
160 MetricLabelValueMap::new(m.clone()),
161 )
162 })
163 .collect()
164 }
165}
166
167type LabelName = String;
168type LabelValue = String;
169
170#[derive(Debug, Clone, PartialEq, Eq)]
171pub struct MetricLabelValueMap {
173 pub label_value_map: HashMap<LabelName, LabelValue>,
175 pub counter: i32,
177}
178
179impl MetricLabelValueMap {
180 pub fn new(metric: Metric) -> Self {
182 Self {
183 label_value_map: metric
184 .get_label()
185 .iter()
186 .map(|label| (label.name().to_string(), label.value().to_string()))
187 .collect(),
188 counter: metric.get_counter().as_ref().unwrap_or_default().value() as i32,
189 }
190 }
191}
192
193impl Default for MetricLabelValueMap {
194 fn default() -> Self {
196 Self {
197 label_value_map: HashMap::new(),
198 counter: 0,
199 }
200 }
201}
202
203#[cfg(test)]
204mod tests {
205 use crate::test_tools::TestLogger;
206
207 use super::*;
208
209 #[test]
210 fn should_export_counter_metrics_in_a_map() {
211 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
212 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
213 let metric_b = metrics_service.get_certificate_total_produced_since_startup();
214 metric_a.increment_by(5);
215 metric_b.increment_by(12);
216
217 let export = metrics_service.export_metrics_map();
218 assert_eq!(5, export[&metric_a.name()][""].counter);
219 assert_eq!(12, export[&metric_b.name()][""].counter);
220 }
221
222 #[test]
223 fn should_export_counter_metrics_with_label_in_a_map() {
224 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
225 let metric_a = metrics_service.get_certificate_detail_total_served_since_startup();
226 metric_a.increment_by(&["A", "B"], 5);
227 metric_a.increment_by(&["1", "2"], 12);
228
229 let export = metrics_service.export_metrics_map();
230
231 let metric_a_token_a_b = export[&metric_a.name()]["B,A"].clone();
232 let metric_a_token_1_2 = export[&metric_a.name()]["2,1"].clone();
233
234 assert_eq!(5, metric_a_token_a_b.to_owned().counter);
235 assert_eq!(
236 "A",
237 metric_a_token_a_b.to_owned().label_value_map["origin_tag"]
238 );
239 assert_eq!(
240 "B",
241 metric_a_token_a_b.to_owned().label_value_map["client_type"]
242 );
243
244 assert_eq!(12, metric_a_token_1_2.to_owned().counter);
245 assert_eq!(
246 "1",
247 metric_a_token_1_2.to_owned().label_value_map["origin_tag"]
248 );
249 assert_eq!(
250 "2",
251 metric_a_token_1_2.to_owned().label_value_map["client_type"]
252 );
253 }
254
255 #[test]
256 fn should_export_several_times_and_counter_return_values_since_start() {
257 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
258 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
259 metric_a.increment_by(5);
260
261 let export = metrics_service.export_metrics_map();
262 assert_eq!(5, export[&metric_a.name()][""].counter);
263
264 metric_a.increment();
265 let export = metrics_service.export_metrics_map();
266 assert_eq!(6, export[&metric_a.name()][""].counter);
267 }
268
269 #[test]
270 fn should_export_counter_even_the_value_is_0() {
271 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
272 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
273
274 let export = metrics_service.export_metrics_map();
275 assert_eq!(0, export[&metric_a.name()][""].counter);
276 }
277
278 #[test]
279 fn metric_service_should_only_contain_counters_as_export_metrics_map_does_not_yet_support_other_types(
280 ) {
281 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
282
283 for metric_family in metrics_service.registry.gather() {
284 for metric in metric_family.get_metric() {
285 assert!(metric.get_counter().is_some());
286 }
287 }
288 }
289}