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 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#[derive(Metrics)]
59#[metrics(scope = "storage.providers.database")]
60struct DatabaseProviderMetrics {
61 insert_storage_hashing: Histogram,
63 insert_account_hashing: Histogram,
65 insert_merkle_tree: Histogram,
67 insert_block: Histogram,
69 insert_state: Histogram,
71 insert_hashes: Histogram,
73 insert_history_indices: Histogram,
75 update_pipeline_stages: Histogram,
77 insert_canonical_headers: Histogram,
79 insert_headers: Histogram,
81 insert_header_numbers: Histogram,
83 insert_header_td: Histogram,
85 insert_block_body_indices: Histogram,
87 insert_tx_blocks: Histogram,
89 get_next_tx_num: Histogram,
91 get_parent_td: Histogram,
93}
94
95impl DatabaseProviderMetrics {
96 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}