reth_trie_parallel/
proof_task_metrics.rs

1use reth_metrics::{metrics::Histogram, Metrics};
2use reth_trie::{
3    hashed_cursor::{HashedCursorMetrics, HashedCursorMetricsCache},
4    trie_cursor::{TrieCursorMetrics, TrieCursorMetricsCache},
5    TrieType,
6};
7
8/// Metrics for the proof task.
9#[derive(Clone, Metrics)]
10#[metrics(scope = "trie.proof_task")]
11pub struct ProofTaskTrieMetrics {
12    /// A histogram for the number of blinded account nodes fetched.
13    blinded_account_nodes: Histogram,
14    /// A histogram for the number of blinded storage nodes fetched.
15    blinded_storage_nodes: Histogram,
16}
17
18impl ProofTaskTrieMetrics {
19    /// Record account nodes fetched.
20    pub fn record_account_nodes(&self, count: usize) {
21        self.blinded_account_nodes.record(count as f64);
22    }
23
24    /// Record storage nodes fetched.
25    pub fn record_storage_nodes(&self, count: usize) {
26        self.blinded_storage_nodes.record(count as f64);
27    }
28}
29
30/// Cursor metrics for proof task operations.
31#[derive(Clone, Debug)]
32pub struct ProofTaskCursorMetrics {
33    /// Metrics for account trie cursor operations.
34    pub account_trie_cursor: TrieCursorMetrics,
35    /// Metrics for account hashed cursor operations.
36    pub account_hashed_cursor: HashedCursorMetrics,
37    /// Metrics for storage trie cursor operations.
38    pub storage_trie_cursor: TrieCursorMetrics,
39    /// Metrics for storage hashed cursor operations.
40    pub storage_hashed_cursor: HashedCursorMetrics,
41}
42
43impl ProofTaskCursorMetrics {
44    /// Create a new instance with properly initialized cursor metrics.
45    pub fn new() -> Self {
46        Self {
47            account_trie_cursor: TrieCursorMetrics::new(TrieType::State),
48            account_hashed_cursor: HashedCursorMetrics::new(TrieType::State),
49            storage_trie_cursor: TrieCursorMetrics::new(TrieType::Storage),
50            storage_hashed_cursor: HashedCursorMetrics::new(TrieType::Storage),
51        }
52    }
53
54    /// Record the cached metrics from the provided cache and reset the cache counters.
55    ///
56    /// This method adds the current counter values from the cache to the Prometheus metrics
57    /// and then resets all cache counters to zero.
58    pub fn record(&mut self, cache: &mut ProofTaskCursorMetricsCache) {
59        self.account_trie_cursor.record(&mut cache.account_trie_cursor);
60        self.account_hashed_cursor.record(&mut cache.account_hashed_cursor);
61        self.storage_trie_cursor.record(&mut cache.storage_trie_cursor);
62        self.storage_hashed_cursor.record(&mut cache.storage_hashed_cursor);
63        cache.reset();
64    }
65}
66
67impl Default for ProofTaskCursorMetrics {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73/// Cached cursor metrics for proof task operations.
74#[derive(Clone, Debug, Default, Copy)]
75pub struct ProofTaskCursorMetricsCache {
76    /// Cached metrics for account trie cursor operations.
77    pub account_trie_cursor: TrieCursorMetricsCache,
78    /// Cached metrics for account hashed cursor operations.
79    pub account_hashed_cursor: HashedCursorMetricsCache,
80    /// Cached metrics for storage trie cursor operations.
81    pub storage_trie_cursor: TrieCursorMetricsCache,
82    /// Cached metrics for storage hashed cursor operations.
83    pub storage_hashed_cursor: HashedCursorMetricsCache,
84}
85
86impl ProofTaskCursorMetricsCache {
87    /// Extend this cache by adding the counts from another cache.
88    ///
89    /// This accumulates the counter values from `other` into this cache.
90    pub fn extend(&mut self, other: &Self) {
91        self.account_trie_cursor.extend(&other.account_trie_cursor);
92        self.account_hashed_cursor.extend(&other.account_hashed_cursor);
93        self.storage_trie_cursor.extend(&other.storage_trie_cursor);
94        self.storage_hashed_cursor.extend(&other.storage_hashed_cursor);
95    }
96
97    /// Reset all counters to zero.
98    pub const fn reset(&mut self) {
99        self.account_trie_cursor.reset();
100        self.account_hashed_cursor.reset();
101        self.storage_trie_cursor.reset();
102        self.storage_hashed_cursor.reset();
103    }
104
105    /// Record the spans for metrics.
106    pub fn record_spans(&self) {
107        self.account_trie_cursor.record_span("account_trie_cursor");
108        self.account_hashed_cursor.record_span("account_hashed_cursor");
109        self.storage_trie_cursor.record_span("storage_trie_cursor");
110        self.storage_hashed_cursor.record_span("storage_hashed_cursor");
111    }
112}