Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add EngineApi metrics #10125

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/engine/tree/src/tree/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use reth_metrics::{
metrics::{Counter, Gauge},
Metrics,
};

/// Metrics for the `EngineApi`.
#[derive(Metrics)]
#[metrics(scope = "consensus.engine.beacon")]
pub(crate) struct EngineApiMetrics {
/// How many executed blocks are currently stored.
pub(crate) executed_blocks: Gauge,
/// The number of times the pipeline was run.
pub(crate) pipeline_runs: Counter,
/// The total count of forkchoice updated messages received.
pub(crate) forkchoice_updated_messages: Counter,
/// The total count of new payload messages received.
pub(crate) new_payload_messages: Counter,
// TODO add latency metrics
}
17 changes: 17 additions & 0 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ use tokio::sync::{
use tracing::*;

mod config;
mod metrics;
use crate::tree::metrics::EngineApiMetrics;
pub use config::TreeConfig;

/// Keeps track of the state of the tree.
Expand Down Expand Up @@ -95,6 +97,11 @@ impl TreeState {
}
}

/// Returns the number of executed blocks stored.
fn block_count(&self) -> usize {
self.blocks_by_hash.len()
}

/// Returns the block by hash.
fn block_by_hash(&self, hash: B256) -> Option<Arc<SealedBlock>> {
self.blocks_by_hash.get(&hash).map(|b| b.block.clone())
Expand Down Expand Up @@ -449,6 +456,8 @@ pub struct EngineApiTreeHandlerImpl<P, E, T: EngineTypes> {
payload_builder: PayloadBuilderHandle<T>,
/// Configuration settings.
config: TreeConfig,
/// Metrics for the engine api.
metrics: EngineApiMetrics,
}

impl<P, E, T> EngineApiTreeHandlerImpl<P, E, T>
Expand Down Expand Up @@ -486,6 +495,7 @@ where
canonical_in_memory_state,
payload_builder,
config,
metrics: Default::default(),
}
}

Expand Down Expand Up @@ -719,6 +729,8 @@ where
// state house keeping after backfill sync
// remove all executed blocks below the backfill height
self.state.tree_state.remove_before(Bound::Included(backfill_height));
self.metrics.executed_blocks.set(self.state.tree_state.block_count() as f64);

// remove all buffered blocks below the backfill height
self.state.buffer.remove_old_blocks(backfill_height);
// we remove all entries because now we're synced to the backfill target and consider this
Expand Down Expand Up @@ -827,6 +839,7 @@ where
}

self.backfill_sync_state = BackfillSyncState::Pending;
self.metrics.pipeline_runs.increment(1);
debug!(target: "engine", "emitting backfill action event");
}

Expand Down Expand Up @@ -1470,6 +1483,7 @@ where
}

self.state.tree_state.insert_executed(executed);
self.metrics.executed_blocks.set(self.state.tree_state.block_count() as f64);

// emit insert event
let engine_event = if self.state.tree_state.is_fork(block_hash) {
Expand Down Expand Up @@ -1717,6 +1731,8 @@ where
cancun_fields: Option<CancunPayloadFields>,
) -> Result<TreeOutcome<PayloadStatus>, InsertBlockFatalError> {
trace!(target: "engine", "invoked new payload");
self.metrics.new_payload_messages.increment(1);

// Ensures that the given payload does not violate any consensus rules that concern the
// block's layout, like:
// - missing or invalid base fee
Expand Down Expand Up @@ -1826,6 +1842,7 @@ where
attrs: Option<<Self::Engine as PayloadTypes>::PayloadAttributes>,
) -> ProviderResult<TreeOutcome<OnForkChoiceUpdated>> {
trace!(target: "engine", ?attrs, "invoked forkchoice update");
self.metrics.forkchoice_updated_messages.increment(1);
self.canonical_in_memory_state.on_forkchoice_update_received();

if let Some(on_updated) = self.pre_validate_forkchoice_update(state)? {
Expand Down
Loading