cat_gateway/metrics/caches/
rbac.rs

1//! Metrics related to RBAC Registration Chain Caching analytics.
2
3use super::cache_metric_inc;
4use crate::{db::index::session::CassandraSession, settings::Settings};
5
6/// Represents a persistent session.
7const PERSISTENT: bool = true;
8/// Represents a volatile session.
9const VOLATILE: bool = false;
10
11/// Updates memory metrics to current values.
12pub(crate) fn update() {
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    CassandraSession::get(PERSISTENT).inspect(|session| {
18        let caches = session.caches();
19
20        reporter::PERSISTENT_PUBLIC_KEYS_CACHE_SIZE
21            .with_label_values(&[&api_host_names, service_id, &network])
22            .set(i64::try_from(caches.rbac_public_key().weighted_size()).unwrap_or(-1));
23
24        reporter::PERSISTENT_STAKE_ADDRESSES_CACHE_SIZE
25            .with_label_values(&[&api_host_names, service_id, &network])
26            .set(i64::try_from(caches.rbac_stake_address().weighted_size()).unwrap_or(-1));
27
28        reporter::PERSISTENT_TRANSACTION_IDS_CACHE_SIZE
29            .with_label_values(&[&api_host_names, service_id, &network])
30            .set(i64::try_from(caches.rbac_transaction_id().weighted_size()).unwrap_or(-1));
31
32        reporter::PERSISTENT_CHAINS_CACHE_SIZE
33            .with_label_values(&[&api_host_names, service_id, &network])
34            .set(i64::try_from(caches.rbac_persistent_chains().weighted_size()).unwrap_or(-1));
35
36        reporter::PERSISTENT_PUBLIC_KEYS_CACHE_ENTRY_COUNT
37            .with_label_values(&[&api_host_names, service_id, &network])
38            .set(i64::try_from(caches.rbac_public_key().entry_count()).unwrap_or(-1));
39
40        reporter::PERSISTENT_STAKE_ADDRESSES_CACHE_ENTRY_COUNT
41            .with_label_values(&[&api_host_names, service_id, &network])
42            .set(i64::try_from(caches.rbac_stake_address().entry_count()).unwrap_or(-1));
43
44        reporter::PERSISTENT_TRANSACTION_IDS_CACHE_ENTRY_COUNT
45            .with_label_values(&[&api_host_names, service_id, &network])
46            .set(i64::try_from(caches.rbac_transaction_id().entry_count()).unwrap_or(-1));
47
48        reporter::PERSISTENT_CHAINS_CACHE_ENTRY_COUNT
49            .with_label_values(&[&api_host_names, service_id, &network])
50            .set(i64::try_from(caches.rbac_persistent_chains().entry_count()).unwrap_or(-1));
51    });
52
53    CassandraSession::get(VOLATILE).inspect(|session| {
54        let caches = session.caches();
55
56        reporter::VOLATILE_PUBLIC_KEYS_CACHE_SIZE
57            .with_label_values(&[&api_host_names, service_id, &network])
58            .set(i64::try_from(caches.rbac_public_key().weighted_size()).unwrap_or(-1));
59
60        reporter::VOLATILE_STAKE_ADDRESSES_CACHE_SIZE
61            .with_label_values(&[&api_host_names, service_id, &network])
62            .set(i64::try_from(caches.rbac_stake_address().weighted_size()).unwrap_or(-1));
63
64        reporter::VOLATILE_TRANSACTION_IDS_CACHE_SIZE
65            .with_label_values(&[&api_host_names, service_id, &network])
66            .set(i64::try_from(caches.rbac_transaction_id().weighted_size()).unwrap_or(-1));
67
68        reporter::VOLATILE_PUBLIC_KEYS_CACHE_ENTRY_COUNT
69            .with_label_values(&[&api_host_names, service_id, &network])
70            .set(i64::try_from(caches.rbac_public_key().entry_count()).unwrap_or(-1));
71
72        reporter::VOLATILE_STAKE_ADDRESSES_CACHE_ENTRY_COUNT
73            .with_label_values(&[&api_host_names, service_id, &network])
74            .set(i64::try_from(caches.rbac_stake_address().entry_count()).unwrap_or(-1));
75
76        reporter::VOLATILE_TRANSACTION_IDS_CACHE_ENTRY_COUNT
77            .with_label_values(&[&api_host_names, service_id, &network])
78            .set(i64::try_from(caches.rbac_transaction_id().entry_count()).unwrap_or(-1));
79    });
80}
81
82/// Increments the `VOLATILE_STAKE_ADDRESSES_CACHE_HIT` metric.
83pub(crate) fn rbac_volatile_stake_address_cache_hits_inc() {
84    cache_metric_inc!(VOLATILE_STAKE_ADDRESSES_CACHE_HIT);
85}
86
87/// Increments the `VOLATILE_STAKE_ADDRESSES_CACHE_HIT` metric.
88pub(crate) fn rbac_volatile_stake_address_cache_miss_inc() {
89    cache_metric_inc!(VOLATILE_STAKE_ADDRESSES_CACHE_MISS);
90}
91
92/// Increments the `PERSISTENT_STAKE_ADDRESSES_CACHE_HIT` metric.
93pub(crate) fn rbac_persistent_stake_address_cache_hits_inc() {
94    cache_metric_inc!(PERSISTENT_STAKE_ADDRESSES_CACHE_HIT);
95}
96
97/// Increments the `PERSISTENT_STAKE_ADDRESSES_CACHE_HIT` metric.
98pub(crate) fn rbac_persistent_stake_address_cache_miss_inc() {
99    cache_metric_inc!(PERSISTENT_STAKE_ADDRESSES_CACHE_MISS);
100}
101
102/// Increments the `PERSISTENT_PUBLIC_KEYS_CACHE_HIT` metric.
103pub(crate) fn rbac_persistent_public_key_cache_hits_inc() {
104    cache_metric_inc!(PERSISTENT_PUBLIC_KEYS_CACHE_HIT);
105}
106
107/// Increments the `PERSISTENT_PUBLIC_KEY_CACHE_MISS` metric.
108pub(crate) fn rbac_persistent_public_key_cache_miss_inc() {
109    cache_metric_inc!(PERSISTENT_PUBLIC_KEYS_CACHE_MISS);
110}
111
112/// Increments the `VOLATILE_PUBLIC_KEYS_CACHE_HIT` metric.
113pub(crate) fn rbac_volatile_public_key_cache_hits_inc() {
114    cache_metric_inc!(VOLATILE_PUBLIC_KEYS_CACHE_HIT);
115}
116
117/// Increments the `VOLATILE_PUBLIC_KEY_CACHE_MISS` metric.
118pub(crate) fn rbac_volatile_public_key_cache_miss_inc() {
119    cache_metric_inc!(VOLATILE_PUBLIC_KEYS_CACHE_MISS);
120}
121
122/// Increments the `PERSISTENT_TRANSACTION_IDS_CACHE_HIT` metric.
123pub(crate) fn rbac_persistent_transaction_id_cache_hits_inc() {
124    cache_metric_inc!(PERSISTENT_TRANSACTION_IDS_CACHE_HIT);
125}
126
127/// Increments the `PERSISTENT_TRANSACTION_IDS_CACHE_MISS` metric.
128pub(crate) fn rbac_persistent_transaction_id_cache_miss_inc() {
129    cache_metric_inc!(PERSISTENT_TRANSACTION_IDS_CACHE_MISS);
130}
131
132/// Increments the `VOLATILE_TRANSACTION_IDS_CACHE_HIT` metric.
133pub(crate) fn rbac_volatile_transaction_id_cache_hits_inc() {
134    cache_metric_inc!(VOLATILE_TRANSACTION_IDS_CACHE_HIT);
135}
136
137/// Increments the `VOLATILE_TRANSACTION_IDS_CACHE_MISS` metric.
138pub(crate) fn rbac_volatile_transaction_id_cache_miss_inc() {
139    cache_metric_inc!(VOLATILE_TRANSACTION_IDS_CACHE_MISS);
140}
141
142/// Increments the `PERSISTENT_CHAINS_CACHE_HIT` metric.
143pub(crate) fn rbac_persistent_chains_cache_hits_inc() {
144    cache_metric_inc!(PERSISTENT_CHAINS_CACHE_HIT);
145}
146
147/// Increments the `PERSISTENT_CHAINS_CACHE_MISS` metric.
148pub(crate) fn rbac_persistent_chains_cache_miss_inc() {
149    cache_metric_inc!(PERSISTENT_CHAINS_CACHE_MISS);
150}
151
152/// Increments the `INDEXING_SYNCHRONIZATION_COUNT` metric.
153pub(crate) fn inc_index_sync() {
154    cache_metric_inc!(INDEXING_SYNCHRONIZATION_COUNT);
155}
156
157/// Increments the `INVALID_RBAC_REGISTRATION_COUNT` metric.
158pub(crate) fn inc_invalid_rbac_reg_count() {
159    cache_metric_inc!(INVALID_RBAC_REGISTRATION_COUNT);
160}
161
162/// All the related RBAC Registration Chain Caching reporting metrics to the Prometheus
163/// service are inside this module.
164pub(crate) mod reporter {
165    #![allow(clippy::unwrap_used)]
166
167    use std::sync::LazyLock;
168
169    use prometheus::{
170        register_int_counter_vec, register_int_gauge_vec, IntCounterVec, IntGaugeVec,
171    };
172
173    /// Labels for the metrics.
174    const METRIC_LABELS: [&str; 3] = ["api_host_names", "service_id", "network"];
175
176    /// A total count of the persistent public keys cache hits.
177    pub(super) static PERSISTENT_PUBLIC_KEYS_CACHE_HIT: LazyLock<IntCounterVec> =
178        LazyLock::new(|| {
179            register_int_counter_vec!(
180                "rbac_persistent_public_keys_cache_hit",
181                "A total count of persistent public keys cache hits",
182                &METRIC_LABELS
183            )
184            .unwrap()
185        });
186
187    /// A total count of the persistent public keys cache misses.
188    pub(super) static PERSISTENT_PUBLIC_KEYS_CACHE_MISS: LazyLock<IntCounterVec> =
189        LazyLock::new(|| {
190            register_int_counter_vec!(
191                "rbac_persistent_public_keys_cache_miss",
192                "A total count of persistent public keys cache misses",
193                &METRIC_LABELS
194            )
195            .unwrap()
196        });
197
198    /// Size in memory allocated bytes of the persistent public keys cache.
199    pub(super) static PERSISTENT_PUBLIC_KEYS_CACHE_SIZE: LazyLock<IntGaugeVec> =
200        LazyLock::new(|| {
201            register_int_gauge_vec!(
202                "rbac_persistent_public_keys_cache_size",
203                "Size in memory allocated bytes of the persistent public keys cache",
204                &METRIC_LABELS
205            )
206            .unwrap()
207        });
208
209    /// Number of entries in the persistent public keys cache.
210    pub(super) static PERSISTENT_PUBLIC_KEYS_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
211        LazyLock::new(|| {
212            register_int_gauge_vec!(
213                "rbac_persistent_public_keys_cache_entry_count",
214                "Number of entries in the persistent public keys cache",
215                &METRIC_LABELS
216            )
217            .unwrap()
218        });
219
220    /// A total count of the volatile public keys cache hits.
221    pub(super) static VOLATILE_PUBLIC_KEYS_CACHE_HIT: LazyLock<IntCounterVec> =
222        LazyLock::new(|| {
223            register_int_counter_vec!(
224                "rbac_volatile_public_keys_cache_hit",
225                "A total count of volatile public keys cache hits",
226                &METRIC_LABELS
227            )
228            .unwrap()
229        });
230
231    /// A total count of the volatile public keys cache misses.
232    pub(super) static VOLATILE_PUBLIC_KEYS_CACHE_MISS: LazyLock<IntCounterVec> =
233        LazyLock::new(|| {
234            register_int_counter_vec!(
235                "rbac_volatile_public_keys_cache_miss",
236                "A total count of volatile public keys cache misses",
237                &METRIC_LABELS
238            )
239            .unwrap()
240        });
241
242    /// Size in memory allocated bytes of the volatile public keys cache.
243    pub(super) static VOLATILE_PUBLIC_KEYS_CACHE_SIZE: LazyLock<IntGaugeVec> =
244        LazyLock::new(|| {
245            register_int_gauge_vec!(
246                "rbac_volatile_public_keys_cache_size",
247                "Size in memory allocated bytes of the volatile public keys cache",
248                &METRIC_LABELS
249            )
250            .unwrap()
251        });
252
253    /// Number of entries in the volatile public keys cache.
254    pub(super) static VOLATILE_PUBLIC_KEYS_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
255        LazyLock::new(|| {
256            register_int_gauge_vec!(
257                "rbac_volatile_public_keys_cache_entry_count",
258                "Number of entries in the volatile public keys cache",
259                &METRIC_LABELS
260            )
261            .unwrap()
262        });
263
264    /// A total count of the persistent stake addresses cache hits.
265    pub(super) static PERSISTENT_STAKE_ADDRESSES_CACHE_HIT: LazyLock<IntCounterVec> =
266        LazyLock::new(|| {
267            register_int_counter_vec!(
268                "rbac_persistent_stake_addresses_cache_hit",
269                "A total count of the persistent stake addresses cache hits",
270                &METRIC_LABELS
271            )
272            .unwrap()
273        });
274
275    /// A total count of the persistent stake addresses cache misses.
276    pub(super) static PERSISTENT_STAKE_ADDRESSES_CACHE_MISS: LazyLock<IntCounterVec> =
277        LazyLock::new(|| {
278            register_int_counter_vec!(
279                "rbac_persistent_stake_addresses_cache_miss",
280                "A total count of the persistent stake addresses cache misses",
281                &METRIC_LABELS
282            )
283            .unwrap()
284        });
285
286    /// Size in memory allocated bytes of the persistent stake addresses cache.
287    pub(super) static PERSISTENT_STAKE_ADDRESSES_CACHE_SIZE: LazyLock<IntGaugeVec> =
288        LazyLock::new(|| {
289            register_int_gauge_vec!(
290                "rbac_persistent_stake_addresses_cache_size",
291                "Size in memory allocated bytes of the persistent stake addresses cache",
292                &METRIC_LABELS
293            )
294            .unwrap()
295        });
296
297    /// Number of entries in the persistent stake addresses cache.
298    pub(super) static PERSISTENT_STAKE_ADDRESSES_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
299        LazyLock::new(|| {
300            register_int_gauge_vec!(
301                "rbac_persistent_stake_addresses_cache_entry_count",
302                "Number of entries in the persistent stake addresses cache",
303                &METRIC_LABELS
304            )
305            .unwrap()
306        });
307
308    /// A total count of the volatile stake addresses cache hits.
309    pub(super) static VOLATILE_STAKE_ADDRESSES_CACHE_HIT: LazyLock<IntCounterVec> =
310        LazyLock::new(|| {
311            register_int_counter_vec!(
312                "rbac_volatile_stake_addresses_cache_hit",
313                "A total count of the volatile stake addresses cache hits",
314                &METRIC_LABELS
315            )
316            .unwrap()
317        });
318
319    /// A total count of the volatile stake addresses cache misses.
320    pub(super) static VOLATILE_STAKE_ADDRESSES_CACHE_MISS: LazyLock<IntCounterVec> =
321        LazyLock::new(|| {
322            register_int_counter_vec!(
323                "rbac_volatile_stake_addresses_cache_miss",
324                "A total count of the volatile stake addresses cache misses",
325                &METRIC_LABELS
326            )
327            .unwrap()
328        });
329
330    /// Size in memory allocated bytes of the volatile stake addresses cache.
331    pub(super) static VOLATILE_STAKE_ADDRESSES_CACHE_SIZE: LazyLock<IntGaugeVec> =
332        LazyLock::new(|| {
333            register_int_gauge_vec!(
334                "rbac_volatile_stake_addresses_cache_size",
335                "Size in memory allocated bytes of the volatile stake addresses cache",
336                &METRIC_LABELS
337            )
338            .unwrap()
339        });
340
341    /// Number of entries in the volatile stake addresses cache.
342    pub(super) static VOLATILE_STAKE_ADDRESSES_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
343        LazyLock::new(|| {
344            register_int_gauge_vec!(
345                "rbac_volatile_stake_addresses_cache_entry_count",
346                "Number of entries in the volatile stake addresses cache",
347                &METRIC_LABELS
348            )
349            .unwrap()
350        });
351
352    /// A total count of the persistent transaction IDs cache hits.
353    pub(super) static PERSISTENT_TRANSACTION_IDS_CACHE_HIT: LazyLock<IntCounterVec> =
354        LazyLock::new(|| {
355            register_int_counter_vec!(
356                "rbac_persistent_transaction_ids_cache_hit",
357                "A total count of the persistent transaction IDs cache hits",
358                &METRIC_LABELS
359            )
360            .unwrap()
361        });
362
363    /// A total count of the persistent transaction IDs cache misses.
364    pub(super) static PERSISTENT_TRANSACTION_IDS_CACHE_MISS: LazyLock<IntCounterVec> =
365        LazyLock::new(|| {
366            register_int_counter_vec!(
367                "rbac_persistent_transaction_ids_cache_miss",
368                "A total count of the persistent transaction IDs cache misses",
369                &METRIC_LABELS
370            )
371            .unwrap()
372        });
373
374    /// Size in memory allocated bytes of the persistent transaction IDs cache.
375    pub(super) static PERSISTENT_TRANSACTION_IDS_CACHE_SIZE: LazyLock<IntGaugeVec> =
376        LazyLock::new(|| {
377            register_int_gauge_vec!(
378                "rbac_persistent_transaction_ids_cache_size",
379                "Size in memory allocated bytes of the persistent transaction IDs cache",
380                &METRIC_LABELS
381            )
382            .unwrap()
383        });
384
385    /// Number of entries in the persistent transaction IDs cache.
386    pub(super) static PERSISTENT_TRANSACTION_IDS_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
387        LazyLock::new(|| {
388            register_int_gauge_vec!(
389                "rbac_persistent_transaction_ids_cache_entry_count",
390                "Number of entries in the persistent transaction IDs cache",
391                &METRIC_LABELS
392            )
393            .unwrap()
394        });
395
396    /// A total count of the volatile transaction IDs cache hits.
397    pub(super) static VOLATILE_TRANSACTION_IDS_CACHE_HIT: LazyLock<IntCounterVec> =
398        LazyLock::new(|| {
399            register_int_counter_vec!(
400                "rbac_volatile_transaction_ids_cache_hit",
401                "A total count of volatile transaction IDs cache hits",
402                &METRIC_LABELS
403            )
404            .unwrap()
405        });
406
407    /// A total count of the volatile transaction IDs cache misses.
408    pub(super) static VOLATILE_TRANSACTION_IDS_CACHE_MISS: LazyLock<IntCounterVec> =
409        LazyLock::new(|| {
410            register_int_counter_vec!(
411                "rbac_volatile_transaction_ids_cache_miss",
412                "A total count of the volatile transaction IDs cache misses",
413                &METRIC_LABELS
414            )
415            .unwrap()
416        });
417
418    /// Size in memory allocated bytes of the volatile transaction IDs cache.
419    pub(super) static VOLATILE_TRANSACTION_IDS_CACHE_SIZE: LazyLock<IntGaugeVec> =
420        LazyLock::new(|| {
421            register_int_gauge_vec!(
422                "rbac_volatile_transaction_ids_cache_size",
423                "Size in memory allocated bytes of the volatile transaction IDs cache",
424                &METRIC_LABELS
425            )
426            .unwrap()
427        });
428
429    /// Number of entries in the volatile transaction IDs cache.
430    pub(super) static VOLATILE_TRANSACTION_IDS_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
431        LazyLock::new(|| {
432            register_int_gauge_vec!(
433                "rbac_volatile_transaction_ids_cache_entry_count",
434                "Number of entries in the volatile transaction IDs cache",
435                &METRIC_LABELS
436            )
437            .unwrap()
438        });
439
440    /// A total count of the persistent RBAC chains cache hits.
441    pub(super) static PERSISTENT_CHAINS_CACHE_HIT: LazyLock<IntCounterVec> = LazyLock::new(|| {
442        register_int_counter_vec!(
443            "rbac_persistent_chains_cache_hit",
444            "A total count of the persistent RBAC chains cache hits",
445            &METRIC_LABELS
446        )
447        .unwrap()
448    });
449
450    /// A total count of the persistent RBAC chains cache misses.
451    pub(crate) static PERSISTENT_CHAINS_CACHE_MISS: LazyLock<IntCounterVec> = LazyLock::new(|| {
452        register_int_counter_vec!(
453            "rbac_persistent_chains_cache_miss",
454            "A total count of the persistent RBAC chains cache misses",
455            &METRIC_LABELS
456        )
457        .unwrap()
458    });
459
460    /// Size in memory allocated bytes of the persistent RBAC chains cache.
461    pub(super) static PERSISTENT_CHAINS_CACHE_SIZE: LazyLock<IntGaugeVec> = LazyLock::new(|| {
462        register_int_gauge_vec!(
463            "rbac_persistent_chains_cache_size",
464            "Size in memory allocated bytes of the persistent RBAC chains cache",
465            &METRIC_LABELS
466        )
467        .unwrap()
468    });
469
470    /// Number of entries in the persistent RBAC chains cache.
471    pub(super) static PERSISTENT_CHAINS_CACHE_ENTRY_COUNT: LazyLock<IntGaugeVec> =
472        LazyLock::new(|| {
473            register_int_gauge_vec!(
474                "rbac_persistent_transaction_ids_cache_entry_count",
475                "Number of entries in the persistent RBAC chains cache",
476                &METRIC_LABELS
477            )
478            .unwrap()
479        });
480
481    /// This counter increases every time we need to synchronize RBAC indexing by waiting
482    /// for other tasks to finish before processing a new registration.
483    pub(super) static INDEXING_SYNCHRONIZATION_COUNT: LazyLock<IntCounterVec> =
484        LazyLock::new(|| {
485            register_int_counter_vec!(
486                "indexing_synchronization_count",
487                "Number of RBAC indexing synchronizations",
488                &METRIC_LABELS
489            )
490            .unwrap()
491        });
492
493    /// This counter increases every when we found invalid RBAC registration during
494    /// indexing.
495    pub(super) static INVALID_RBAC_REGISTRATION_COUNT: LazyLock<IntCounterVec> =
496        LazyLock::new(|| {
497            register_int_counter_vec!(
498                "invalid_rbac_registration_count",
499                "Number of Invalid RBAC registrations found during indexing",
500                &METRIC_LABELS
501            )
502            .unwrap()
503        });
504}