Skip to content

Commit ee94f95

Browse files
georgehaofrisitano
andauthored
Feat/add metric manager (#296)
* add metrics to handle * add manager metrics * fix lint * fix lint * update * Update crates/manager/src/manager/handle.rs Co-authored-by: frisitano <[email protected]> * fix lint --------- Co-authored-by: frisitano <[email protected]>
1 parent 5ac99d5 commit ee94f95

File tree

6 files changed

+123
-5
lines changed

6 files changed

+123
-5
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/manager/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ futures.workspace = true
5656
tokio-stream.workspace = true
5757
tokio.workspace = true
5858
tracing.workspace = true
59+
metrics.workspace = true
60+
metrics-derive.workspace = true
5961

6062
[dev-dependencies]
6163
alloy-consensus.workspace = true

crates/manager/src/consensus.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use alloy_primitives::{Address, Signature};
2+
use metrics::Counter;
3+
use metrics_derive::Metrics;
24
use reth_primitives_traits::GotExpected;
35
use reth_scroll_primitives::ScrollBlock;
46
use rollup_node_primitives::{sig_encode_hash, ConsensusUpdate};
@@ -40,17 +42,28 @@ impl Consensus for NoopConsensus {
4042
}
4143
}
4244

45+
/// The metrics for the [`SystemContractConsensus`].
46+
#[derive(Metrics, Clone)]
47+
#[metrics(scope = "consensus")]
48+
pub(crate) struct SystemContractConsensusMetrics {
49+
/// System contract validate new block failed counter.
50+
pub validate_new_block_failed: Counter,
51+
}
52+
4353
/// The system contract consensus.
4454
#[derive(Debug)]
4555
pub struct SystemContractConsensus {
4656
authorized_signer: Address,
57+
58+
/// The metrics for the [`SystemContractConsensus`].
59+
metrics: SystemContractConsensusMetrics,
4760
}
4861

4962
impl SystemContractConsensus {
5063
/// Creates a new [`SystemContractConsensus`] consensus instance with the given authorized
5164
/// signers.
52-
pub const fn new(authorized_signer: Address) -> Self {
53-
Self { authorized_signer }
65+
pub fn new(authorized_signer: Address) -> Self {
66+
Self { authorized_signer, metrics: SystemContractConsensusMetrics::default() }
5467
}
5568
}
5669

@@ -70,6 +83,7 @@ impl Consensus for SystemContractConsensus {
7083
let signer = reth_primitives_traits::crypto::secp256k1::recover_signer(signature, hash)?;
7184

7285
if self.authorized_signer != signer {
86+
self.metrics.validate_new_block_failed.increment(1);
7387
return Err(ConsensusError::IncorrectSigner(GotExpected {
7488
got: signer,
7589
expected: self.authorized_signer,

crates/manager/src/manager/handle.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
use super::{RollupManagerCommand, RollupManagerEvent};
2+
use crate::manager::metrics::HandleMetrics;
23
use reth_network_api::FullNetwork;
34
use reth_scroll_node::ScrollNetworkPrimitives;
45
use reth_tokio_util::EventStream;
56
use rollup_node_primitives::BlockInfo;
67
use scroll_network::ScrollNetworkHandle;
78
use tokio::sync::{mpsc, oneshot};
9+
use tracing::error;
810

911
/// The handle used to send commands to the rollup manager.
1012
#[derive(Debug, Clone)]
1113
pub struct RollupManagerHandle<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> {
1214
/// The channel used to send commands to the rollup manager.
1315
to_manager_tx: mpsc::Sender<RollupManagerCommand<N>>,
16+
handle_metrics: HandleMetrics,
1417
}
1518

1619
impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> RollupManagerHandle<N> {
1720
/// Create a new rollup manager handle.
18-
pub const fn new(to_manager_tx: mpsc::Sender<RollupManagerCommand<N>>) -> Self {
19-
Self { to_manager_tx }
21+
pub fn new(to_manager_tx: mpsc::Sender<RollupManagerCommand<N>>) -> Self {
22+
Self { to_manager_tx, handle_metrics: HandleMetrics::default() }
2023
}
2124

2225
/// Sends a command to the rollup manager.
2326
pub async fn send_command(&self, command: RollupManagerCommand<N>) {
24-
let _ = self.to_manager_tx.send(command).await;
27+
if let Err(err) = self.to_manager_tx.send(command).await {
28+
self.handle_metrics.handle_send_command_failed.increment(1);
29+
error!(target: "rollup::manager::handle", "Failed to send command to rollup manager: {}", err);
30+
}
2531
}
2632

2733
/// Sends a command to the rollup manager to build a block.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use metrics::{Counter, Gauge};
2+
use metrics_derive::Metrics;
3+
4+
/// The metrics for the [`super::RollupManagerHandle`].
5+
#[derive(Metrics, Clone)]
6+
#[metrics(scope = "NodeManager")]
7+
pub(crate) struct HandleMetrics {
8+
/// Failed to send command to rollup manager from handle counter.
9+
pub handle_send_command_failed: Counter,
10+
}
11+
12+
/// The metrics for the [`super::RollupNodeManager`].
13+
#[derive(Metrics, Clone)]
14+
#[metrics(scope = "NodeManager")]
15+
pub(crate) struct RollupNodeManagerMetrics {
16+
/// Manager received and handle rollup manager command counter.
17+
pub handle_rollup_manager_command: Counter,
18+
/// Manager received and handle engine driver event counter.
19+
pub handle_engine_driver_event: Counter,
20+
/// Manager received and handle new block produced counter.
21+
pub handle_new_block_produced: Counter,
22+
/// Manager received and handle l1 notification counter.
23+
pub handle_l1_notification: Counter,
24+
/// Manager received and handle chain orchestrator event counter.
25+
pub handle_chain_orchestrator_event: Counter,
26+
/// Manager received and handle signer event counter.
27+
pub handle_signer_event: Counter,
28+
/// Manager received and handle build new payload counter.
29+
pub handle_build_new_payload: Counter,
30+
/// Manager received and handle l1 consolidation counter.
31+
pub handle_l1_consolidation: Counter,
32+
/// Manager received and handle network manager event counter.
33+
pub handle_network_manager_event: Counter,
34+
/// Manager finalized batch index gauge.
35+
pub handle_finalized_batch_index: Gauge,
36+
/// Manager l1 finalized block number gauge.
37+
pub handle_l1_finalized_block_number: Gauge,
38+
/// Manager L1 reorg L1 block number gauge.
39+
pub handle_l1_reorg_l1_block_number: Gauge,
40+
/// Manager L1 reorg L2 head block number gauge.
41+
pub handle_l1_reorg_l2_head_block_number: Gauge,
42+
/// Manager L1 reorg L2 safe block number gauge.
43+
pub handle_l1_reorg_l2_safe_block_number: Gauge,
44+
/// Manager chain import block number gauge.
45+
pub handle_chain_import_block_number: Gauge,
46+
/// Manager optimistic syncing block number gauge.
47+
pub handle_optimistic_syncing_block_number: Gauge,
48+
}

crates/manager/src/manager/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use super::Consensus;
66
use crate::poll_nested_stream_with_budget;
7+
use ::metrics::counter;
78
use alloy_provider::Provider;
89
use futures::StreamExt;
910
use reth_chainspec::EthChainSpec;
@@ -54,6 +55,9 @@ mod event;
5455
pub use event::RollupManagerEvent;
5556

5657
mod handle;
58+
mod metrics;
59+
60+
use crate::manager::metrics::RollupNodeManagerMetrics;
5761
pub use handle::RollupManagerHandle;
5862

5963
/// The size of the event channel.
@@ -114,6 +118,8 @@ pub struct RollupNodeManager<
114118
block_building_trigger: Option<Interval>,
115119
/// The original block time configuration for restoring automatic sequencing.
116120
block_time_config: Option<u64>,
121+
// metrics for the rollup node manager.
122+
metrics: RollupNodeManagerMetrics,
117123
}
118124

119125
/// The current status of the rollup manager.
@@ -200,6 +206,7 @@ where
200206
None
201207
},
202208
block_time_config: block_time,
209+
metrics: RollupNodeManagerMetrics::default(),
203210
};
204211
(rnm, RollupManagerHandle::new(handle_tx))
205212
}
@@ -283,10 +290,12 @@ where
283290
// Remove once we implement issue #273.
284291
// Update the derivation pipeline on new finalized batch.
285292
for batch_info in finalized_batches {
293+
self.metrics.handle_finalized_batch_index.set(batch_info.index as f64);
286294
self.derivation_pipeline.push_batch(batch_info, block_number);
287295
}
288296
}
289297
ChainOrchestratorEvent::L1BlockFinalized(l1_block_number, finalized_batches, ..) => {
298+
self.metrics.handle_l1_finalized_block_number.set(l1_block_number as f64);
290299
// update the sequencer's l1 finalized block number.
291300
if let Some(sequencer) = self.sequencer.as_mut() {
292301
sequencer.set_l1_finalized_block_number(l1_block_number);
@@ -308,6 +317,14 @@ where
308317
l2_head_block_info,
309318
l2_safe_block_info,
310319
} => {
320+
self.metrics.handle_l1_reorg_l1_block_number.set(l1_block_number as f64);
321+
self.metrics
322+
.handle_l1_reorg_l2_head_block_number
323+
.set(l2_head_block_info.as_ref().map_or(0, |info| info.number) as f64);
324+
self.metrics
325+
.handle_l1_reorg_l2_safe_block_number
326+
.set(l2_safe_block_info.as_ref().map_or(0, |info| info.number) as f64);
327+
311328
// Handle the reorg in the engine driver.
312329
self.engine.handle_l1_reorg(
313330
l1_block_number,
@@ -328,11 +345,17 @@ where
328345
}
329346
}
330347
ChainOrchestratorEvent::ChainExtended(chain_import) => {
348+
self.metrics
349+
.handle_chain_import_block_number
350+
.set(chain_import.chain.last().unwrap().number as f64);
331351
trace!(target: "scroll::node::manager", head = ?chain_import.chain.last().unwrap().header.clone(), peer_id = ?chain_import.peer_id.clone(), "Received chain extension from peer");
332352
// Issue the new chain to the engine driver for processing.
333353
self.engine.handle_chain_import(chain_import)
334354
}
335355
ChainOrchestratorEvent::ChainReorged(chain_import) => {
356+
self.metrics
357+
.handle_chain_import_block_number
358+
.set(chain_import.chain.last().unwrap().number as f64);
336359
trace!(target: "scroll::node::manager", head = ?chain_import.chain.last().unwrap().header, ?chain_import.peer_id, "Received chain reorg from peer");
337360

338361
// Issue the new chain to the engine driver for processing.
@@ -342,6 +365,8 @@ where
342365
let block_info: BlockInfo = (&block).into();
343366
trace!(target: "scroll::node::manager", ?block_info, "Received optimistic sync from peer");
344367

368+
self.metrics.handle_optimistic_syncing_block_number.set(block_info.number as f64);
369+
345370
// Issue the new block info to the engine driver for processing.
346371
self.engine.handle_optimistic_sync(block_info)
347372
}
@@ -360,6 +385,12 @@ where
360385

361386
match err {
362387
ChainOrchestratorError::L1MessageMismatch { expected, actual } => {
388+
counter!(
389+
"manager_handle_chain_orchestrator_event_failed",
390+
"type" => "l1_message_mismatch",
391+
)
392+
.increment(1);
393+
363394
if let Some(event_sender) = self.event_sender.as_ref() {
364395
event_sender.notify(RollupManagerEvent::L1MessageConsolidationError {
365396
expected: *expected,
@@ -368,6 +399,12 @@ where
368399
}
369400
}
370401
ChainOrchestratorError::DatabaseError(DatabaseError::L1MessageNotFound(start)) => {
402+
counter!(
403+
"manager_handle_chain_orchestrator_event_failed",
404+
"type" => "l1_message_not_found",
405+
)
406+
.increment(1);
407+
371408
if let Some(event_sender) = self.event_sender.as_ref() {
372409
event_sender.notify(RollupManagerEvent::L1MessageMissingInDatabase {
373410
start: start.clone(),
@@ -428,6 +465,7 @@ where
428465

429466
/// Handles an [`L1Notification`] from the L1 watcher.
430467
fn handle_l1_notification(&mut self, notification: L1Notification) {
468+
self.metrics.handle_l1_notification.increment(1);
431469
if let Some(event_sender) = self.event_sender.as_ref() {
432470
event_sender.notify(RollupManagerEvent::L1NotificationEvent(notification.clone()));
433471
}
@@ -499,6 +537,7 @@ where
499537

500538
// Poll the handle receiver for commands.
501539
while let Poll::Ready(Some(command)) = this.handle_rx.poll_recv(cx) {
540+
this.metrics.handle_rollup_manager_command.increment(1);
502541
match command {
503542
RollupManagerCommand::BuildBlock => {
504543
proceed_if!(
@@ -550,6 +589,7 @@ where
550589

551590
// Drain all EngineDriver events.
552591
while let Poll::Ready(Some(event)) = this.engine.poll_next_unpin(cx) {
592+
this.metrics.handle_engine_driver_event.increment(1);
553593
this.handle_engine_driver_event(event);
554594
}
555595

@@ -559,6 +599,7 @@ where
559599
if let Some(Poll::Ready(Some(attributes))) =
560600
this.sequencer.as_mut().map(|x| x.poll_next_unpin(cx))
561601
{
602+
this.metrics.handle_new_block_produced.increment(1);
562603
this.engine.handle_build_new_payload(attributes);
563604
}
564605
);
@@ -580,6 +621,7 @@ where
580621

581622
// Drain all chain orchestrator events.
582623
while let Poll::Ready(Some(result)) = this.chain.poll_next_unpin(cx) {
624+
this.metrics.handle_chain_orchestrator_event.increment(1);
583625
match result {
584626
Ok(event) => this.handle_chain_orchestrator_event(event),
585627
Err(err) => {
@@ -592,6 +634,7 @@ where
592634
while let Some(Poll::Ready(Some(event))) =
593635
this.signer.as_mut().map(|s| s.poll_next_unpin(cx))
594636
{
637+
this.metrics.handle_signer_event.increment(1);
595638
match event {
596639
SignerEvent::SignedBlock { block, signature } => {
597640
trace!(target: "scroll::node::manager", ?block, ?signature, "Received signed block from signer, announcing to the network");
@@ -619,6 +662,7 @@ where
619662
this.block_building_trigger.as_mut().map(|trigger| trigger.poll_tick(cx)),
620663
this.sequencer.as_mut()
621664
) {
665+
this.metrics.handle_build_new_payload.increment(1);
622666
if !this.consensus.should_sequence_block(
623667
this.signer
624668
.as_ref()
@@ -636,11 +680,13 @@ where
636680

637681
// Poll Derivation Pipeline and push attribute in queue if any.
638682
while let Poll::Ready(Some(attributes)) = this.derivation_pipeline.poll_next_unpin(cx) {
683+
this.metrics.handle_l1_consolidation.increment(1);
639684
this.engine.handle_l1_consolidation(attributes)
640685
}
641686

642687
// Handle network manager events.
643688
while let Poll::Ready(Some(event)) = this.network.poll_next_unpin(cx) {
689+
this.metrics.handle_network_manager_event.increment(1);
644690
this.handle_network_manager_event(event);
645691
}
646692

0 commit comments

Comments
 (0)