@@ -3,6 +3,7 @@ use std::{
33 borrow:: Cow ,
44 fmt:: Debug ,
55 sync:: { Arc , OnceLock } ,
6+ time:: Duration ,
67} ;
78
89/// Implementors of this trait are expected to be defined in each language's bridge.
@@ -23,7 +24,14 @@ pub trait CoreMeter: Send + Sync + Debug {
2324 ) -> MetricAttributes ;
2425 fn counter ( & self , params : MetricParameters ) -> Arc < dyn Counter > ;
2526 fn histogram ( & self , params : MetricParameters ) -> Arc < dyn Histogram > ;
27+ fn histogram_f64 ( & self , params : MetricParameters ) -> Arc < dyn HistogramF64 > ;
28+ /// Create a histogram which records Durations. Implementations should choose to emit in
29+ /// either milliseconds or seconds depending on how they have been configured.
30+ /// [MetricParameters::unit] should be overwritten by implementations to be `ms` or `s`
31+ /// accordingly.
32+ fn histogram_duration ( & self , params : MetricParameters ) -> Arc < dyn HistogramDuration > ;
2633 fn gauge ( & self , params : MetricParameters ) -> Arc < dyn Gauge > ;
34+ fn gauge_f64 ( & self , params : MetricParameters ) -> Arc < dyn GaugeF64 > ;
2735}
2836
2937#[ derive( Debug , Clone , derive_builder:: Builder ) ]
@@ -74,9 +82,22 @@ impl CoreMeter for Arc<dyn CoreMeter> {
7482 fn histogram ( & self , params : MetricParameters ) -> Arc < dyn Histogram > {
7583 self . as_ref ( ) . histogram ( params)
7684 }
85+
86+ fn histogram_f64 ( & self , params : MetricParameters ) -> Arc < dyn HistogramF64 > {
87+ self . as_ref ( ) . histogram_f64 ( params)
88+ }
89+
90+ fn histogram_duration ( & self , params : MetricParameters ) -> Arc < dyn HistogramDuration > {
91+ self . as_ref ( ) . histogram_duration ( params)
92+ }
93+
7794 fn gauge ( & self , params : MetricParameters ) -> Arc < dyn Gauge > {
7895 self . as_ref ( ) . gauge ( params)
7996 }
97+
98+ fn gauge_f64 ( & self , params : MetricParameters ) -> Arc < dyn GaugeF64 > {
99+ self . as_ref ( ) . gauge_f64 ( params)
100+ }
80101}
81102
82103/// Attributes which are provided every time a call to record a specific metric is made.
@@ -158,22 +179,34 @@ pub trait Histogram: Send + Sync {
158179 // When referring to durations, this value is in millis
159180 fn record ( & self , value : u64 , attributes : & MetricAttributes ) ;
160181}
182+ pub trait HistogramF64 : Send + Sync {
183+ // When referring to durations, this value is in seconds
184+ fn record ( & self , value : f64 , attributes : & MetricAttributes ) ;
185+ }
186+ pub trait HistogramDuration : Send + Sync {
187+ fn record ( & self , value : Duration , attributes : & MetricAttributes ) ;
188+ }
161189
162190pub trait Gauge : Send + Sync {
163191 // When referring to durations, this value is in millis
164192 fn record ( & self , value : u64 , attributes : & MetricAttributes ) ;
165193}
194+ pub trait GaugeF64 : Send + Sync {
195+ // When referring to durations, this value is in seconds
196+ fn record ( & self , value : f64 , attributes : & MetricAttributes ) ;
197+ }
166198
167199#[ derive( Debug , Clone ) ]
168200pub enum MetricEvent < I : BufferInstrumentRef > {
169201 Create {
170202 params : MetricParameters ,
171- /// One you receive this event, call `set` on this with the initialized instrument reference
203+ /// Once you receive this event, call `set` on this with the initialized instrument
204+ /// reference
172205 populate_into : LazyBufferInstrument < I > ,
173206 kind : MetricKind ,
174207 } ,
175208 CreateAttributes {
176- /// One you receive this event, call `set` on this with the initialized attributes
209+ /// Once you receive this event, call `set` on this with the initialized attributes
177210 populate_into : BufferAttributes ,
178211 /// If not `None`, use these already-initialized attributes as the base (extended with
179212 /// `attributes`) for the ones you are about to initialize.
@@ -190,14 +223,18 @@ pub enum MetricEvent<I: BufferInstrumentRef> {
190223pub enum MetricKind {
191224 Counter ,
192225 Gauge ,
226+ GaugeF64 ,
193227 Histogram ,
228+ HistogramF64 ,
229+ HistogramDuration ,
194230}
195231#[ derive( Debug , Clone , Copy ) ]
196232pub enum MetricUpdateVal {
197- // Currently all deltas are natural numbers
198233 Delta ( u64 ) ,
199- // Currently all values are natural numbers
234+ DeltaF64 ( f64 ) ,
200235 Value ( u64 ) ,
236+ ValueF64 ( f64 ) ,
237+ Duration ( Duration ) ,
201238}
202239
203240pub trait MetricCallBufferer < I : BufferInstrumentRef > : Send + Sync {
@@ -260,9 +297,21 @@ impl CoreMeter for NoOpCoreMeter {
260297 Arc :: new ( NoOpInstrument )
261298 }
262299
300+ fn histogram_f64 ( & self , _: MetricParameters ) -> Arc < dyn HistogramF64 > {
301+ Arc :: new ( NoOpInstrument )
302+ }
303+
304+ fn histogram_duration ( & self , _: MetricParameters ) -> Arc < dyn HistogramDuration > {
305+ Arc :: new ( NoOpInstrument )
306+ }
307+
263308 fn gauge ( & self , _: MetricParameters ) -> Arc < dyn Gauge > {
264309 Arc :: new ( NoOpInstrument )
265310 }
311+
312+ fn gauge_f64 ( & self , _: MetricParameters ) -> Arc < dyn GaugeF64 > {
313+ Arc :: new ( NoOpInstrument )
314+ }
266315}
267316
268317pub struct NoOpInstrument ;
@@ -272,9 +321,18 @@ impl Counter for NoOpInstrument {
272321impl Histogram for NoOpInstrument {
273322 fn record ( & self , _: u64 , _: & MetricAttributes ) { }
274323}
324+ impl HistogramF64 for NoOpInstrument {
325+ fn record ( & self , _: f64 , _: & MetricAttributes ) { }
326+ }
327+ impl HistogramDuration for NoOpInstrument {
328+ fn record ( & self , _: Duration , _: & MetricAttributes ) { }
329+ }
275330impl Gauge for NoOpInstrument {
276331 fn record ( & self , _: u64 , _: & MetricAttributes ) { }
277332}
333+ impl GaugeF64 for NoOpInstrument {
334+ fn record ( & self , _: f64 , _: & MetricAttributes ) { }
335+ }
278336
279337#[ derive( Debug , Clone ) ]
280338pub struct NoOpAttributes ;
@@ -331,4 +389,17 @@ mod otel_impls {
331389 }
332390 }
333391 }
392+
393+ impl HistogramF64 for metrics:: Histogram < f64 > {
394+ fn record ( & self , value : f64 , attributes : & MetricAttributes ) {
395+ if let MetricAttributes :: OTel { kvs } = attributes {
396+ self . record ( value, kvs) ;
397+ } else {
398+ debug_assert ! (
399+ false ,
400+ "Must use OTel attributes with an OTel metric implementation"
401+ ) ;
402+ }
403+ }
404+ }
334405}
0 commit comments