From 993b84408bc2b271fd428c5a6d2677a969564b3d Mon Sep 17 00:00:00 2001 From: Chris Evanko <106608356+cjeva10@users.noreply.github.com> Date: Mon, 24 Jul 2023 17:42:59 -0400 Subject: [PATCH] feat: add canonicalization latency metric (#3865) --- crates/consensus/beacon/src/engine/metrics.rs | 10 ++++++- crates/consensus/beacon/src/engine/mod.rs | 30 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/consensus/beacon/src/engine/metrics.rs b/crates/consensus/beacon/src/engine/metrics.rs index 04080e93be..66a95dc578 100644 --- a/crates/consensus/beacon/src/engine/metrics.rs +++ b/crates/consensus/beacon/src/engine/metrics.rs @@ -1,5 +1,5 @@ use reth_metrics::{ - metrics::{self, Counter, Gauge}, + metrics::{self, Counter, Gauge, Histogram}, Metrics, }; @@ -15,6 +15,14 @@ pub(crate) struct EngineMetrics { pub(crate) new_payload_messages: Counter, /// The number of times the pruner was run. pub(crate) pruner_runs: Counter, + /// Latency for making canonical already canonical block + pub(crate) make_canonical_already_canonical_latency: Histogram, + /// Latency for making canonical committed block + pub(crate) make_canonical_committed_latency: Histogram, + /// Latency for making canonical returns error + pub(crate) make_canonical_error_latency: Histogram, + /// Latency for all making canonical results + pub(crate) make_canonical_latency: Histogram, } /// Metrics for the `EngineSyncController`. diff --git a/crates/consensus/beacon/src/engine/mod.rs b/crates/consensus/beacon/src/engine/mod.rs index 35ecd356a2..7c74bc3957 100644 --- a/crates/consensus/beacon/src/engine/mod.rs +++ b/crates/consensus/beacon/src/engine/mod.rs @@ -12,7 +12,7 @@ use reth_db::database::Database; use reth_interfaces::{ blockchain_tree::{ error::{InsertBlockError, InsertBlockErrorKind}, - BlockStatus, BlockchainTreeEngine, InsertPayloadOk, + BlockStatus, BlockchainTreeEngine, CanonicalOutcome, InsertPayloadOk, }, consensus::ForkchoiceState, executor::{BlockExecutionError, BlockValidationError}, @@ -39,6 +39,7 @@ use std::{ pin::Pin, sync::Arc, task::{Context, Poll}, + time::Instant, }; use tokio::sync::{ mpsc, @@ -628,7 +629,10 @@ where return Ok(OnForkChoiceUpdated::syncing()) } - let status = match self.blockchain.make_canonical(&state.head_block_hash) { + let start = Instant::now(); + let make_canonical_result = self.blockchain.make_canonical(&state.head_block_hash); + self.record_make_canonical_latency(start, &make_canonical_result); + let status = match make_canonical_result { Ok(outcome) => { if !outcome.is_already_canonical() { debug!(target: "consensus::engine", hash=?state.head_block_hash, number=outcome.header().number, "canonicalized new head"); @@ -684,6 +688,28 @@ where Ok(OnForkChoiceUpdated::valid(status)) } + /// Record latency metrics for one call to make a block canonical + /// Takes start time of the call and result of the make canonical call + /// + /// Handles cases for error, already canonical and commmitted blocks + fn record_make_canonical_latency( + &self, + start: Instant, + outcome: &Result, + ) { + let elapsed = start.elapsed(); + self.metrics.make_canonical_latency.record(elapsed); + match outcome { + Ok(CanonicalOutcome::AlreadyCanonical { .. }) => { + self.metrics.make_canonical_already_canonical_latency.record(elapsed) + } + Ok(CanonicalOutcome::Committed { .. }) => { + self.metrics.make_canonical_committed_latency.record(elapsed) + } + Err(_) => self.metrics.make_canonical_error_latency.record(elapsed), + } + } + /// Ensures that the given forkchoice state is consistent, assuming the head block has been /// made canonical. This takes a status as input, and will only perform consistency checks if /// the input status is VALID.