cat_gateway/metrics/
chain_indexer.rs

1//! Metrics related to Chain Indexer analytics.
2
3/// All the related Chain Indexer metrics update functions
4pub(crate) mod metrics_updater {
5    use std::ops::Sub;
6
7    use cardano_chain_follower::Slot;
8
9    use crate::{metrics::chain_indexer::reporter, settings::Settings};
10
11    /// Triggers to indicate that the Chain Indexer is starting or reaches the live tip.
12    pub(crate) fn reached_live_tip(reached: bool) {
13        let api_host_names = Settings::api_host_names().join(",");
14        let service_id = Settings::service_id();
15        let network = Settings::cardano_network().to_string();
16
17        reporter::LIVE_REACHED_TIP
18            .with_label_values(&[&api_host_names, service_id, &network])
19            .set(i64::from(reached));
20    }
21
22    /// Triggers to indicate that the Chain Indexer is starting or reaches the immutable
23    /// tip.
24    pub(crate) fn reached_immutable_tip(reached: bool) {
25        let api_host_names = Settings::api_host_names().join(",");
26        let service_id = Settings::service_id();
27        let network = Settings::cardano_network().to_string();
28
29        reporter::IMMUTABLE_REACHED_TIP
30            .with_label_values(&[&api_host_names, service_id, &network])
31            .set(i64::from(reached));
32    }
33
34    /// Triggers to update the number of current synchronization tasks.
35    pub(crate) fn sync_tasks(current_sync_tasks: u16) {
36        let api_host_names = Settings::api_host_names().join(",");
37        let service_id = Settings::service_id();
38        let network = Settings::cardano_network().to_string();
39
40        reporter::RUNNING_INDEXER_TASKS_COUNT
41            .with_label_values(&[&api_host_names, service_id, &network])
42            .set(From::from(current_sync_tasks));
43    }
44
45    /// Triggers to update the tip for both live and immutable slots.
46    pub(crate) fn current_tip_slot(live_slot: Slot, immutable_slot: Slot) {
47        let api_host_names = Settings::api_host_names().join(",");
48        let service_id = Settings::service_id();
49        let network = Settings::cardano_network().to_string();
50
51        reporter::CURRENT_LIVE_TIP_SLOT
52            .with_label_values(&[&api_host_names, service_id, &network])
53            .set(i64::try_from(u64::from(live_slot)).unwrap_or(-1));
54        reporter::CURRENT_IMMUTABLE_TIP_SLOT
55            .with_label_values(&[&api_host_names, service_id, &network])
56            .set(i64::try_from(u64::from(immutable_slot)).unwrap_or(-1));
57        reporter::SLOT_TIP_DIFF
58            .with_label_values(&[&api_host_names, service_id, &network])
59            .set(
60                u64::from(live_slot.sub(immutable_slot))
61                    .try_into()
62                    .unwrap_or(-1),
63            );
64    }
65
66    /// Triggers to update the highest indexed slot progress.
67    pub(crate) fn highest_complete_indexed_slot(slot: Slot) {
68        let api_host_names = Settings::api_host_names().join(",");
69        let service_id = Settings::service_id();
70        let network = Settings::cardano_network().to_string();
71
72        reporter::HIGHEST_COMPLETE_INDEXED_SLOT
73            .with_label_values(&[&api_host_names, service_id, &network])
74            .set(i64::try_from(u64::from(slot)).unwrap_or(-1));
75    }
76
77    /// Triggers to update when backward data is purged.
78    pub(crate) fn backward_data_purge() {
79        let api_host_names = Settings::api_host_names().join(",");
80        let service_id = Settings::service_id();
81        let network = Settings::cardano_network().to_string();
82
83        reporter::TRIGGERED_BACKWARD_PURGES_COUNT
84            .with_label_values(&[&api_host_names, service_id, &network])
85            .inc();
86    }
87
88    /// Triggers to update when forward data is purged.
89    pub(crate) fn forward_data_purge(purge_slots: u64) {
90        let api_host_names = Settings::api_host_names().join(",");
91        let service_id = Settings::service_id();
92        let network = Settings::cardano_network().to_string();
93
94        reporter::TRIGGERED_FORWARD_PURGES_COUNT
95            .with_label_values(&[&api_host_names, service_id, &network])
96            .inc();
97        reporter::PURGED_SLOTS
98            .with_label_values(&[&api_host_names, service_id, &network])
99            .set(i64::try_from(purge_slots).unwrap_or(-1));
100    }
101}
102
103/// All the related Chain Indexer reporting metrics to the Prometheus service are inside
104/// this module.
105pub(crate) mod reporter {
106    use std::sync::LazyLock;
107
108    use prometheus::{
109        register_int_counter_vec, register_int_gauge_vec, IntCounterVec, IntGaugeVec,
110    };
111
112    /// Labels for the metrics.
113    const METRIC_LABELS: [&str; 3] = ["api_host_names", "service_id", "network"];
114
115    /// Chain Indexer current live tip slot.
116    pub(crate) static CURRENT_LIVE_TIP_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
117        register_int_gauge_vec!(
118            "indexer_current_live_tip_slot",
119            "Chain Indexer current live tip slot",
120            &METRIC_LABELS
121        )
122        .unwrap()
123    });
124
125    /// Chain Indexer current immutable tip slot.
126    pub(crate) static CURRENT_IMMUTABLE_TIP_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
127        register_int_gauge_vec!(
128            "indexer_current_immutable_tip_slot",
129            "Chain Indexer current immutable tip slot",
130            &METRIC_LABELS
131        )
132        .unwrap()
133    });
134
135    /// Chain Indexer highest complete indexed slot.
136    pub(crate) static HIGHEST_COMPLETE_INDEXED_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
137        register_int_gauge_vec!(
138            "indexer_highest_complete_indexed_slot",
139            "Chain Indexer highest complete indexed slot",
140            &METRIC_LABELS
141        )
142        .unwrap()
143    });
144
145    /// Chain Indexer number of current running indexer tasks.
146    pub(crate) static RUNNING_INDEXER_TASKS_COUNT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
147        register_int_gauge_vec!(
148            "indexer_running_indexer_tasks_count",
149            "Chain Indexer number of current running indexer tasks",
150            &METRIC_LABELS
151        )
152        .unwrap()
153    });
154
155    /// Chain Indexer number of times triggering backward data purge.
156    pub(crate) static TRIGGERED_BACKWARD_PURGES_COUNT: LazyLock<IntCounterVec> =
157        LazyLock::new(|| {
158            register_int_counter_vec!(
159                "indexer_triggered_backward_purges_count",
160                "Chain Indexer number of times triggering backward data purge",
161                &METRIC_LABELS
162            )
163            .unwrap()
164        });
165
166    /// Chain Indexer number of times triggering forward data purge.
167    pub(crate) static TRIGGERED_FORWARD_PURGES_COUNT: LazyLock<IntCounterVec> =
168        LazyLock::new(|| {
169            register_int_counter_vec!(
170                "indexer_triggered_forward_purges_count",
171                "Chain Indexer number of times triggering forward data purge",
172                &METRIC_LABELS
173            )
174            .unwrap()
175        });
176
177    /// Chain Indexer number of purged slots.
178    pub(crate) static PURGED_SLOTS: LazyLock<IntGaugeVec> = LazyLock::new(|| {
179        register_int_gauge_vec!(
180            "indexer_triggered_purged_slots",
181            "Chain Indexer total number of slots that have already been purged",
182            &METRIC_LABELS
183        )
184        .unwrap()
185    });
186
187    /// Chain Indexer indicator whether immutable tip is reached or in progress.
188    pub(crate) static IMMUTABLE_REACHED_TIP: LazyLock<IntGaugeVec> = LazyLock::new(|| {
189        register_int_gauge_vec!(
190            "indexer_reached_immutable_tip_indicator",
191            "Chain Indexer indicator whether immutable chain tip is reached or in progress. It also reflects a freshly downloaded mithril snapshots, when new immutable synchronization starts.",
192            &METRIC_LABELS
193        )
194        .unwrap()
195    });
196
197    /// Chain Indexer indicator whether live tip is reached or in progress.
198    pub(crate) static LIVE_REACHED_TIP: LazyLock<IntGaugeVec> = LazyLock::new(|| {
199        register_int_gauge_vec!(
200            "indexer_reached_live_tip_indicator",
201            "Chain Indexer indicator whether live chain tip is reached or in progress. Live chain synchronization never stops, this metric shows when first tip is reached.",
202            &METRIC_LABELS
203        )
204        .unwrap()
205    });
206
207    /// The slot difference between immutable tip slot and the volatile tip slot number.
208    pub(crate) static SLOT_TIP_DIFF: LazyLock<IntGaugeVec> = LazyLock::new(|| {
209        register_int_gauge_vec!(
210            "indexer_slot_tip_diff",
211            "The slot difference between immutable tip slot and the volatile tip slot number",
212            &METRIC_LABELS
213        )
214        .unwrap()
215    });
216}