@@ -100,6 +100,18 @@ pub fn linear_bucket_interval(start_value: f64, width: f64, end_value: f64) -> O
100100 Some ( buckets)
101101}
102102
103+ /// The unit of measurement for latency metrics.
104+ pub enum MeasurementUnit {
105+ /// Measure latency in seconds.
106+ Seconds ,
107+ /// Measure latency in milliseconds.
108+ Milliseconds ,
109+ /// Measure latency in microseconds.
110+ Microseconds ,
111+ /// Measure latency in nanoseconds.
112+ Nanoseconds ,
113+ }
114+
103115/// A guard for an active latency measurement.
104116///
105117/// Finishes the measurement when dropped, and then updates the `Metric`.
@@ -109,6 +121,7 @@ where
109121{
110122 start : Instant ,
111123 metric : Option < & ' metric Metric > ,
124+ unit : MeasurementUnit ,
112125}
113126
114127impl < Metric > ActiveMeasurementGuard < ' _ , Metric >
@@ -126,7 +139,16 @@ where
126139 fn finish_by_ref ( & mut self ) -> f64 {
127140 match self . metric . take ( ) {
128141 Some ( metric) => {
129- let latency = self . start . elapsed ( ) . as_secs_f64 ( ) * 1000.0 ;
142+ let latency = match self . unit {
143+ MeasurementUnit :: Seconds => self . start . elapsed ( ) . as_secs_f64 ( ) ,
144+ MeasurementUnit :: Milliseconds => self . start . elapsed ( ) . as_secs_f64 ( ) * 1000.0 ,
145+ MeasurementUnit :: Microseconds => {
146+ self . start . elapsed ( ) . as_secs_f64 ( ) * 1_000_000.0
147+ }
148+ MeasurementUnit :: Nanoseconds => {
149+ self . start . elapsed ( ) . as_secs_f64 ( ) * 1_000_000_000.0
150+ }
151+ } ;
130152 metric. finish_measurement ( latency) ;
131153 latency
132154 }
@@ -152,35 +174,37 @@ where
152174pub trait MeasureLatency : Sized {
153175 /// Starts measuring the latency, finishing when the returned
154176 /// [`ActiveMeasurementGuard`] is dropped.
155- fn measure_latency ( & self ) -> ActiveMeasurementGuard < ' _ , Self > ;
177+ fn measure_latency ( & self , unit : MeasurementUnit ) -> ActiveMeasurementGuard < ' _ , Self > ;
156178
157- /// Updates the metric with measured latency in `milliseconds` .
158- fn finish_measurement ( & self , milliseconds : f64 ) ;
179+ /// Updates the metric with measured latency in specified unit .
180+ fn finish_measurement ( & self , value : f64 ) ;
159181}
160182
161183impl MeasureLatency for HistogramVec {
162- fn measure_latency ( & self ) -> ActiveMeasurementGuard < ' _ , Self > {
184+ fn measure_latency ( & self , unit : MeasurementUnit ) -> ActiveMeasurementGuard < ' _ , Self > {
163185 ActiveMeasurementGuard {
164186 start : Instant :: now ( ) ,
165187 metric : Some ( self ) ,
188+ unit,
166189 }
167190 }
168191
169- fn finish_measurement ( & self , milliseconds : f64 ) {
170- self . with_label_values ( & [ ] ) . observe ( milliseconds ) ;
192+ fn finish_measurement ( & self , value : f64 ) {
193+ self . with_label_values ( & [ ] ) . observe ( value ) ;
171194 }
172195}
173196
174197impl MeasureLatency for Histogram {
175- fn measure_latency ( & self ) -> ActiveMeasurementGuard < ' _ , Self > {
198+ fn measure_latency ( & self , unit : MeasurementUnit ) -> ActiveMeasurementGuard < ' _ , Self > {
176199 ActiveMeasurementGuard {
177200 start : Instant :: now ( ) ,
178201 metric : Some ( self ) ,
202+ unit,
179203 }
180204 }
181205
182- fn finish_measurement ( & self , milliseconds : f64 ) {
183- self . observe ( milliseconds ) ;
206+ fn finish_measurement ( & self , value : f64 ) {
207+ self . observe ( value ) ;
184208 }
185209}
186210
0 commit comments