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