Skip to content

Commit 48b0809

Browse files
authored
Add tx count metric for blocks (#571)
* Add tx count metric for blocks * Run lint
1 parent 39cbaf9 commit 48b0809

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

node/src/ethereum_l1/execution_layer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ impl ExecutionLayer {
174174
let count = u16::try_from(l2_block.prebuilt_tx_list.tx_list.len())?;
175175
tx_vec.extend(l2_block.prebuilt_tx_list.tx_list.clone());
176176

177+
// Emit metrics for transaction count in this block
178+
self.metrics.observe_block_tx_count(u64::from(count));
179+
177180
/* times_shift is the difference in seconds between the current L2 block and the L2 previous block. */
178181
let time_shift: u8 = if i == 0 {
179182
/* For first block, we don't have a previous block to compare the timestamp with. */

node/src/metrics/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Metrics {
1818
batch_propose_tries: Histogram,
1919
batch_block_count: Histogram,
2020
batch_blob_size: Histogram,
21+
block_tx_count: Histogram,
2122
rpc_driver_call_duration: HistogramVec,
2223
rpc_driver_call: CounterVec,
2324
rpc_driver_call_error: CounterVec,
@@ -142,6 +143,20 @@ impl Metrics {
142143
error!("Error: Failed to register batch_blob_size: {}", err);
143144
}
144145

146+
let opts = HistogramOpts::new("block_tx_count", "Number of transactions in each block")
147+
.buckets(vec![
148+
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
149+
16.0, 17.0, 18.0, 19.0, 20.0, 25.0, 30.0, 40.0, 50.0, 100.0, 200.0, 500.0, 1000.0,
150+
]);
151+
let block_tx_count = match Histogram::with_opts(opts) {
152+
Ok(histogram) => histogram,
153+
Err(err) => panic!("Failed to create block_tx_count histogram: {err}"),
154+
};
155+
156+
if let Err(err) = registry.register(Box::new(block_tx_count.clone())) {
157+
error!("Error: Failed to register block_tx_count: {}", err);
158+
}
159+
145160
let opts = HistogramOpts::new(
146161
"rpc_driver_call_duration_seconds",
147162
"Duration of RPC calls to driver in seconds",
@@ -205,6 +220,7 @@ impl Metrics {
205220
batch_propose_tries,
206221
batch_block_count,
207222
batch_blob_size,
223+
block_tx_count,
208224
rpc_driver_call_duration,
209225
rpc_driver_call,
210226
rpc_driver_call_error,
@@ -260,6 +276,11 @@ impl Metrics {
260276
self.batch_blob_size.observe(blob_size as f64);
261277
}
262278

279+
#[allow(clippy::cast_precision_loss)]
280+
pub fn observe_block_tx_count(&self, tx_count: u64) {
281+
self.block_tx_count.observe(tx_count as f64);
282+
}
283+
263284
pub fn observe_rpc_driver_call_duration(&self, method: &str, duration: f64) {
264285
if let Ok(metric) = self
265286
.rpc_driver_call_duration
@@ -353,6 +374,7 @@ mod tests {
353374
metrics.inc_batch_confirmed();
354375
metrics.observe_batch_propose_tries(1);
355376
metrics.observe_batch_info(5, 1000);
377+
metrics.observe_block_tx_count(3);
356378

357379
let output = metrics.gather();
358380
println!("{output}");
@@ -368,6 +390,8 @@ mod tests {
368390
assert!(output.contains("batch_propose_tries_count 1"));
369391
assert!(output.contains("batch_block_count_sum 5"));
370392
assert!(output.contains("batch_blob_size_sum 1000"));
393+
assert!(output.contains("block_tx_count_count 1"));
394+
assert!(output.contains("block_tx_count_sum 3"));
371395
}
372396

373397
#[test]

0 commit comments

Comments
 (0)