11use metrics:: Histogram ;
22use metrics_derive:: Metrics ;
3+ use reth_scroll_primitives:: ScrollBlock ;
34use std:: { collections:: HashMap , time:: Instant } ;
45use strum:: { EnumIter , IntoEnumIterator } ;
56
@@ -26,37 +27,28 @@ impl MetricsHandler {
2627 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_no_empty_block_building_recording ( & self ) {
31- if let Some ( block_build_start) = self . block_building_meter . block_building_start {
32- let duration = block_build_start. elapsed ( ) ;
33- self . block_building_meter . metric . block_building_duration . record ( duration. as_secs_f64 ( ) ) ;
34- }
35- }
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) ;
3636
37- pub ( crate ) fn finish_all_block_building_recording ( & self ) {
38- if let Some ( block_build_start) = self . block_building_meter . block_building_start {
39- let duration = block_build_start. elapsed ( ) ;
40- self . block_building_meter
41- . metric
42- . all_block_building_duration
43- . record ( duration. as_secs_f64 ( ) ) ;
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+ }
4442 }
45- }
4643
47- pub ( crate ) fn finish_block_building_interval_recording ( & mut self ) {
48- let interval = self
49- . block_building_meter
50- . last_block_building_time
51- . take ( )
52- . map ( |last_block_building_time| last_block_building_time. elapsed ( ) ) ;
53- if let Some ( interval) = interval {
54- self . block_building_meter
55- . metric
56- . consecutive_block_interval
57- . record ( interval. as_secs_f64 ( ) ) ;
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+ }
5851 }
59- self . block_building_meter . last_block_building_time = Some ( Instant :: now ( ) ) ;
6052 }
6153}
6254
0 commit comments