cat_gateway/metrics/caches/
native_assets.rs

1//! Cache metrics for Native Assets.
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
9/// Update Cache metrics
10pub(crate) fn update() {
11    let api_host_names = Settings::api_host_names().join(",");
12    let service_id = Settings::service_id();
13    let network = Settings::cardano_network().to_string();
14
15    CassandraSession::get(PERSISTENT).inspect(|session| {
16        let cache = session.caches().assets_native();
17        reporter::NATIVE_ASSETS_CACHE_SIZE
18            .with_label_values(&[&api_host_names, service_id, &network])
19            .set(i64::try_from(cache.weighted_size()).unwrap_or(-1));
20
21        reporter::NATIVE_ASSETS_CACHE_ENTRIES_COUNT
22            .with_label_values(&[&api_host_names, service_id, &network])
23            .set(i64::try_from(cache.entry_count()).unwrap_or(-1));
24    });
25}
26
27/// Increment the Native Assets Cache hits count.
28pub(crate) fn native_assets_hits_inc() {
29    cache_metric_inc!(NATIVE_ASSETS_CACHE_HIT_COUNT);
30}
31
32/// Increment the Native Assets Cache misses count.
33pub(crate) fn native_assets_misses_inc() {
34    cache_metric_inc!(NATIVE_ASSETS_CACHE_MISSES_COUNT);
35}
36
37mod reporter {
38    //! Prometheus reporter metrics.
39    #![allow(clippy::unwrap_used)]
40
41    use std::sync::LazyLock;
42
43    use prometheus::{
44        register_int_counter_vec, register_int_gauge_vec, IntCounterVec, IntGaugeVec,
45    };
46
47    /// Labels for the metrics
48    const METRIC_LABELS: [&str; 3] = ["api_host_names", "service_id", "network"];
49
50    /// Size of the Native Assets cache.
51    pub(super) static NATIVE_ASSETS_CACHE_SIZE: LazyLock<IntGaugeVec> = LazyLock::new(|| {
52        register_int_gauge_vec!(
53            "cache_native_assets_size",
54            "Returns the total weighted size of Native Assets entries in this cache",
55            &METRIC_LABELS
56        )
57        .unwrap()
58    });
59
60    /// Number of entries in the Native Assets cache.
61    pub(super) static NATIVE_ASSETS_CACHE_ENTRIES_COUNT: LazyLock<IntGaugeVec> =
62        LazyLock::new(|| {
63            register_int_gauge_vec!(
64                "cache_native_assets_entries_count",
65                "Returns the number of Native Assets entries in this cache",
66                &METRIC_LABELS
67            )
68            .unwrap()
69        });
70
71    /// Number of hits in the Native Assets cache.
72    pub(super) static NATIVE_ASSETS_CACHE_HIT_COUNT: LazyLock<IntCounterVec> =
73        LazyLock::new(|| {
74            register_int_counter_vec!(
75                "cache_native_assets_hits_count",
76                "Returns the number of hits (entries found) in the Native Assets cache",
77                &METRIC_LABELS
78            )
79            .unwrap()
80        });
81
82    /// Number of misses in the Native Assets cache.
83    pub(super) static NATIVE_ASSETS_CACHE_MISSES_COUNT: LazyLock<IntCounterVec> =
84        LazyLock::new(|| {
85            register_int_counter_vec!(
86                "cache_native_assets_misses_count",
87                "Returns the number of misses (entries not found) in the Native Assets cache",
88                &METRIC_LABELS
89            )
90            .unwrap()
91        });
92}