@@ -3,6 +3,7 @@ use std::{
3
3
borrow:: Cow ,
4
4
fmt:: Debug ,
5
5
sync:: { Arc , OnceLock } ,
6
+ time:: Duration ,
6
7
} ;
7
8
8
9
/// Implementors of this trait are expected to be defined in each language's bridge.
@@ -23,7 +24,14 @@ pub trait CoreMeter: Send + Sync + Debug {
23
24
) -> MetricAttributes ;
24
25
fn counter ( & self , params : MetricParameters ) -> Arc < dyn Counter > ;
25
26
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 > ;
26
33
fn gauge ( & self , params : MetricParameters ) -> Arc < dyn Gauge > ;
34
+ fn gauge_f64 ( & self , params : MetricParameters ) -> Arc < dyn GaugeF64 > ;
27
35
}
28
36
29
37
#[ derive( Debug , Clone , derive_builder:: Builder ) ]
@@ -74,9 +82,22 @@ impl CoreMeter for Arc<dyn CoreMeter> {
74
82
fn histogram ( & self , params : MetricParameters ) -> Arc < dyn Histogram > {
75
83
self . as_ref ( ) . histogram ( params)
76
84
}
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
+
77
94
fn gauge ( & self , params : MetricParameters ) -> Arc < dyn Gauge > {
78
95
self . as_ref ( ) . gauge ( params)
79
96
}
97
+
98
+ fn gauge_f64 ( & self , params : MetricParameters ) -> Arc < dyn GaugeF64 > {
99
+ self . as_ref ( ) . gauge_f64 ( params)
100
+ }
80
101
}
81
102
82
103
/// Attributes which are provided every time a call to record a specific metric is made.
@@ -158,22 +179,34 @@ pub trait Histogram: Send + Sync {
158
179
// When referring to durations, this value is in millis
159
180
fn record ( & self , value : u64 , attributes : & MetricAttributes ) ;
160
181
}
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
+ }
161
189
162
190
pub trait Gauge : Send + Sync {
163
191
// When referring to durations, this value is in millis
164
192
fn record ( & self , value : u64 , attributes : & MetricAttributes ) ;
165
193
}
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
+ }
166
198
167
199
#[ derive( Debug , Clone ) ]
168
200
pub enum MetricEvent < I : BufferInstrumentRef > {
169
201
Create {
170
202
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
172
205
populate_into : LazyBufferInstrument < I > ,
173
206
kind : MetricKind ,
174
207
} ,
175
208
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
177
210
populate_into : BufferAttributes ,
178
211
/// If not `None`, use these already-initialized attributes as the base (extended with
179
212
/// `attributes`) for the ones you are about to initialize.
@@ -190,14 +223,18 @@ pub enum MetricEvent<I: BufferInstrumentRef> {
190
223
pub enum MetricKind {
191
224
Counter ,
192
225
Gauge ,
226
+ GaugeF64 ,
193
227
Histogram ,
228
+ HistogramF64 ,
229
+ HistogramDuration ,
194
230
}
195
231
#[ derive( Debug , Clone , Copy ) ]
196
232
pub enum MetricUpdateVal {
197
- // Currently all deltas are natural numbers
198
233
Delta ( u64 ) ,
199
- // Currently all values are natural numbers
234
+ DeltaF64 ( f64 ) ,
200
235
Value ( u64 ) ,
236
+ ValueF64 ( f64 ) ,
237
+ Duration ( Duration ) ,
201
238
}
202
239
203
240
pub trait MetricCallBufferer < I : BufferInstrumentRef > : Send + Sync {
@@ -260,9 +297,21 @@ impl CoreMeter for NoOpCoreMeter {
260
297
Arc :: new ( NoOpInstrument )
261
298
}
262
299
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
+
263
308
fn gauge ( & self , _: MetricParameters ) -> Arc < dyn Gauge > {
264
309
Arc :: new ( NoOpInstrument )
265
310
}
311
+
312
+ fn gauge_f64 ( & self , _: MetricParameters ) -> Arc < dyn GaugeF64 > {
313
+ Arc :: new ( NoOpInstrument )
314
+ }
266
315
}
267
316
268
317
pub struct NoOpInstrument ;
@@ -272,9 +321,18 @@ impl Counter for NoOpInstrument {
272
321
impl Histogram for NoOpInstrument {
273
322
fn record ( & self , _: u64 , _: & MetricAttributes ) { }
274
323
}
324
+ impl HistogramF64 for NoOpInstrument {
325
+ fn record ( & self , _: f64 , _: & MetricAttributes ) { }
326
+ }
327
+ impl HistogramDuration for NoOpInstrument {
328
+ fn record ( & self , _: Duration , _: & MetricAttributes ) { }
329
+ }
275
330
impl Gauge for NoOpInstrument {
276
331
fn record ( & self , _: u64 , _: & MetricAttributes ) { }
277
332
}
333
+ impl GaugeF64 for NoOpInstrument {
334
+ fn record ( & self , _: f64 , _: & MetricAttributes ) { }
335
+ }
278
336
279
337
#[ derive( Debug , Clone ) ]
280
338
pub struct NoOpAttributes ;
@@ -331,4 +389,17 @@ mod otel_impls {
331
389
}
332
390
}
333
391
}
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
+ }
334
405
}
0 commit comments