Skip to content

Commit 7c764ff

Browse files
authored
feat: metrics (#153)
* feat: add watcher metrics * feat: add indexer and derivation pipeline metrics * feat: signer metrics * feat: sequencer and engine metrics * fix: lint
1 parent 0d62718 commit 7c764ff

File tree

27 files changed

+1404
-105
lines changed

27 files changed

+1404
-105
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ auto_impl = "1.2"
189189
derive_more = { version = "2.0", default-features = false }
190190
eyre = "0.6"
191191
futures = { version = "0.3", default-features = false }
192+
metrics = "0.24.0"
193+
metrics-derive = "0.1"
192194
parking_lot = "0.12"
193195
rand = { version = "0.9" }
194196
reqwest = "0.12"

crates/derivation-pipeline/Cargo.toml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ scroll-db.workspace = true
2727

2828
# misc
2929
futures.workspace = true
30+
metrics.workspace = true
31+
metrics-derive.workspace = true
3032
thiserror.workspace = true
3133
tracing.workspace = true
3234

@@ -36,16 +38,3 @@ eyre.workspace = true
3638
scroll-db = { workspace = true, features = ["test-utils"] }
3739
scroll-codec = { workspace = true, features = ["test-utils"] }
3840
tokio = { workspace = true, features = ["macros"] }
39-
40-
[features]
41-
default = ["std"]
42-
std = [
43-
"scroll-alloy-consensus/std",
44-
"scroll-alloy-rpc-types-engine/std",
45-
"alloy-primitives/std",
46-
"alloy-rpc-types-engine/std",
47-
"alloy-eips/std",
48-
"scroll-alloy-consensus/std",
49-
"scroll-alloy-rpc-types-engine/std",
50-
"futures/std",
51-
]

crates/derivation-pipeline/src/lib.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
//! This crate provides a simple implementation of a derivation pipeline that transforms a batch
44
//! into payload attributes for block building.
55
6-
#![cfg_attr(not(feature = "std"), no_std)]
7-
86
mod data_source;
97

10-
pub use error::DerivationPipelineError;
118
mod error;
9+
pub use error::DerivationPipelineError;
1210

13-
#[cfg(not(feature = "std"))]
14-
extern crate alloc as std;
11+
mod metrics;
12+
pub use metrics::DerivationPipelineMetrics;
1513

1614
use crate::data_source::CodecDataSource;
17-
use std::{boxed::Box, collections::VecDeque, sync::Arc, vec::Vec};
15+
use std::{boxed::Box, collections::VecDeque, sync::Arc, time::Instant, vec::Vec};
1816

1917
use alloy_primitives::{Address, B256};
2018
use alloy_rpc_types_engine::PayloadAttributes;
@@ -61,6 +59,8 @@ pub struct DerivationPipeline<P> {
6159
attributes_queue: VecDeque<ScrollPayloadAttributesWithBatchInfo>,
6260
/// The waker for the pipeline.
6361
waker: Option<Waker>,
62+
/// The metrics of the pipeline.
63+
metrics: DerivationPipelineMetrics,
6464
}
6565

6666
impl<P> DerivationPipeline<P>
@@ -76,6 +76,7 @@ where
7676
pipeline_futures: Default::default(),
7777
attributes_queue: Default::default(),
7878
waker: None,
79+
metrics: DerivationPipelineMetrics::default(),
7980
}
8081
}
8182

@@ -93,10 +94,13 @@ where
9394
/// futures.
9495
fn handle_next_batch(&mut self) -> Option<DerivationPipelineFuture> {
9596
let database = self.database.clone();
97+
let metrics = self.metrics.clone();
9698
let provider = self.l1_provider.clone();
9799

98100
if let Some(info) = self.batch_queue.pop_front() {
99101
let fut = Box::pin(async move {
102+
let derive_start = Instant::now();
103+
100104
// get the batch commit data.
101105
let batch = database
102106
.get_batch_by_index(info.index)
@@ -107,6 +111,12 @@ where
107111
// derive the attributes and attach the corresponding batch info.
108112
let attrs =
109113
derive(batch, provider, database).await.map_err(|err| (info.clone(), err))?;
114+
115+
// update metrics.
116+
metrics.derived_blocks.increment(attrs.len() as u64);
117+
let execution_duration = derive_start.elapsed().as_secs_f64();
118+
metrics.blocks_per_second.set(attrs.len() as f64 / execution_duration);
119+
110120
Ok(attrs.into_iter().map(|attr| (attr, *info).into()).collect())
111121
});
112122
return Some(fut);
@@ -415,14 +425,14 @@ mod tests {
415425
break
416426
}
417427
}
418-
let expected = ScrollPayloadAttributes{
419-
payload_attributes: PayloadAttributes{
428+
let expected = ScrollPayloadAttributes {
429+
payload_attributes: PayloadAttributes {
420430
timestamp: 1696935657,
421431
..Default::default()
422432
},
423433
transactions: Some(vec![bytes!("f88c8202658417d7840082a4f294530000000000000000000000000000000000000280a4bede39b500000000000000000000000000000000000000000000000000000001669aa2f583104ec4a07461e6555f927393ebdf5f183738450c3842bc3b86a1db7549d9bee21fadd0b1a06d7ba96897bd9fb8e838a327d3ca34be66da11955f10d1fb2264949071e9e8cd")]),
424434
no_tx_pool: true,
425-
block_data_hint: Some(BlockDataHint{ extra_data: bytes!("d883050000846765746888676f312e31392e31856c696e757800000000000000909feed55823062d606d517c3f971201615962991dbc2c1ef5897b3b2c9bd0b93b5bae6e5cedbddfc4bcbbe731477beaca8b3eff111de7ac67a388850405265200"), difficulty: U256::from(2) }),
435+
block_data_hint: Some(BlockDataHint { extra_data: bytes!("d883050000846765746888676f312e31392e31856c696e757800000000000000909feed55823062d606d517c3f971201615962991dbc2c1ef5897b3b2c9bd0b93b5bae6e5cedbddfc4bcbbe731477beaca8b3eff111de7ac67a388850405265200"), difficulty: U256::from(2) }),
426436
};
427437
assert_eq!(attribute, expected);
428438

@@ -478,20 +488,20 @@ mod tests {
478488
let attribute =
479489
attributes.iter().find(|a| a.payload_attributes.timestamp == 1696935384).unwrap();
480490

481-
let expected = ScrollPayloadAttributes{
482-
payload_attributes: PayloadAttributes{
491+
let expected = ScrollPayloadAttributes {
492+
payload_attributes: PayloadAttributes {
483493
timestamp: 1696935384,
484494
..Default::default()
485495
},
486496
transactions: Some(vec![bytes!("7ef901b7218302904094781e90f1c8fc4611c9b7497c3b47f99ef6969cbc80b901848ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf0000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000000000000000000000000000006a94d74f4300000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000947885bcbd5cecef1336b5300fb5186a12ddd8c478"), bytes!("7ef901b7228302904094781e90f1c8fc4611c9b7497c3b47f99ef6969cbc80b901848ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf000000000000000000000000000000000000000000000000000470de4df820000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f00000000000000000000000000000000000000000000000000470de4df8200000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000947885bcbd5cecef1336b5300fb5186a12ddd8c478")]),
487497
no_tx_pool: true,
488-
block_data_hint: None
498+
block_data_hint: None,
489499
};
490500
assert_eq!(attribute, &expected);
491501

492502
let attribute = attributes.last().unwrap();
493-
let expected = ScrollPayloadAttributes{
494-
payload_attributes: PayloadAttributes{
503+
let expected = ScrollPayloadAttributes {
504+
payload_attributes: PayloadAttributes {
495505
timestamp: 1696935657,
496506
..Default::default()
497507
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use metrics::{Counter, Gauge};
2+
use metrics_derive::Metrics;
3+
4+
/// The metrics for the [`super::DerivationPipeline`].
5+
#[derive(Metrics, Clone)]
6+
#[metrics(scope = "derivation_pipeline")]
7+
pub struct DerivationPipelineMetrics {
8+
/// A counter on the derived L2 blocks processed.
9+
pub derived_blocks: Counter,
10+
/// The blocks per second derived by the pipeline.
11+
pub blocks_per_second: Gauge,
12+
}

crates/engine/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ scroll-network.workspace = true
4545
async-trait = { workspace = true, optional = true }
4646
eyre.workspace = true
4747
futures.workspace = true
48+
metrics.workspace = true
49+
metrics-derive.workspace = true
4850
thiserror.workspace = true
4951
tokio.workspace = true
5052
tracing.workspace = true

0 commit comments

Comments
 (0)