11//! Prometheus metrics for state transition.
22
33use std:: sync:: LazyLock ;
4+ use std:: time:: Instant ;
45
5- use prometheus:: { IntCounter , IntCounterVec , register_int_counter, register_int_counter_vec} ;
6+ use prometheus:: {
7+ Histogram , IntCounter , IntCounterVec , register_int_counter, register_int_counter_vec,
8+ } ;
69
710static LEAN_STATE_TRANSITION_SLOTS_PROCESSED_TOTAL : LazyLock < IntCounter > = LazyLock :: new ( || {
811 register_int_counter ! (
@@ -45,7 +48,7 @@ pub fn inc_finalizations(result: &str) {
4548 LEAN_FINALIZATIONS_TOTAL . with_label_values ( & [ result] ) . inc ( ) ;
4649}
4750
48- static LEAN_STATE_TRANSITION_TIME_SECONDS : LazyLock < prometheus :: Histogram > = LazyLock :: new ( || {
51+ static LEAN_STATE_TRANSITION_TIME_SECONDS : LazyLock < Histogram > = LazyLock :: new ( || {
4952 prometheus:: register_histogram!(
5053 "lean_state_transition_time_seconds" ,
5154 "Duration of the entire state transition" ,
@@ -54,7 +57,7 @@ static LEAN_STATE_TRANSITION_TIME_SECONDS: LazyLock<prometheus::Histogram> = Laz
5457 . unwrap ( )
5558} ) ;
5659
57- static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS : LazyLock < prometheus :: Histogram > =
60+ static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS : LazyLock < Histogram > =
5861 LazyLock :: new ( || {
5962 prometheus:: register_histogram!(
6063 "lean_state_transition_slots_processing_time_seconds" ,
@@ -64,7 +67,7 @@ static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS: LazyLock<prometheus:
6467 . unwrap ( )
6568 } ) ;
6669
67- static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS : LazyLock < prometheus :: Histogram > =
70+ static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS : LazyLock < Histogram > =
6871 LazyLock :: new ( || {
6972 prometheus:: register_histogram!(
7073 "lean_state_transition_block_processing_time_seconds" ,
@@ -74,7 +77,7 @@ static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS: LazyLock<prometheus:
7477 . unwrap ( )
7578 } ) ;
7679
77- static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS : LazyLock < prometheus :: Histogram > =
80+ static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS : LazyLock < Histogram > =
7881 LazyLock :: new ( || {
7982 prometheus:: register_histogram!(
8083 "lean_state_transition_attestations_processing_time_seconds" ,
@@ -84,22 +87,43 @@ static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS: LazyLock<prom
8487 . unwrap ( )
8588 } ) ;
8689
87- /// Record state transition time in seconds.
88- pub fn observe_state_transition_time ( duration_secs : f64 ) {
89- LEAN_STATE_TRANSITION_TIME_SECONDS . observe ( duration_secs) ;
90+ /// A guard that records elapsed time to a histogram when dropped.
91+ pub struct TimingGuard {
92+ histogram : & ' static Histogram ,
93+ start : Instant ,
9094}
9195
92- /// Record slots processing time in seconds.
93- pub fn observe_slots_processing_time ( duration_secs : f64 ) {
94- LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS . observe ( duration_secs) ;
96+ impl TimingGuard {
97+ fn new ( histogram : & ' static Histogram ) -> Self {
98+ Self {
99+ histogram,
100+ start : Instant :: now ( ) ,
101+ }
102+ }
95103}
96104
97- /// Record block processing time in seconds.
98- pub fn observe_block_processing_time ( duration_secs : f64 ) {
99- LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS . observe ( duration_secs) ;
105+ impl Drop for TimingGuard {
106+ fn drop ( & mut self ) {
107+ self . histogram . observe ( self . start . elapsed ( ) . as_secs_f64 ( ) ) ;
108+ }
100109}
101110
102- /// Record attestations processing time in seconds.
103- pub fn observe_attestations_processing_time ( duration_secs : f64 ) {
104- LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS . observe ( duration_secs) ;
111+ /// Start timing state transition. Records duration when the guard is dropped.
112+ pub fn time_state_transition ( ) -> TimingGuard {
113+ TimingGuard :: new ( & LEAN_STATE_TRANSITION_TIME_SECONDS )
114+ }
115+
116+ /// Start timing slots processing. Records duration when the guard is dropped.
117+ pub fn time_slots_processing ( ) -> TimingGuard {
118+ TimingGuard :: new ( & LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS )
119+ }
120+
121+ /// Start timing block processing. Records duration when the guard is dropped.
122+ pub fn time_block_processing ( ) -> TimingGuard {
123+ TimingGuard :: new ( & LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS )
124+ }
125+
126+ /// Start timing attestations processing. Records duration when the guard is dropped.
127+ pub fn time_attestations_processing ( ) -> TimingGuard {
128+ TimingGuard :: new ( & LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS )
105129}
0 commit comments