@@ -42,46 +42,51 @@ pub const OTEL_METRICS_KNOWN_FIELD_LIST: [&str; 5] = [
42
42
fn flatten_exemplar (
43
43
exemplars : & [ Exemplar ] ,
44
44
other_attributes : & mut Map < String , Value > ,
45
- ) -> Map < String , Value > {
46
- let mut exemplar_json = Map :: new ( ) ;
47
- for exemplar in exemplars {
48
- insert_attributes (
49
- & mut exemplar_json,
50
- & exemplar. filtered_attributes ,
51
- other_attributes,
52
- ) ;
53
- exemplar_json. insert (
54
- "exemplar_time_unix_nano" . to_string ( ) ,
55
- Value :: String ( convert_epoch_nano_to_timestamp (
56
- exemplar. time_unix_nano as i64 ,
57
- ) ) ,
58
- ) ;
59
- exemplar_json. insert (
60
- "exemplar_span_id" . to_string ( ) ,
61
- Value :: String ( hex:: encode ( & exemplar. span_id ) ) ,
62
- ) ;
63
- exemplar_json. insert (
64
- "exemplar_trace_id" . to_string ( ) ,
65
- Value :: String ( hex:: encode ( & exemplar. trace_id ) ) ,
66
- ) ;
67
- if let Some ( value) = & exemplar. value {
68
- match value {
69
- ExemplarValue :: AsDouble ( double_val) => {
70
- exemplar_json. insert (
71
- "exemplar_value" . to_string ( ) ,
72
- Value :: Number ( serde_json:: Number :: from_f64 ( * double_val) . unwrap ( ) ) ,
73
- ) ;
74
- }
75
- ExemplarValue :: AsInt ( int_val) => {
76
- exemplar_json. insert (
77
- "exemplar_value" . to_string ( ) ,
78
- Value :: Number ( serde_json:: Number :: from ( * int_val) ) ,
79
- ) ;
45
+ ) -> Vec < Map < String , Value > > {
46
+ exemplars
47
+ . iter ( )
48
+ . map ( |exemplar| {
49
+ let mut exemplar_json = Map :: new ( ) ;
50
+ insert_attributes (
51
+ & mut exemplar_json,
52
+ & exemplar. filtered_attributes ,
53
+ other_attributes,
54
+ ) ;
55
+ exemplar_json. insert (
56
+ "exemplar_time_unix_nano" . to_string ( ) ,
57
+ Value :: String ( convert_epoch_nano_to_timestamp (
58
+ exemplar. time_unix_nano as i64 ,
59
+ ) ) ,
60
+ ) ;
61
+ exemplar_json. insert (
62
+ "exemplar_span_id" . to_string ( ) ,
63
+ Value :: String ( hex:: encode ( & exemplar. span_id ) ) ,
64
+ ) ;
65
+ exemplar_json. insert (
66
+ "exemplar_trace_id" . to_string ( ) ,
67
+ Value :: String ( hex:: encode ( & exemplar. trace_id ) ) ,
68
+ ) ;
69
+ if let Some ( value) = & exemplar. value {
70
+ match value {
71
+ ExemplarValue :: AsDouble ( double_val) => {
72
+ exemplar_json. insert (
73
+ "exemplar_value" . to_string ( ) ,
74
+ serde_json:: Number :: from_f64 ( * double_val)
75
+ . map ( Value :: Number )
76
+ . unwrap_or ( Value :: Null ) ,
77
+ ) ;
78
+ }
79
+ ExemplarValue :: AsInt ( int_val) => {
80
+ exemplar_json. insert (
81
+ "exemplar_value" . to_string ( ) ,
82
+ Value :: Number ( serde_json:: Number :: from ( * int_val) ) ,
83
+ ) ;
84
+ }
80
85
}
81
86
}
82
- }
83
- }
84
- exemplar_json
87
+ exemplar_json
88
+ } )
89
+ . collect ( )
85
90
}
86
91
87
92
/// otel metrics event has json array for number data points
@@ -113,17 +118,20 @@ fn flatten_number_data_points(
113
118
data_point. time_unix_nano as i64 ,
114
119
) ) ,
115
120
) ;
116
- let exemplar_json = flatten_exemplar ( & data_point. exemplars , other_attributes) ;
117
- for ( key, value) in exemplar_json {
118
- data_point_json. insert ( key, value) ;
119
- }
121
+ data_point_json. extend (
122
+ flatten_exemplar ( & data_point. exemplars , other_attributes)
123
+ . into_iter ( )
124
+ . flatten ( ) ,
125
+ ) ;
120
126
data_point_json. extend ( flatten_data_point_flags ( data_point. flags ) ) ;
121
127
if let Some ( value) = & data_point. value {
122
128
match value {
123
129
NumberDataPointValue :: AsDouble ( double_val) => {
124
130
data_point_json. insert (
125
131
"data_point_value" . to_string ( ) ,
126
- Value :: Number ( serde_json:: Number :: from_f64 ( * double_val) . unwrap ( ) ) ,
132
+ serde_json:: Number :: from_f64 ( * double_val)
133
+ . map ( Value :: Number )
134
+ . unwrap_or ( Value :: Null ) ,
127
135
) ;
128
136
}
129
137
NumberDataPointValue :: AsInt ( int_val) => {
@@ -232,17 +240,23 @@ fn flatten_histogram(
232
240
data_point
233
241
. explicit_bounds
234
242
. iter ( )
235
- . map ( |bound| Value :: Number ( serde_json:: Number :: from_f64 ( * bound) . unwrap ( ) ) )
243
+ . map ( |bound| {
244
+ serde_json:: Number :: from_f64 ( * bound)
245
+ . map ( Value :: Number )
246
+ . unwrap_or ( Value :: Null )
247
+ } )
236
248
. collect ( ) ,
237
249
) ;
238
250
data_point_json. insert (
239
251
"data_point_explicit_bounds" . to_string ( ) ,
240
252
data_point_explicit_bounds,
241
253
) ;
242
- let exemplar_json = flatten_exemplar ( & data_point. exemplars , other_attributes) ;
243
- for ( key, value) in exemplar_json {
244
- data_point_json. insert ( key. to_string ( ) , value) ;
245
- }
254
+ data_point_json. extend (
255
+ flatten_exemplar ( & data_point. exemplars , other_attributes)
256
+ . into_iter ( )
257
+ . flatten ( ) ,
258
+ ) ;
259
+
246
260
data_point_json. extend ( flatten_data_point_flags ( data_point. flags ) ) ;
247
261
insert_number_if_some ( & mut data_point_json, "min" , & data_point. min ) ;
248
262
insert_number_if_some ( & mut data_point_json, "max" , & data_point. max ) ;
@@ -332,10 +346,12 @@ fn flatten_exp_histogram(
332
346
data_point_json. insert ( format ! ( "negative_{}" , key) , value) ;
333
347
}
334
348
}
335
- let exemplar_json = flatten_exemplar ( & data_point. exemplars , other_attributes) ;
336
- for ( key, value) in exemplar_json {
337
- data_point_json. insert ( key, value) ;
338
- }
349
+ data_point_json. extend (
350
+ flatten_exemplar ( & data_point. exemplars , other_attributes)
351
+ . into_iter ( )
352
+ . flatten ( ) ,
353
+ ) ;
354
+
339
355
data_points_json. push ( data_point_json) ;
340
356
}
341
357
let mut exp_histogram_json = Map :: new ( ) ;
@@ -384,7 +400,9 @@ fn flatten_summary(
384
400
) ;
385
401
data_point_json. insert (
386
402
"data_point_sum" . to_string ( ) ,
387
- Value :: Number ( serde_json:: Number :: from_f64 ( data_point. sum ) . unwrap ( ) ) ,
403
+ serde_json:: Number :: from_f64 ( data_point. sum )
404
+ . map ( Value :: Number )
405
+ . unwrap_or ( Value :: Null ) ,
388
406
) ;
389
407
data_point_json. insert (
390
408
"data_point_quantile_values" . to_string ( ) ,
@@ -397,16 +415,15 @@ fn flatten_summary(
397
415
vec ! [
398
416
(
399
417
"quantile" ,
400
- Value :: Number (
401
- serde_json:: Number :: from_f64( quantile_value. quantile)
402
- . unwrap( ) ,
403
- ) ,
418
+ serde_json:: Number :: from_f64( quantile_value. quantile)
419
+ . map( Value :: Number )
420
+ . unwrap_or( Value :: Null ) ,
404
421
) ,
405
422
(
406
423
"value" ,
407
- Value :: Number (
408
- serde_json :: Number :: from_f64 ( quantile_value . value ) . unwrap ( ) ,
409
- ) ,
424
+ serde_json :: Number :: from_f64 ( quantile_value . value )
425
+ . map ( Value :: Number )
426
+ . unwrap_or ( Value :: Null ) ,
410
427
) ,
411
428
]
412
429
. into_iter ( )
0 commit comments