11use metrics:: Histogram ;
22use metrics_derive:: Metrics ;
3+ use reth_scroll_primitives:: ScrollBlock ;
34use std:: { collections:: HashMap , time:: Instant } ;
45use strum:: { EnumIter , IntoEnumIterator } ;
56
@@ -20,17 +21,33 @@ impl MetricsHandler {
2021
2122 /// Starts tracking a new block building task.
2223 pub ( crate ) fn start_block_building_recording ( & mut self ) {
23- if self . block_building_meter . start . is_some ( ) {
24+ if self . block_building_meter . block_building_start . is_some ( ) {
2425 tracing:: warn!( target: "scroll::chain_orchestrator" , "block building recording is already ongoing, overwriting" ) ;
2526 }
26- self . block_building_meter . start = Some ( Instant :: now ( ) ) ;
27+ self . block_building_meter . block_building_start = Some ( Instant :: now ( ) ) ;
2728 }
2829
29- /// The duration of the current block building task if any.
30- pub ( crate ) fn finish_block_building_recording ( & mut self ) {
31- let duration = self . block_building_meter . start . take ( ) . map ( |start| start. elapsed ( ) ) ;
32- if let Some ( duration) = duration {
33- self . block_building_meter . metric . block_building_duration . record ( duration. as_secs_f64 ( ) ) ;
30+ /// Finishes tracking the current block building task.
31+ pub ( crate ) fn finish_block_building_recording ( & mut self , block : Option < & ScrollBlock > ) {
32+ let now = Instant :: now ( ) ;
33+ if let Some ( t) = self . block_building_meter . block_building_start . take ( ) {
34+ let elapsed = now. duration_since ( t) . as_secs_f64 ( ) ;
35+ self . block_building_meter . metric . all_block_building_duration . record ( elapsed) ;
36+
37+ // Record only if it's not an empty block
38+ let is_empty_block = block. map ( |b| b. body . transactions . is_empty ( ) ) . unwrap_or ( true ) ;
39+ if !is_empty_block {
40+ self . block_building_meter . metric . block_building_duration . record ( elapsed) ;
41+ }
42+ }
43+
44+ if block. is_some ( ) {
45+ if let Some ( t) = self . block_building_meter . last_block_building_time . replace ( now) {
46+ self . block_building_meter
47+ . metric
48+ . consecutive_block_interval
49+ . record ( now. duration_since ( t) . as_secs_f64 ( ) ) ;
50+ }
3451 }
3552 }
3653}
@@ -104,13 +121,18 @@ pub(crate) struct ChainOrchestratorMetrics {
104121#[ derive( Debug , Default ) ]
105122pub ( crate ) struct BlockBuildingMeter {
106123 metric : BlockBuildingMetric ,
107- start : Option < Instant > ,
124+ block_building_start : Option < Instant > ,
125+ last_block_building_time : Option < Instant > ,
108126}
109127
110128/// Block building related metric.
111129#[ derive( Metrics , Clone ) ]
112130#[ metrics( scope = "chain_orchestrator" ) ]
113131pub ( crate ) struct BlockBuildingMetric {
114- /// The duration of the block building task.
132+ /// The duration of the block building task without empty block
115133 block_building_duration : Histogram ,
134+ /// The duration of the block building task for all blocks include empty block
135+ all_block_building_duration : Histogram ,
136+ /// The duration of the block interval include empty block
137+ consecutive_block_interval : Histogram ,
116138}
0 commit comments