reth_provider/providers/database/
metrics.rs

1use metrics::Histogram;
2use reth_metrics::Metrics;
3use std::time::{Duration, Instant};
4
5#[derive(Debug)]
6pub(crate) struct DurationsRecorder {
7    start: Instant,
8    current_metrics: DatabaseProviderMetrics,
9    pub(crate) actions: Vec<(Action, Duration)>,
10    latest: Option<Duration>,
11}
12
13impl Default for DurationsRecorder {
14    fn default() -> Self {
15        Self {
16            start: Instant::now(),
17            actions: Vec::new(),
18            latest: None,
19            current_metrics: DatabaseProviderMetrics::default(),
20        }
21    }
22}
23
24impl DurationsRecorder {
25    /// Records the duration since last record, saves it for future logging and instantly reports as
26    /// a metric with `action` label.
27    pub(crate) fn record_relative(&mut self, action: Action) {
28        let elapsed = self.start.elapsed();
29        let duration = elapsed - self.latest.unwrap_or_default();
30
31        self.actions.push((action, duration));
32        self.current_metrics.record_duration(action, duration);
33        self.latest = Some(elapsed);
34    }
35}
36
37#[derive(Debug, Copy, Clone)]
38pub(crate) enum Action {
39    InsertStorageHashing,
40    InsertAccountHashing,
41    InsertMerkleTree,
42    InsertBlock,
43    InsertState,
44    InsertHashes,
45    InsertHistoryIndices,
46    UpdatePipelineStages,
47    InsertCanonicalHeaders,
48    InsertHeaders,
49    InsertHeaderNumbers,
50    InsertHeaderTerminalDifficulties,
51    InsertBlockBodyIndices,
52    InsertTransactionBlocks,
53    GetNextTxNum,
54    GetParentTD,
55}
56
57/// Database provider metrics
58#[derive(Metrics)]
59#[metrics(scope = "storage.providers.database")]
60struct DatabaseProviderMetrics {
61    /// Duration of insert storage hashing
62    insert_storage_hashing: Histogram,
63    /// Duration of insert account hashing
64    insert_account_hashing: Histogram,
65    /// Duration of insert merkle tree
66    insert_merkle_tree: Histogram,
67    /// Duration of insert block
68    insert_block: Histogram,
69    /// Duration of insert state
70    insert_state: Histogram,
71    /// Duration of insert hashes
72    insert_hashes: Histogram,
73    /// Duration of insert history indices
74    insert_history_indices: Histogram,
75    /// Duration of update pipeline stages
76    update_pipeline_stages: Histogram,
77    /// Duration of insert canonical headers
78    insert_canonical_headers: Histogram,
79    /// Duration of insert headers
80    insert_headers: Histogram,
81    /// Duration of insert header numbers
82    insert_header_numbers: Histogram,
83    /// Duration of insert header TD
84    insert_header_td: Histogram,
85    /// Duration of insert block body indices
86    insert_block_body_indices: Histogram,
87    /// Duration of insert transaction blocks
88    insert_tx_blocks: Histogram,
89    /// Duration of get next tx num
90    get_next_tx_num: Histogram,
91    /// Duration of get parent TD
92    get_parent_td: Histogram,
93}
94
95impl DatabaseProviderMetrics {
96    /// Records the duration for the given action.
97    pub(crate) fn record_duration(&self, action: Action, duration: Duration) {
98        match action {
99            Action::InsertStorageHashing => self.insert_storage_hashing.record(duration),
100            Action::InsertAccountHashing => self.insert_account_hashing.record(duration),
101            Action::InsertMerkleTree => self.insert_merkle_tree.record(duration),
102            Action::InsertBlock => self.insert_block.record(duration),
103            Action::InsertState => self.insert_state.record(duration),
104            Action::InsertHashes => self.insert_hashes.record(duration),
105            Action::InsertHistoryIndices => self.insert_history_indices.record(duration),
106            Action::UpdatePipelineStages => self.update_pipeline_stages.record(duration),
107            Action::InsertCanonicalHeaders => self.insert_canonical_headers.record(duration),
108            Action::InsertHeaders => self.insert_headers.record(duration),
109            Action::InsertHeaderNumbers => self.insert_header_numbers.record(duration),
110            Action::InsertHeaderTerminalDifficulties => self.insert_header_td.record(duration),
111            Action::InsertBlockBodyIndices => self.insert_block_body_indices.record(duration),
112            Action::InsertTransactionBlocks => self.insert_tx_blocks.record(duration),
113            Action::GetNextTxNum => self.get_next_tx_num.record(duration),
114            Action::GetParentTD => self.get_parent_td.record(duration),
115        }
116    }
117}