mithril_aggregator/metrics/
service.rs1use std::collections::HashMap;
2
3use mithril_metric::{MetricCounterWithLabels, MetricsServiceExporter, build_metrics_service};
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.iter().map(|p| p.value()).collect::<Vec<_>>().join(",")
144 }
145
146 fn build_metric_map(
147 &self,
148 metric_family: &MetricFamily,
149 ) -> HashMap<String, MetricLabelValueMap> {
150 metric_family
151 .get_metric()
152 .iter()
153 .map(|m| {
154 (
155 self.build_label_key(m.get_label()),
156 MetricLabelValueMap::new(m.clone()),
157 )
158 })
159 .collect()
160 }
161}
162
163type LabelName = String;
164type LabelValue = String;
165
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct MetricLabelValueMap {
169 pub label_value_map: HashMap<LabelName, LabelValue>,
171 pub counter: i32,
173}
174
175impl MetricLabelValueMap {
176 pub fn new(metric: Metric) -> Self {
178 Self {
179 label_value_map: metric
180 .get_label()
181 .iter()
182 .map(|label| (label.name().to_string(), label.value().to_string()))
183 .collect(),
184 counter: metric.get_counter().as_ref().unwrap_or_default().value() as i32,
185 }
186 }
187}
188
189impl Default for MetricLabelValueMap {
190 fn default() -> Self {
192 Self {
193 label_value_map: HashMap::new(),
194 counter: 0,
195 }
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use crate::test::TestLogger;
202
203 use super::*;
204
205 #[test]
206 fn should_export_counter_metrics_in_a_map() {
207 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
208 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
209 let metric_b = metrics_service.get_certificate_total_produced_since_startup();
210 metric_a.increment_by(5);
211 metric_b.increment_by(12);
212
213 let export = metrics_service.export_metrics_map();
214 assert_eq!(5, export[&metric_a.name()][""].counter);
215 assert_eq!(12, export[&metric_b.name()][""].counter);
216 }
217
218 #[test]
219 fn should_export_counter_metrics_with_label_in_a_map() {
220 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
221 let metric_a = metrics_service.get_certificate_detail_total_served_since_startup();
222 metric_a.increment_by(&["A", "B"], 5);
223 metric_a.increment_by(&["1", "2"], 12);
224
225 let export = metrics_service.export_metrics_map();
226
227 let metric_a_token_a_b = export[&metric_a.name()]["B,A"].clone();
228 let metric_a_token_1_2 = export[&metric_a.name()]["2,1"].clone();
229
230 assert_eq!(5, metric_a_token_a_b.to_owned().counter);
231 assert_eq!(
232 "A",
233 metric_a_token_a_b.to_owned().label_value_map["origin_tag"]
234 );
235 assert_eq!(
236 "B",
237 metric_a_token_a_b.to_owned().label_value_map["client_type"]
238 );
239
240 assert_eq!(12, metric_a_token_1_2.to_owned().counter);
241 assert_eq!(
242 "1",
243 metric_a_token_1_2.to_owned().label_value_map["origin_tag"]
244 );
245 assert_eq!(
246 "2",
247 metric_a_token_1_2.to_owned().label_value_map["client_type"]
248 );
249 }
250
251 #[test]
252 fn should_export_several_times_and_counter_return_values_since_start() {
253 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
254 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
255 metric_a.increment_by(5);
256
257 let export = metrics_service.export_metrics_map();
258 assert_eq!(5, export[&metric_a.name()][""].counter);
259
260 metric_a.increment();
261 let export = metrics_service.export_metrics_map();
262 assert_eq!(6, export[&metric_a.name()][""].counter);
263 }
264
265 #[test]
266 fn should_export_counter_even_the_value_is_0() {
267 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
268 let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
269
270 let export = metrics_service.export_metrics_map();
271 assert_eq!(0, export[&metric_a.name()][""].counter);
272 }
273
274 #[test]
275 fn metric_service_should_only_contain_counters_as_export_metrics_map_does_not_yet_support_other_types()
276 {
277 let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
278
279 for metric_family in metrics_service.registry.gather() {
280 for metric in metric_family.get_metric() {
281 assert!(metric.get_counter().is_some());
282 }
283 }
284 }
285}