1
1
use std:: any:: Any ;
2
+ use std:: time:: Duration ;
2
3
use std:: { collections:: HashMap , sync:: Arc } ;
3
4
4
5
use pyo3:: prelude:: * ;
@@ -32,11 +33,26 @@ pub struct MetricHistogramRef {
32
33
histogram : Arc < dyn metrics:: Histogram > ,
33
34
}
34
35
36
+ #[ pyclass]
37
+ pub struct MetricHistogramFloatRef {
38
+ histogram : Arc < dyn metrics:: HistogramF64 > ,
39
+ }
40
+
41
+ #[ pyclass]
42
+ pub struct MetricHistogramDurationRef {
43
+ histogram : Arc < dyn metrics:: HistogramDuration > ,
44
+ }
45
+
35
46
#[ pyclass]
36
47
pub struct MetricGaugeRef {
37
48
gauge : Arc < dyn metrics:: Gauge > ,
38
49
}
39
50
51
+ #[ pyclass]
52
+ pub struct MetricGaugeFloatRef {
53
+ gauge : Arc < dyn metrics:: GaugeF64 > ,
54
+ }
55
+
40
56
pub fn new_metric_meter ( runtime_ref : & runtime:: RuntimeRef ) -> Option < MetricMeterRef > {
41
57
runtime_ref
42
58
. runtime
@@ -84,6 +100,36 @@ impl MetricMeterRef {
84
100
}
85
101
}
86
102
103
+ fn new_histogram_float (
104
+ & self ,
105
+ name : String ,
106
+ description : Option < String > ,
107
+ unit : Option < String > ,
108
+ ) -> MetricHistogramFloatRef {
109
+ MetricHistogramFloatRef {
110
+ histogram : self . meter . inner . histogram_f64 ( build_metric_parameters (
111
+ name,
112
+ description,
113
+ unit,
114
+ ) ) ,
115
+ }
116
+ }
117
+
118
+ fn new_histogram_duration (
119
+ & self ,
120
+ name : String ,
121
+ description : Option < String > ,
122
+ unit : Option < String > ,
123
+ ) -> MetricHistogramDurationRef {
124
+ MetricHistogramDurationRef {
125
+ histogram : self . meter . inner . histogram_duration ( build_metric_parameters (
126
+ name,
127
+ description,
128
+ unit,
129
+ ) ) ,
130
+ }
131
+ }
132
+
87
133
fn new_gauge (
88
134
& self ,
89
135
name : String ,
@@ -97,6 +143,20 @@ impl MetricMeterRef {
97
143
. gauge ( build_metric_parameters ( name, description, unit) ) ,
98
144
}
99
145
}
146
+
147
+ fn new_gauge_float (
148
+ & self ,
149
+ name : String ,
150
+ description : Option < String > ,
151
+ unit : Option < String > ,
152
+ ) -> MetricGaugeFloatRef {
153
+ MetricGaugeFloatRef {
154
+ gauge : self
155
+ . meter
156
+ . inner
157
+ . gauge_f64 ( build_metric_parameters ( name, description, unit) ) ,
158
+ }
159
+ }
100
160
}
101
161
102
162
#[ pymethods]
@@ -113,13 +173,35 @@ impl MetricHistogramRef {
113
173
}
114
174
}
115
175
176
+ #[ pymethods]
177
+ impl MetricHistogramFloatRef {
178
+ fn record ( & self , value : f64 , attrs_ref : & MetricAttributesRef ) {
179
+ self . histogram . record ( value, & attrs_ref. attrs ) ;
180
+ }
181
+ }
182
+
183
+ #[ pymethods]
184
+ impl MetricHistogramDurationRef {
185
+ fn record ( & self , value_ms : u64 , attrs_ref : & MetricAttributesRef ) {
186
+ self . histogram
187
+ . record ( Duration :: from_millis ( value_ms) , & attrs_ref. attrs ) ;
188
+ }
189
+ }
190
+
116
191
#[ pymethods]
117
192
impl MetricGaugeRef {
118
193
fn set ( & self , value : u64 , attrs_ref : & MetricAttributesRef ) {
119
194
self . gauge . record ( value, & attrs_ref. attrs ) ;
120
195
}
121
196
}
122
197
198
+ #[ pymethods]
199
+ impl MetricGaugeFloatRef {
200
+ fn set ( & self , value : f64 , attrs_ref : & MetricAttributesRef ) {
201
+ self . gauge . record ( value, & attrs_ref. attrs ) ;
202
+ }
203
+ }
204
+
123
205
fn build_metric_parameters (
124
206
name : String ,
125
207
description : Option < String > ,
@@ -192,16 +274,18 @@ pub struct BufferedMetricUpdate {
192
274
}
193
275
194
276
#[ derive( Clone ) ]
195
- pub struct BufferedMetricUpdateValue ( metrics:: MetricUpdateVal ) ;
277
+ pub enum BufferedMetricUpdateValue {
278
+ U64 ( u64 ) ,
279
+ U128 ( u128 ) ,
280
+ F64 ( f64 ) ,
281
+ }
196
282
197
283
impl IntoPy < PyObject > for BufferedMetricUpdateValue {
198
284
fn into_py ( self , py : Python ) -> PyObject {
199
- match self . 0 {
200
- metrics:: MetricUpdateVal :: Delta ( v) => v. into_py ( py) ,
201
- metrics:: MetricUpdateVal :: DeltaF64 ( v) => v. into_py ( py) ,
202
- metrics:: MetricUpdateVal :: Value ( v) => v. into_py ( py) ,
203
- metrics:: MetricUpdateVal :: ValueF64 ( v) => v. into_py ( py) ,
204
- metrics:: MetricUpdateVal :: Duration ( v) => v. as_millis ( ) . into_py ( py) ,
285
+ match self {
286
+ BufferedMetricUpdateValue :: U64 ( v) => v. into_py ( py) ,
287
+ BufferedMetricUpdateValue :: U128 ( v) => v. into_py ( py) ,
288
+ BufferedMetricUpdateValue :: F64 ( v) => v. into_py ( py) ,
205
289
}
206
290
}
207
291
}
@@ -236,16 +320,18 @@ impl CustomMetricAttributes for BufferedMetricAttributes {
236
320
pub fn convert_metric_events < ' p > (
237
321
py : Python < ' p > ,
238
322
events : Vec < MetricEvent < BufferedMetricRef > > ,
323
+ durations_as_seconds : bool ,
239
324
) -> Vec < BufferedMetricUpdate > {
240
325
events
241
326
. into_iter ( )
242
- . filter_map ( |e| convert_metric_event ( py, e) )
327
+ . filter_map ( |e| convert_metric_event ( py, e, durations_as_seconds ) )
243
328
. collect ( )
244
329
}
245
330
246
331
fn convert_metric_event < ' p > (
247
332
py : Python < ' p > ,
248
333
event : MetricEvent < BufferedMetricRef > ,
334
+ durations_as_seconds : bool ,
249
335
) -> Option < BufferedMetricUpdate > {
250
336
match event {
251
337
// Create the metric and put it on the lazy ref
@@ -262,9 +348,19 @@ fn convert_metric_event<'p>(
262
348
description : Some ( params. description )
263
349
. filter ( |s| !s. is_empty ( ) )
264
350
. map ( |s| s. to_string ( ) ) ,
265
- unit : Some ( params. unit )
266
- . filter ( |s| !s. is_empty ( ) )
267
- . map ( |s| s. to_string ( ) ) ,
351
+ unit : if matches ! ( kind, metrics:: MetricKind :: HistogramDuration )
352
+ && params. unit == "duration"
353
+ {
354
+ if durations_as_seconds {
355
+ Some ( "s" . to_owned ( ) )
356
+ } else {
357
+ Some ( "ms" . to_owned ( ) )
358
+ }
359
+ } else if params. unit . is_empty ( ) {
360
+ None
361
+ } else {
362
+ Some ( params. unit . to_string ( ) )
363
+ } ,
268
364
kind : match kind {
269
365
metrics:: MetricKind :: Counter => 0 ,
270
366
metrics:: MetricKind :: Gauge | metrics:: MetricKind :: GaugeF64 => 1 ,
@@ -324,7 +420,18 @@ fn convert_metric_event<'p>(
324
420
update,
325
421
} => Some ( BufferedMetricUpdate {
326
422
metric : instrument. get ( ) . clone ( ) . 0 . clone ( ) ,
327
- value : BufferedMetricUpdateValue ( update) ,
423
+ value : match update {
424
+ metrics:: MetricUpdateVal :: Duration ( v) if durations_as_seconds => {
425
+ BufferedMetricUpdateValue :: F64 ( v. as_secs_f64 ( ) )
426
+ }
427
+ metrics:: MetricUpdateVal :: Duration ( v) => {
428
+ BufferedMetricUpdateValue :: U128 ( v. as_millis ( ) )
429
+ }
430
+ metrics:: MetricUpdateVal :: Delta ( v) => BufferedMetricUpdateValue :: U64 ( v) ,
431
+ metrics:: MetricUpdateVal :: DeltaF64 ( v) => BufferedMetricUpdateValue :: F64 ( v) ,
432
+ metrics:: MetricUpdateVal :: Value ( v) => BufferedMetricUpdateValue :: U64 ( v) ,
433
+ metrics:: MetricUpdateVal :: ValueF64 ( v) => BufferedMetricUpdateValue :: F64 ( v) ,
434
+ } ,
328
435
attributes : attributes
329
436
. get ( )
330
437
. clone ( )
0 commit comments