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(
47        live_slot: Slot,
48        immutable_slot: Slot,
49    ) {
50        let api_host_names = Settings::api_host_names().join(",");
51        let service_id = Settings::service_id();
52        let network = Settings::cardano_network().to_string();
53
54        reporter::CURRENT_LIVE_TIP_SLOT
55            .with_label_values(&[&api_host_names, service_id, &network])
56            .set(i64::try_from(u64::from(live_slot)).unwrap_or(-1));
57        reporter::CURRENT_IMMUTABLE_TIP_SLOT
58            .with_label_values(&[&api_host_names, service_id, &network])
59            .set(i64::try_from(u64::from(immutable_slot)).unwrap_or(-1));
60        reporter::SLOT_TIP_DIFF
61            .with_label_values(&[&api_host_names, service_id, &network])
62            .set(
63                u64::from(live_slot.sub(immutable_slot))
64                    .try_into()
65                    .unwrap_or(-1),
66            );
67    }
68
69    /// Triggers to update the highest indexed slot progress.
70    pub(crate) fn highest_complete_indexed_slot(slot: Slot) {
71        let api_host_names = Settings::api_host_names().join(",");
72        let service_id = Settings::service_id();
73        let network = Settings::cardano_network().to_string();
74
75        reporter::HIGHEST_COMPLETE_INDEXED_SLOT
76            .with_label_values(&[&api_host_names, service_id, &network])
77            .set(i64::try_from(u64::from(slot)).unwrap_or(-1));
78    }
79
80    /// Triggers to update when backward data is purged.
81    pub(crate) fn backward_data_purge() {
82        let api_host_names = Settings::api_host_names().join(",");
83        let service_id = Settings::service_id();
84        let network = Settings::cardano_network().to_string();
85
86        reporter::TRIGGERED_BACKWARD_PURGES_COUNT
87            .with_label_values(&[&api_host_names, service_id, &network])
88            .inc();
89    }
90
91    /// Triggers to update when forward data is purged.
92    pub(crate) fn forward_data_purge(purge_slots: u64) {
93        let api_host_names = Settings::api_host_names().join(",");
94        let service_id = Settings::service_id();
95        let network = Settings::cardano_network().to_string();
96
97        reporter::TRIGGERED_FORWARD_PURGES_COUNT
98            .with_label_values(&[&api_host_names, service_id, &network])
99            .inc();
100        reporter::PURGED_SLOTS
101            .with_label_values(&[&api_host_names, service_id, &network])
102            .set(i64::try_from(purge_slots).unwrap_or(-1));
103    }
104}
105
106/// All the related Chain Indexer reporting metrics to the Prometheus service are inside
107/// this module.
108pub(crate) mod reporter {
109    #![allow(clippy::unwrap_used)]
110
111    use std::sync::LazyLock;
112
113    use prometheus::{
114        register_int_counter_vec, register_int_gauge_vec, IntCounterVec, IntGaugeVec,
115    };
116
117    /// Labels for the metrics.
118    const METRIC_LABELS: [&str; 3] = ["api_host_names", "service_id", "network"];
119
120    /// Chain Indexer current live tip slot.
121    pub(crate) static CURRENT_LIVE_TIP_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
122        register_int_gauge_vec!(
123            "indexer_current_live_tip_slot",
124            "Chain Indexer current live tip slot",
125            &METRIC_LABELS
126        )
127        .unwrap()
128    });
129
130    /// Chain Indexer current immutable tip slot.
131    pub(crate) static CURRENT_IMMUTABLE_TIP_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
132        register_int_gauge_vec!(
133            "indexer_current_immutable_tip_slot",
134            "Chain Indexer current immutable tip slot",
135            &METRIC_LABELS
136        )
137        .unwrap()
138    });
139
140    /// Chain Indexer highest complete indexed slot.
141    pub(crate) static HIGHEST_COMPLETE_INDEXED_SLOT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
142        register_int_gauge_vec!(
143            "indexer_highest_complete_indexed_slot",
144            "Chain Indexer highest complete indexed slot",
145            &METRIC_LABELS
146        )
147        .unwrap()
148    });
149
150    /// Chain Indexer number of current running indexer tasks.
151    pub(crate) static RUNNING_INDEXER_TASKS_COUNT: LazyLock<IntGaugeVec> = LazyLock::new(|| {
152        register_int_gauge_vec!(
153            "indexer_running_indexer_tasks_count",
154            "Chain Indexer number of current running indexer tasks",
155            &METRIC_LABELS
156        )
157        .unwrap()
158    });
159
160    /// Chain Indexer number of times triggering backward data purge.
161    pub(crate) static TRIGGERED_BACKWARD_PURGES_COUNT: LazyLock<IntCounterVec> =
162        LazyLock::new(|| {
163            register_int_counter_vec!(
164                "indexer_triggered_backward_purges_count",
165                "Chain Indexer number of times triggering backward data purge",
166                &METRIC_LABELS
167            )
168            .unwrap()
169        });
170
171    /// Chain Indexer number of times triggering forward data purge.
172    pub(crate) static TRIGGERED_FORWARD_PURGES_COUNT: LazyLock<IntCounterVec> =
173        LazyLock::new(|| {
174            register_int_counter_vec!(
175                "indexer_triggered_forward_purges_count",
176                "Chain Indexer number of times triggering forward data purge",
177                &METRIC_LABELS
178            )
179            .unwrap()
180        });
181
182    /// Chain Indexer number of purged slots.
183    pub(crate) static PURGED_SLOTS: LazyLock<IntGaugeVec> = LazyLock::new(|| {
184        register_int_gauge_vec!(
185            "indexer_triggered_purged_slots",
186            "Chain Indexer total number of slots that have already been purged",
187            &METRIC_LABELS
188        )
189        .unwrap()
190    });
191
192    /// Chain Indexer indicator whether immutable tip is reached or in progress.
193    pub(crate) static IMMUTABLE_REACHED_TIP: LazyLock<IntGaugeVec> = LazyLock::new(|| {
194        register_int_gauge_vec!(
195            "indexer_reached_immutable_tip_indicator",
196            "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.",
197            &METRIC_LABELS
198        )
199        .unwrap()
200    });
201
202    /// Chain Indexer indicator whether live tip is reached or in progress.
203    pub(crate) static LIVE_REACHED_TIP: LazyLock<IntGaugeVec> = LazyLock::new(|| {
204        register_int_gauge_vec!(
205            "indexer_reached_live_tip_indicator",
206            "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.",
207            &METRIC_LABELS
208        )
209        .unwrap()
210    });
211
212    /// The slot difference between immutable tip slot and the volatile tip slot number.
213    pub(crate) static SLOT_TIP_DIFF: LazyLock<IntGaugeVec> = LazyLock::new(|| {
214        register_int_gauge_vec!(
215            "indexer_slot_tip_diff",
216            "The slot difference between immutable tip slot and the volatile tip slot number",
217            &METRIC_LABELS
218        )
219        .unwrap()
220    });
221}