reth_provider/providers/database/
metrics.rsuse metrics::Histogram;
use reth_metrics::Metrics;
use std::time::{Duration, Instant};
#[derive(Debug)]
pub(crate) struct DurationsRecorder {
start: Instant,
current_metrics: DatabaseProviderMetrics,
pub(crate) actions: Vec<(Action, Duration)>,
latest: Option<Duration>,
}
impl Default for DurationsRecorder {
fn default() -> Self {
Self {
start: Instant::now(),
actions: Vec::new(),
latest: None,
current_metrics: DatabaseProviderMetrics::default(),
}
}
}
impl DurationsRecorder {
pub(crate) fn record_duration(&mut self, action: Action, duration: Duration) {
self.actions.push((action, duration));
self.current_metrics.record_duration(action, duration);
self.latest = Some(self.start.elapsed());
}
pub(crate) fn record_relative(&mut self, action: Action) {
let elapsed = self.start.elapsed();
let duration = elapsed - self.latest.unwrap_or_default();
self.actions.push((action, duration));
self.current_metrics.record_duration(action, duration);
self.latest = Some(elapsed);
}
}
#[derive(Debug, Copy, Clone)]
pub(crate) enum Action {
InsertStorageHashing,
InsertAccountHashing,
InsertMerkleTree,
InsertBlock,
InsertState,
InsertHashes,
InsertHistoryIndices,
UpdatePipelineStages,
InsertCanonicalHeaders,
InsertHeaders,
InsertHeaderNumbers,
InsertHeaderTerminalDifficulties,
InsertBlockOmmers,
InsertTransactionSenders,
InsertTransactions,
InsertTransactionHashNumbers,
InsertBlockWithdrawals,
InsertBlockBodyIndices,
InsertTransactionBlocks,
GetNextTxNum,
GetParentTD,
}
#[derive(Metrics)]
#[metrics(scope = "storage.providers.database")]
struct DatabaseProviderMetrics {
insert_storage_hashing: Histogram,
insert_account_hashing: Histogram,
insert_merkle_tree: Histogram,
insert_block: Histogram,
insert_state: Histogram,
insert_hashes: Histogram,
insert_history_indices: Histogram,
update_pipeline_stages: Histogram,
insert_canonical_headers: Histogram,
insert_headers: Histogram,
insert_header_numbers: Histogram,
insert_header_td: Histogram,
insert_block_ommers: Histogram,
insert_tx_senders: Histogram,
insert_transactions: Histogram,
insert_tx_hash_numbers: Histogram,
insert_block_withdrawals: Histogram,
insert_block_body_indices: Histogram,
insert_tx_blocks: Histogram,
get_next_tx_num: Histogram,
get_parent_td: Histogram,
}
impl DatabaseProviderMetrics {
pub(crate) fn record_duration(&self, action: Action, duration: Duration) {
match action {
Action::InsertStorageHashing => self.insert_storage_hashing.record(duration),
Action::InsertAccountHashing => self.insert_account_hashing.record(duration),
Action::InsertMerkleTree => self.insert_merkle_tree.record(duration),
Action::InsertBlock => self.insert_block.record(duration),
Action::InsertState => self.insert_state.record(duration),
Action::InsertHashes => self.insert_hashes.record(duration),
Action::InsertHistoryIndices => self.insert_history_indices.record(duration),
Action::UpdatePipelineStages => self.update_pipeline_stages.record(duration),
Action::InsertCanonicalHeaders => self.insert_canonical_headers.record(duration),
Action::InsertHeaders => self.insert_headers.record(duration),
Action::InsertHeaderNumbers => self.insert_header_numbers.record(duration),
Action::InsertHeaderTerminalDifficulties => self.insert_header_td.record(duration),
Action::InsertBlockOmmers => self.insert_block_ommers.record(duration),
Action::InsertTransactionSenders => self.insert_tx_senders.record(duration),
Action::InsertTransactions => self.insert_transactions.record(duration),
Action::InsertTransactionHashNumbers => self.insert_tx_hash_numbers.record(duration),
Action::InsertBlockWithdrawals => self.insert_block_withdrawals.record(duration),
Action::InsertBlockBodyIndices => self.insert_block_body_indices.record(duration),
Action::InsertTransactionBlocks => self.insert_tx_blocks.record(duration),
Action::GetNextTxNum => self.get_next_tx_num.record(duration),
Action::GetParentTD => self.get_parent_td.record(duration),
}
}
}