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    use std::sync::LazyLock;
40
41    use prometheus::{
42        register_int_counter_vec, register_int_gauge_vec, IntCounterVec, IntGaugeVec,
43    };
44
45    /// Labels for the metrics
46    const METRIC_LABELS: [&str; 3] = ["api_host_names", "service_id", "network"];
47
48    /// Size of the Native Assets cache.
49    pub(super) static NATIVE_ASSETS_CACHE_SIZE: LazyLock<IntGaugeVec> = LazyLock::new(|| {
50        register_int_gauge_vec!(
51            "cache_native_assets_size",
52            "Returns the total weighted size of Native Assets entries in this cache",
53            &METRIC_LABELS
54        )
55        .unwrap()
56    });
57
58    /// Number of entries in the Native Assets cache.
59    pub(super) static NATIVE_ASSETS_CACHE_ENTRIES_COUNT: LazyLock<IntGaugeVec> =
60        LazyLock::new(|| {
61            register_int_gauge_vec!(
62                "cache_native_assets_entries_count",
63                "Returns the number of Native Assets entries in this cache",
64                &METRIC_LABELS
65            )
66            .unwrap()
67        });
68
69    /// Number of hits in the Native Assets cache.
70    pub(super) static NATIVE_ASSETS_CACHE_HIT_COUNT: LazyLock<IntCounterVec> =
71        LazyLock::new(|| {
72            register_int_counter_vec!(
73                "cache_native_assets_hits_count",
74                "Returns the number of hits (entries found) in the Native Assets cache",
75                &METRIC_LABELS
76            )
77            .unwrap()
78        });
79
80    /// Number of misses in the Native Assets cache.
81    pub(super) static NATIVE_ASSETS_CACHE_MISSES_COUNT: LazyLock<IntCounterVec> =
82        LazyLock::new(|| {
83            register_int_counter_vec!(
84                "cache_native_assets_misses_count",
85                "Returns the number of misses (entries not found) in the Native Assets cache",
86                &METRIC_LABELS
87            )
88            .unwrap()
89        });
90}