cat_gateway/metrics/caches/
txo_assets.rs

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