sui_core/execution_cache/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use tracing::trace;

use prometheus::{
    register_int_counter_vec_with_registry, register_int_counter_with_registry,
    register_int_gauge_with_registry, IntCounter, IntCounterVec, IntGauge, Registry,
};

pub struct ExecutionCacheMetrics {
    pub(crate) pending_notify_read: IntGauge,
    pub(crate) cache_requests: IntCounterVec,
    pub(crate) cache_hits: IntCounterVec,
    pub(crate) cache_negative_hits: IntCounterVec,
    pub(crate) cache_misses: IntCounterVec,
    pub(crate) cache_writes: IntCounterVec,
    pub(crate) expired_tickets: IntCounter,
    pub(crate) backpressure_status: IntGauge,
    pub(crate) backpressure_toggles: IntCounter,
}

impl ExecutionCacheMetrics {
    pub fn new(registry: &Registry) -> Self {
        Self {
            pending_notify_read: register_int_gauge_with_registry!(
                "pending_notify_read",
                "Pending notify read requests",
                registry,
            )
            .unwrap(),
            // `request_type` is "object_by_version", "object_latest", "transaction", etc
            // level in these metrics may be "uncommitted", "committed", "package_cache" or "db"
            cache_requests: register_int_counter_vec_with_registry!(
                "execution_cache_requests",
                "Execution cache requests",
                &["request_type", "level"],
                registry,
            )
            .unwrap(),
            cache_hits: register_int_counter_vec_with_registry!(
                "execution_cache_hits",
                "Execution cache hits",
                &["request_type", "level"],
                registry,
            )
            .unwrap(),
            cache_negative_hits: register_int_counter_vec_with_registry!(
                "execution_cache_negative_hits",
                "Execution cache negative hits",
                &["request_type", "level"],
                registry,
            )
            .unwrap(),
            cache_misses: register_int_counter_vec_with_registry!(
                "execution_cache_misses",
                "Execution cache misses",
                &["request_type", "level"],
                registry,
            )
            .unwrap(),

            // `collection` should be "object", "marker", "transaction_effects", etc
            cache_writes: register_int_counter_vec_with_registry!(
                "execution_cache_writes",
                "Execution cache writes",
                &["collection"],
                registry,
            )
            .unwrap(),

            expired_tickets: register_int_counter_with_registry!(
                "execution_cache_expired_tickets",
                "Failed inserts to monotonic caches because of expired tickets",
                registry,
            )
            .unwrap(),
            backpressure_status: register_int_gauge_with_registry!(
                "execution_cache_backpressure_status",
                "Backpressure status (1 = on, 0 = off)",
                registry,
            )
            .unwrap(),
            backpressure_toggles: register_int_counter_with_registry!(
                "execution_cache_backpressure_toggles",
                "Number of times backpressure was turned on or off",
                registry,
            )
            .unwrap(),
        }
    }

    pub(crate) fn record_cache_request(&self, request_type: &'static str, level: &'static str) {
        trace!(target: "cache_metrics", "Cache request: {} {}", request_type, level);
        self.cache_requests
            .with_label_values(&[request_type, level])
            .inc();
    }

    pub(crate) fn record_cache_multi_request(
        &self,
        request_type: &'static str,
        level: &'static str,
        count: usize,
    ) {
        trace!(
            target: "cache_metrics",
            "Cache multi request: {} {} count: {}",
            request_type,
            level,
            count
        );
        self.cache_requests
            .with_label_values(&[request_type, level])
            .inc_by(count as u64);
    }

    pub(crate) fn record_cache_hit(&self, request_type: &'static str, level: &'static str) {
        trace!(target: "cache_metrics", "Cache hit: {} {}", request_type, level);
        self.cache_hits
            .with_label_values(&[request_type, level])
            .inc();
    }

    pub(crate) fn record_cache_miss(&self, request_type: &'static str, level: &'static str) {
        trace!(target: "cache_metrics", "Cache miss: {} {}", request_type, level);
        self.cache_misses
            .with_label_values(&[request_type, level])
            .inc();
    }

    pub(crate) fn record_cache_negative_hit(
        &self,
        request_type: &'static str,
        level: &'static str,
    ) {
        trace!(target: "cache_metrics", "Cache negative hit: {} {}", request_type, level);
        self.cache_negative_hits
            .with_label_values(&[request_type, level])
            .inc();
    }

    pub(crate) fn record_cache_write(&self, collection: &'static str) {
        self.cache_writes.with_label_values(&[collection]).inc();
    }

    pub(crate) fn record_ticket_expiry(&self) {
        self.expired_tickets.inc();
    }
}