reth_blockchain_tree/
metrics.rs

1use metrics::Histogram;
2use reth_metrics::{
3    metrics::{Counter, Gauge},
4    Metrics,
5};
6use std::time::{Duration, Instant};
7
8/// Metrics for the blockchain tree block buffer
9#[derive(Metrics)]
10#[metrics(scope = "blockchain_tree.block_buffer")]
11pub struct BlockBufferMetrics {
12    /// Total blocks in the block buffer
13    pub blocks: Gauge,
14}
15
16#[derive(Debug)]
17pub(crate) struct MakeCanonicalDurationsRecorder {
18    start: Instant,
19    pub(crate) actions: Vec<(MakeCanonicalAction, Duration)>,
20    latest: Option<Duration>,
21    current_metrics: MakeCanonicalMetrics,
22}
23
24impl Default for MakeCanonicalDurationsRecorder {
25    fn default() -> Self {
26        Self {
27            start: Instant::now(),
28            actions: Vec::new(),
29            latest: None,
30            current_metrics: MakeCanonicalMetrics::default(),
31        }
32    }
33}
34
35impl MakeCanonicalDurationsRecorder {
36    /// Records the duration since last record, saves it for future logging and instantly reports as
37    /// a metric with `action` label.
38    pub(crate) fn record_relative(&mut self, action: MakeCanonicalAction) {
39        let elapsed = self.start.elapsed();
40        let duration = elapsed - self.latest.unwrap_or_default();
41
42        self.actions.push((action, duration));
43        self.current_metrics.record(action, duration);
44        self.latest = Some(elapsed);
45    }
46}
47
48/// Metrics for the entire blockchain tree
49#[derive(Metrics)]
50#[metrics(scope = "blockchain_tree")]
51pub struct TreeMetrics {
52    /// Total number of sidechains (not including the canonical chain)
53    pub sidechains: Gauge,
54    /// The highest block number in the canonical chain
55    pub canonical_chain_height: Gauge,
56    /// The number of reorgs
57    pub reorgs: Counter,
58    /// The latest reorg depth
59    pub latest_reorg_depth: Gauge,
60    /// Longest sidechain height
61    pub longest_sidechain_height: Gauge,
62    /// The number of times cached trie updates were used for insert.
63    pub trie_updates_insert_cached: Counter,
64    /// The number of times trie updates were recomputed for insert.
65    pub trie_updates_insert_recomputed: Counter,
66}
67
68/// Represents actions for making a canonical chain.
69#[derive(Debug, Copy, Clone)]
70pub(crate) enum MakeCanonicalAction {
71    /// Cloning old blocks for canonicalization.
72    CloneOldBlocks,
73    /// Finding the canonical header.
74    FindCanonicalHeader,
75    /// Splitting the chain for canonicalization.
76    SplitChain,
77    /// Splitting chain forks for canonicalization.
78    SplitChainForks,
79    /// Merging all chains for canonicalization.
80    MergeAllChains,
81    /// Updating the canonical index during canonicalization.
82    UpdateCanonicalIndex,
83    /// Retrieving (cached or recomputed) state trie updates
84    RetrieveStateTrieUpdates,
85    /// Committing the canonical chain to the database.
86    CommitCanonicalChainToDatabase,
87    /// Reverting the canonical chain from the database.
88    RevertCanonicalChainFromDatabase,
89    /// Inserting an old canonical chain.
90    InsertOldCanonicalChain,
91    /// Clearing trie updates of other children chains after fork choice update.
92    ClearTrieUpdatesForOtherChildren,
93}
94
95/// Canonicalization metrics
96#[derive(Metrics)]
97#[metrics(scope = "blockchain_tree.make_canonical")]
98struct MakeCanonicalMetrics {
99    /// Duration of the clone old blocks action.
100    clone_old_blocks: Histogram,
101    /// Duration of the find canonical header action.
102    find_canonical_header: Histogram,
103    /// Duration of the split chain action.
104    split_chain: Histogram,
105    /// Duration of the split chain forks action.
106    split_chain_forks: Histogram,
107    /// Duration of the merge all chains action.
108    merge_all_chains: Histogram,
109    /// Duration of the update canonical index action.
110    update_canonical_index: Histogram,
111    /// Duration of the retrieve state trie updates action.
112    retrieve_state_trie_updates: Histogram,
113    /// Duration of the commit canonical chain to database action.
114    commit_canonical_chain_to_database: Histogram,
115    /// Duration of the revert canonical chain from database action.
116    revert_canonical_chain_from_database: Histogram,
117    /// Duration of the insert old canonical chain action.
118    insert_old_canonical_chain: Histogram,
119    /// Duration of the clear trie updates of other children chains after fork choice update
120    /// action.
121    clear_trie_updates_for_other_children: Histogram,
122}
123
124impl MakeCanonicalMetrics {
125    /// Records the duration for the given action.
126    pub(crate) fn record(&self, action: MakeCanonicalAction, duration: Duration) {
127        match action {
128            MakeCanonicalAction::CloneOldBlocks => self.clone_old_blocks.record(duration),
129            MakeCanonicalAction::FindCanonicalHeader => self.find_canonical_header.record(duration),
130            MakeCanonicalAction::SplitChain => self.split_chain.record(duration),
131            MakeCanonicalAction::SplitChainForks => self.split_chain_forks.record(duration),
132            MakeCanonicalAction::MergeAllChains => self.merge_all_chains.record(duration),
133            MakeCanonicalAction::UpdateCanonicalIndex => {
134                self.update_canonical_index.record(duration)
135            }
136            MakeCanonicalAction::RetrieveStateTrieUpdates => {
137                self.retrieve_state_trie_updates.record(duration)
138            }
139            MakeCanonicalAction::CommitCanonicalChainToDatabase => {
140                self.commit_canonical_chain_to_database.record(duration)
141            }
142            MakeCanonicalAction::RevertCanonicalChainFromDatabase => {
143                self.revert_canonical_chain_from_database.record(duration)
144            }
145            MakeCanonicalAction::InsertOldCanonicalChain => {
146                self.insert_old_canonical_chain.record(duration)
147            }
148            MakeCanonicalAction::ClearTrieUpdatesForOtherChildren => {
149                self.clear_trie_updates_for_other_children.record(duration)
150            }
151        }
152    }
153}