sui_core/execution_cache/
metrics.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use tracing::trace;
5
6use prometheus::{
7    register_int_counter_vec_with_registry, register_int_counter_with_registry,
8    register_int_gauge_with_registry, IntCounter, IntCounterVec, IntGauge, Registry,
9};
10
11pub struct ExecutionCacheMetrics {
12    pub(crate) pending_notify_read: IntGauge,
13    pub(crate) cache_requests: IntCounterVec,
14    pub(crate) cache_hits: IntCounterVec,
15    pub(crate) cache_negative_hits: IntCounterVec,
16    pub(crate) cache_misses: IntCounterVec,
17    pub(crate) cache_writes: IntCounterVec,
18    pub(crate) expired_tickets: IntCounter,
19    pub(crate) backpressure_status: IntGauge,
20    pub(crate) backpressure_toggles: IntCounter,
21}
22
23impl ExecutionCacheMetrics {
24    pub fn new(registry: &Registry) -> Self {
25        Self {
26            pending_notify_read: register_int_gauge_with_registry!(
27                "pending_notify_read",
28                "Pending notify read requests",
29                registry,
30            )
31            .unwrap(),
32            // `request_type` is "object_by_version", "object_latest", "transaction", etc
33            // level in these metrics may be "uncommitted", "committed", "package_cache" or "db"
34            cache_requests: register_int_counter_vec_with_registry!(
35                "execution_cache_requests",
36                "Execution cache requests",
37                &["request_type", "level"],
38                registry,
39            )
40            .unwrap(),
41            cache_hits: register_int_counter_vec_with_registry!(
42                "execution_cache_hits",
43                "Execution cache hits",
44                &["request_type", "level"],
45                registry,
46            )
47            .unwrap(),
48            cache_negative_hits: register_int_counter_vec_with_registry!(
49                "execution_cache_negative_hits",
50                "Execution cache negative hits",
51                &["request_type", "level"],
52                registry,
53            )
54            .unwrap(),
55            cache_misses: register_int_counter_vec_with_registry!(
56                "execution_cache_misses",
57                "Execution cache misses",
58                &["request_type", "level"],
59                registry,
60            )
61            .unwrap(),
62
63            // `collection` should be "object", "marker", "transaction_effects", etc
64            cache_writes: register_int_counter_vec_with_registry!(
65                "execution_cache_writes",
66                "Execution cache writes",
67                &["collection"],
68                registry,
69            )
70            .unwrap(),
71
72            expired_tickets: register_int_counter_with_registry!(
73                "execution_cache_expired_tickets",
74                "Failed inserts to monotonic caches because of expired tickets",
75                registry,
76            )
77            .unwrap(),
78            backpressure_status: register_int_gauge_with_registry!(
79                "execution_cache_backpressure_status",
80                "Backpressure status (1 = on, 0 = off)",
81                registry,
82            )
83            .unwrap(),
84            backpressure_toggles: register_int_counter_with_registry!(
85                "execution_cache_backpressure_toggles",
86                "Number of times backpressure was turned on or off",
87                registry,
88            )
89            .unwrap(),
90        }
91    }
92
93    pub(crate) fn record_cache_request(&self, request_type: &'static str, level: &'static str) {
94        trace!(target: "cache_metrics", "Cache request: {} {}", request_type, level);
95        self.cache_requests
96            .with_label_values(&[request_type, level])
97            .inc();
98    }
99
100    pub(crate) fn record_cache_multi_request(
101        &self,
102        request_type: &'static str,
103        level: &'static str,
104        count: usize,
105    ) {
106        trace!(
107            target: "cache_metrics",
108            "Cache multi request: {} {} count: {}",
109            request_type,
110            level,
111            count
112        );
113        self.cache_requests
114            .with_label_values(&[request_type, level])
115            .inc_by(count as u64);
116    }
117
118    pub(crate) fn record_cache_hit(&self, request_type: &'static str, level: &'static str) {
119        trace!(target: "cache_metrics", "Cache hit: {} {}", request_type, level);
120        self.cache_hits
121            .with_label_values(&[request_type, level])
122            .inc();
123    }
124
125    pub(crate) fn record_cache_miss(&self, request_type: &'static str, level: &'static str) {
126        trace!(target: "cache_metrics", "Cache miss: {} {}", request_type, level);
127        self.cache_misses
128            .with_label_values(&[request_type, level])
129            .inc();
130    }
131
132    pub(crate) fn record_cache_negative_hit(
133        &self,
134        request_type: &'static str,
135        level: &'static str,
136    ) {
137        trace!(target: "cache_metrics", "Cache negative hit: {} {}", request_type, level);
138        self.cache_negative_hits
139            .with_label_values(&[request_type, level])
140            .inc();
141    }
142
143    pub(crate) fn record_cache_write(&self, collection: &'static str) {
144        self.cache_writes.with_label_values(&[collection]).inc();
145    }
146
147    pub(crate) fn record_ticket_expiry(&self) {
148        self.expired_tickets.inc();
149    }
150}