mithril_aggregator/metrics/
service.rs

1use std::collections::HashMap;
2
3use mithril_metric::{build_metrics_service, MetricsServiceExporter};
4
5use mithril_metric::metric::{MetricCollector, MetricCounter};
6
7build_metrics_service!(
8    MetricsService,
9    certificate_detail_total_served_since_startup:MetricCounter(
10        "mithril_aggregator_certificate_detail_total_served_since_startup",
11        "Number of certificate details served since startup on a Mithril aggregator node"
12    ),
13    artifact_detail_cardano_immutable_files_full_total_served_since_startup:MetricCounter(
14        "mithril_aggregator_artifact_detail_cardano_db_total_served_since_startup",
15        "Number of Cardano immutable files full artifact details served since startup on a Mithril aggregator node"
16    ),
17    cardano_immutable_files_full_total_restoration_since_startup:MetricCounter(
18        "mithril_aggregator_cardano_db_total_restoration_since_startup",
19        "Number of Cardano immutable files full restorations since startup on a Mithril aggregator node"
20    ),
21    cardano_database_immutable_files_restored_since_startup:MetricCounter(
22        "mithril_aggregator_cardano_db_immutable_files_restored_since_startup",
23        "Number of Cardano immutable files restored since startup on a Mithril aggregator node"
24    ),
25    cardano_database_ancillary_files_restored_since_startup:MetricCounter(
26        "mithril_aggregator_cardano_db_ancillary_files_restored_since_startup",
27        "Number of Cardano ancillary files restored since startup on a Mithril aggregator node"
28    ),
29    cardano_database_complete_restoration_since_startup:MetricCounter(
30        "mithril_aggregator_cardano_db_complete_restoration_since_startup",
31        "Number of complete Cardano database restoration since startup on a Mithril aggregator node"
32    ),
33    cardano_database_partial_restoration_since_startup:MetricCounter(
34        "mithril_aggregator_cardano_db_partial_restoration_since_startup",
35        "Number of partial Cardano database restoration since startup on a Mithril aggregator node"
36    ),
37    artifact_detail_cardano_database_total_served_since_startup:MetricCounter(
38        "mithril_aggregator_artifact_detail_cardano_database_total_served_since_startup",
39        "Number of Cardano database artifact details served since startup on a Mithril aggregator node"
40    ),
41    artifact_detail_mithril_stake_distribution_total_served_since_startup:MetricCounter(
42        "mithril_aggregator_artifact_detail_mithril_stake_distribution_total_served_since_startup",
43        "Number of Mithril stake distribution artifact details served since startup on a Mithril aggregator node"
44    ),
45    artifact_detail_cardano_stake_distribution_total_served_since_startup:MetricCounter(
46        "mithril_aggregator_artifact_detail_cardano_stake_distribution_total_served_since_startup",
47        "Number of Cardano stake distribution artifact details served since startup on a Mithril aggregator node"
48    ),
49    artifact_detail_cardano_transaction_total_served_since_startup:MetricCounter(
50        "mithril_aggregator_artifact_detail_cardano_transaction_total_served_since_startup",
51        "Number of Cardano transaction artifact details served since startup on a Mithril aggregator node"
52    ),
53    proof_cardano_transaction_total_proofs_served_since_startup:MetricCounter(
54        "mithril_aggregator_proof_cardano_transaction_total_proofs_served_since_startup",
55        "Number of Cardano transaction proofs served since startup on a Mithril aggregator node"
56    ),
57    proof_cardano_transaction_total_transactions_served_since_startup:MetricCounter(
58        "mithril_aggregator_proof_cardano_transaction_total_transactions_served_since_startup",
59        "Number of Cardano transaction hashes requested for proof since startup on a Mithril aggregator node"
60    ),
61    signer_registration_total_received_since_startup:MetricCounter(
62        "mithril_aggregator_signer_registration_total_received_since_startup",
63        "Number of signer registrations received since startup on a Mithril aggregator node"
64    ),
65    signature_registration_total_received_since_startup:MetricCounter(
66        "mithril_aggregator_signature_registration_total_received_since_startup",
67        "Number of signature registrations received since startup on a Mithril aggregator node"
68    ),
69    certificate_total_produced_since_startup:MetricCounter(
70        "mithril_aggregator_certificate_total_produced_since_startup",
71        "Number of certificates produced since startup on a Mithril aggregator node"
72    ),
73    artifact_cardano_immutable_files_full_total_produced_since_startup:MetricCounter(
74        "mithril_aggregator_artifact_cardano_db_total_produced_since_startup",
75        "Number of Cardano immutable files full artifacts produced since startup on a Mithril aggregator node"
76    ),
77    artifact_cardano_database_total_produced_since_startup:MetricCounter(
78        "mithril_aggregator_artifact_cardano_database_total_produced_since_startup",
79        "Number of Cardano database artifacts produced since startup on a Mithril aggregator node"
80    ),
81    artifact_mithril_stake_distribution_total_produced_since_startup:MetricCounter(
82        "mithril_aggregator_artifact_mithril_stake_distribution_total_produced_since_startup",
83        "Number of Mithril stake distribution artifacts produced since startup on a Mithril aggregator node"
84    ),
85    artifact_cardano_stake_distribution_total_produced_since_startup:MetricCounter(
86        "mithril_aggregator_artifact_cardano_stake_distribution_total_produced_since_startup",
87        "Number of Cardano stake distribution artifacts produced since startup on a Mithril aggregator node"
88    ),
89    artifact_cardano_transaction_total_produced_since_startup:MetricCounter(
90        "mithril_aggregator_artifact_cardano_transaction_total_produced_since_startup",
91        "Number of Cardano transaction artifacts produced since startup on a Mithril aggregator node"
92    ),
93    runtime_cycle_success_since_startup:MetricCounter(
94        "mithril_aggregator_runtime_cycle_success_since_startup",
95        "Number of successful runtime cycles since startup on a Mithril aggregator"
96    ),
97    runtime_cycle_total_since_startup:MetricCounter(
98        "mithril_aggregator_runtime_cycle_total_since_startup",
99        "Number of runtime cycles since startup on a Mithril aggregator"
100    )
101
102);
103
104impl MetricsService {
105    /// Export metrics in map.
106    // `get metric` returns a list of Metrics for CounterVec purposes for example.
107    // We therefore add up the values ​​even though we will always only have one value with our Counter type metrics.
108    pub fn export_metrics_map(&self) -> HashMap<String, u32> {
109        self.registry
110            .gather()
111            .iter()
112            .map(|metric_family| {
113                (
114                    metric_family.get_name().to_string(),
115                    metric_family
116                        .get_metric()
117                        .iter()
118                        .map(|m| m.get_counter().get_value() as u32)
119                        .sum(),
120                )
121            })
122            .collect()
123    }
124}
125
126#[cfg(test)]
127mod tests {
128    use crate::test_tools::TestLogger;
129
130    use super::*;
131
132    #[test]
133    fn should_export_counter_metrics_in_a_map() {
134        let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
135        let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
136        let metric_b = metrics_service.get_certificate_total_produced_since_startup();
137        metric_a.increment_by(5);
138        metric_b.increment_by(12);
139
140        let export = metrics_service.export_metrics_map();
141        assert_eq!(5, export[&metric_a.name()]);
142        assert_eq!(12, export[&metric_b.name()]);
143    }
144
145    #[test]
146    fn should_export_several_times_and_counter_return_values_since_start() {
147        let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
148        let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
149        metric_a.increment_by(5);
150
151        let export = metrics_service.export_metrics_map();
152        assert_eq!(5, export[&metric_a.name()]);
153
154        metric_a.increment();
155        let export = metrics_service.export_metrics_map();
156        assert_eq!(6, export[&metric_a.name()]);
157    }
158
159    #[test]
160    fn should_export_counter_even_the_value_is_0() {
161        let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
162        let metric_a = metrics_service.get_runtime_cycle_total_since_startup();
163
164        let export = metrics_service.export_metrics_map();
165        assert_eq!(0, export[&metric_a.name()]);
166    }
167
168    #[test]
169    fn metric_service_should_only_contain_counters_as_export_metrics_map_does_not_yet_support_other_types(
170    ) {
171        let metrics_service = MetricsService::new(TestLogger::stdout()).unwrap();
172
173        for metric_family in metrics_service.registry.gather() {
174            for metric in metric_family.get_metric() {
175                assert!(metric.has_counter());
176            }
177        }
178    }
179}