use crate::stats::TrieStats;
use metrics::{Counter, Histogram};
use reth_metrics::Metrics;
#[derive(Debug)]
pub struct StateRootMetrics {
pub state_trie: TrieRootMetrics,
pub storage_trie: TrieRootMetrics,
}
impl Default for StateRootMetrics {
fn default() -> Self {
Self {
state_trie: TrieRootMetrics::new(TrieType::State),
storage_trie: TrieRootMetrics::new(TrieType::Storage),
}
}
}
#[derive(Clone, Metrics)]
#[metrics(scope = "trie")]
pub struct TrieRootMetrics {
duration_seconds: Histogram,
branches_added: Histogram,
leaves_added: Histogram,
}
impl TrieRootMetrics {
pub fn new(ty: TrieType) -> Self {
Self::new_with_labels(&[("type", ty.as_str())])
}
pub fn record(&self, stats: TrieStats) {
self.duration_seconds.record(stats.duration().as_secs_f64());
self.branches_added.record(stats.branches_added() as f64);
self.leaves_added.record(stats.leaves_added() as f64);
}
}
#[derive(Clone, Copy, Debug)]
pub enum TrieType {
State,
Storage,
}
impl TrieType {
pub(crate) const fn as_str(&self) -> &'static str {
match self {
Self::State => "state",
Self::Storage => "storage",
}
}
}
#[derive(Clone, Metrics)]
#[metrics(scope = "trie.walker")]
pub struct WalkerMetrics {
out_of_order_subnode: Counter,
}
impl WalkerMetrics {
pub fn new(ty: TrieType) -> Self {
Self::new_with_labels(&[("type", ty.as_str())])
}
pub fn inc_out_of_order_subnode(&self, amount: u64) {
self.out_of_order_subnode.increment(amount);
}
}