cat_gateway/metrics/chain_follower/
mod.rs

1//! Metrics related to Chain Follower analytics.
2
3use cardano_blockchain_types::Network;
4use cardano_chain_follower::Statistics;
5
6use crate::settings::Settings;
7
8mod reporter;
9
10/// Updates Chain Follower metrics to current values.
11pub(crate) fn update() {
12    let api_host_names = Settings::api_host_names().join(",");
13    let service_id = Settings::service_id();
14    let network = Settings::cardano_network();
15
16    let follower_stats = Statistics::new(network);
17
18    report_mithril(&follower_stats, &api_host_names, service_id, network);
19    report_live(&follower_stats, &api_host_names, service_id, network);
20}
21
22/// Performs reporting Chain Follower's Mithril information to Prometheus.
23#[allow(clippy::indexing_slicing)]
24fn report_mithril(stats: &Statistics, api_host_names: &str, service_id: &str, network: Network) {
25    let stats = &stats.mithril;
26    let network = network.to_string();
27
28    reporter::MITHRIL_UPDATES
29        .with_label_values(&[api_host_names, service_id, &network])
30        .set(i64::try_from(stats.updates).unwrap_or(-1));
31    reporter::MITHRIL_TIP
32        .with_label_values(&[api_host_names, service_id, &network])
33        .set(i64::try_from(u64::from(stats.tip)).unwrap_or(-1));
34    reporter::MITHRIL_DL_START
35        .with_label_values(&[api_host_names, service_id, &network])
36        .set(stats.dl_start.timestamp());
37    reporter::MITHRIL_DL_END
38        .with_label_values(&[api_host_names, service_id, &network])
39        .set(stats.dl_end.timestamp());
40    reporter::MITHRIL_DL_FAILURES
41        .with_label_values(&[api_host_names, service_id, &network])
42        .set(i64::try_from(stats.dl_failures).unwrap_or(-1));
43    reporter::MITHRIL_LAST_DL_DURATION
44        .with_label_values(&[api_host_names, service_id, &network])
45        .set(i64::try_from(stats.last_dl_duration).unwrap_or(-1));
46    reporter::MITHRIL_DL_SIZE
47        .with_label_values(&[api_host_names, service_id, &network])
48        .set(i64::try_from(stats.dl_size).unwrap_or(-1));
49    reporter::MITHRIL_EXTRACT_START
50        .with_label_values(&[api_host_names, service_id, &network])
51        .set(stats.extract_start.timestamp());
52    reporter::MITHRIL_EXTRACT_END
53        .with_label_values(&[api_host_names, service_id, &network])
54        .set(stats.extract_end.timestamp());
55    reporter::MITHRIL_EXTRACT_FAILURES
56        .with_label_values(&[api_host_names, service_id, &network])
57        .set(i64::try_from(stats.extract_failures).unwrap_or(-1));
58    reporter::MITHRIL_EXTRACT_SIZE
59        .with_label_values(&[api_host_names, service_id, &network])
60        .set(i64::try_from(stats.extract_size).unwrap_or(-1));
61    reporter::MITHRIL_DEDUPLICATED_SIZE
62        .with_label_values(&[api_host_names, service_id, &network])
63        .set(i64::try_from(stats.deduplicated_size).unwrap_or(-1));
64    reporter::MITHRIL_DEDUPLICATED
65        .with_label_values(&[api_host_names, service_id, &network])
66        .set(i64::try_from(stats.deduplicated).unwrap_or(-1));
67    reporter::MITHRIL_CHANGED
68        .with_label_values(&[api_host_names, service_id, &network])
69        .set(i64::try_from(stats.changed).unwrap_or(-1));
70    reporter::MITHRIL_NEW
71        .with_label_values(&[api_host_names, service_id, &network])
72        .set(i64::try_from(stats.new).unwrap_or(-1));
73    reporter::MITHRIL_VALIDATE_START
74        .with_label_values(&[api_host_names, service_id, &network])
75        .set(stats.validate_start.timestamp());
76    reporter::MITHRIL_VALIDATE_END
77        .with_label_values(&[api_host_names, service_id, &network])
78        .set(stats.validate_end.timestamp());
79    reporter::MITHRIL_VALIDATE_FAILURES
80        .with_label_values(&[api_host_names, service_id, &network])
81        .set(i64::try_from(stats.validate_failures).unwrap_or(-1));
82    reporter::MITHRIL_INVALID_BLOCKS
83        .with_label_values(&[api_host_names, service_id, &network])
84        .set(i64::try_from(stats.invalid_blocks).unwrap_or(-1));
85    reporter::MITHRIL_DOWNLOAD_OR_VALIDATION_FAILED
86        .with_label_values(&[api_host_names, service_id, &network])
87        .set(i64::try_from(stats.download_or_validation_failed).unwrap_or(-1));
88    reporter::MITHRIL_FAILED_TO_GET_TIP
89        .with_label_values(&[api_host_names, service_id, &network])
90        .set(i64::try_from(stats.failed_to_get_tip).unwrap_or(-1));
91    reporter::MITHRIL_TIP_DID_NOT_ADVANCE
92        .with_label_values(&[api_host_names, service_id, &network])
93        .set(i64::try_from(stats.tip_did_not_advance).unwrap_or(-1));
94    reporter::MITHRIL_TIP_FAILED_TO_SEND_TO_UPDATER
95        .with_label_values(&[api_host_names, service_id, &network])
96        .set(i64::try_from(stats.tip_failed_to_send_to_updater).unwrap_or(-1));
97    reporter::MITHRIL_FAILED_TO_ACTIVATE_NEW_SNAPSHOT
98        .with_label_values(&[api_host_names, service_id, &network])
99        .set(i64::try_from(stats.failed_to_activate_new_snapshot).unwrap_or(-1));
100}
101
102/// Performs reporting Chain Follower's Live information to Prometheus.
103#[allow(clippy::indexing_slicing)]
104fn report_live(stats: &Statistics, api_host_names: &str, service_id: &str, network: Network) {
105    let stats = &stats.live;
106    let network = network.to_string();
107
108    reporter::LIVE_SYNC_START
109        .with_label_values(&[api_host_names, service_id, &network])
110        .set(stats.sync_start.timestamp());
111    reporter::LIVE_SYNC_END
112        .with_label_values(&[api_host_names, service_id, &network])
113        .set(stats.sync_end.map_or(0, |s| s.timestamp()));
114    reporter::LIVE_BACKFILL_START
115        .with_label_values(&[api_host_names, service_id, &network])
116        .set(stats.backfill_start.map_or(0, |s| s.timestamp()));
117    reporter::LIVE_BACKFILL_SIZE
118        .with_label_values(&[api_host_names, service_id, &network])
119        .set(i64::try_from(stats.backfill_size).unwrap_or(-1));
120    reporter::LIVE_BACKFILL_END
121        .with_label_values(&[api_host_names, service_id, &network])
122        .set(stats.backfill_end.map_or(0, |s| s.timestamp()));
123    reporter::LIVE_BACKFILL_FAILURES
124        .with_label_values(&[api_host_names, service_id, &network])
125        .set(i64::try_from(stats.backfill_failures).unwrap_or(-1));
126    reporter::LIVE_BACKFILL_FAILURE_TIME
127        .with_label_values(&[api_host_names, service_id, &network])
128        .set(stats.backfill_failure_time.map_or(0, |s| s.timestamp()));
129    reporter::LIVE_BLOCKS
130        .with_label_values(&[api_host_names, service_id, &network])
131        .set(i64::try_from(stats.blocks).unwrap_or(-1));
132    reporter::LIVE_HEAD_SLOT
133        .with_label_values(&[api_host_names, service_id, &network])
134        .set(i64::try_from(u64::from(stats.head_slot)).unwrap_or(-1));
135    reporter::LIVE_TIP
136        .with_label_values(&[api_host_names, service_id, &network])
137        .set(i64::try_from(u64::from(stats.tip)).unwrap_or(-1));
138    reporter::LIVE_RECONNECTS
139        .with_label_values(&[api_host_names, service_id, &network])
140        .set(i64::try_from(stats.reconnects).unwrap_or(-1));
141    reporter::LIVE_LAST_CONNECT
142        .with_label_values(&[api_host_names, service_id, &network])
143        .set(stats.last_connect.timestamp());
144    reporter::LIVE_LAST_DISCONNECT
145        .with_label_values(&[api_host_names, service_id, &network])
146        .set(stats.last_disconnect.timestamp());
147    reporter::LIVE_CONNECTED
148        .with_label_values(&[api_host_names, service_id, &network])
149        .set(i64::from(stats.connected));
150    reporter::LIVE_ROLLBACKS_LIVE_COUNT
151        .with_label_values(&[api_host_names, service_id, &network])
152        .set(i64::try_from(stats.rollbacks.live.len()).unwrap_or(-1));
153    reporter::LIVE_ROLLBACKS_PEER_COUNT
154        .with_label_values(&[api_host_names, service_id, &network])
155        .set(i64::try_from(stats.rollbacks.peer.len()).unwrap_or(-1));
156    reporter::LIVE_ROLLBACKS_FOLLOWER_COUNT
157        .with_label_values(&[api_host_names, service_id, &network])
158        .set(i64::try_from(stats.rollbacks.follower.len()).unwrap_or(-1));
159    reporter::LIVE_NEW_BLOCKS
160        .with_label_values(&[api_host_names, service_id, &network])
161        .set(i64::try_from(stats.new_blocks).unwrap_or(-1));
162    reporter::LIVE_INVALID_BLOCKS
163        .with_label_values(&[api_host_names, service_id, &network])
164        .set(i64::try_from(stats.invalid_blocks).unwrap_or(-1));
165    reporter::LIVE_FOLLOWER_COUNT
166        .with_label_values(&[api_host_names, service_id, &network])
167        .set(i64::try_from(stats.follower.len()).unwrap_or(-1));
168}