@@ -42,46 +42,51 @@ pub const OTEL_METRICS_KNOWN_FIELD_LIST: [&str; 5] = [
4242fn flatten_exemplar (
4343 exemplars : & [ Exemplar ] ,
4444 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+ }
8085 }
8186 }
82- }
83- }
84- exemplar_json
87+ exemplar_json
88+ } )
89+ . collect ( )
8590}
8691
8792/// otel metrics event has json array for number data points
@@ -113,17 +118,20 @@ fn flatten_number_data_points(
113118 data_point. time_unix_nano as i64 ,
114119 ) ) ,
115120 ) ;
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+ ) ;
120126 data_point_json. extend ( flatten_data_point_flags ( data_point. flags ) ) ;
121127 if let Some ( value) = & data_point. value {
122128 match value {
123129 NumberDataPointValue :: AsDouble ( double_val) => {
124130 data_point_json. insert (
125131 "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 ) ,
127135 ) ;
128136 }
129137 NumberDataPointValue :: AsInt ( int_val) => {
@@ -232,17 +240,23 @@ fn flatten_histogram(
232240 data_point
233241 . explicit_bounds
234242 . 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+ } )
236248 . collect ( ) ,
237249 ) ;
238250 data_point_json. insert (
239251 "data_point_explicit_bounds" . to_string ( ) ,
240252 data_point_explicit_bounds,
241253 ) ;
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+
246260 data_point_json. extend ( flatten_data_point_flags ( data_point. flags ) ) ;
247261 insert_number_if_some ( & mut data_point_json, "min" , & data_point. min ) ;
248262 insert_number_if_some ( & mut data_point_json, "max" , & data_point. max ) ;
@@ -332,10 +346,12 @@ fn flatten_exp_histogram(
332346 data_point_json. insert ( format ! ( "negative_{}" , key) , value) ;
333347 }
334348 }
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+
339355 data_points_json. push ( data_point_json) ;
340356 }
341357 let mut exp_histogram_json = Map :: new ( ) ;
@@ -384,7 +400,9 @@ fn flatten_summary(
384400 ) ;
385401 data_point_json. insert (
386402 "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 ) ,
388406 ) ;
389407 data_point_json. insert (
390408 "data_point_quantile_values" . to_string ( ) ,
@@ -397,16 +415,15 @@ fn flatten_summary(
397415 vec ! [
398416 (
399417 "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 ) ,
404421 ) ,
405422 (
406423 "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 ) ,
410427 ) ,
411428 ]
412429 . into_iter ( )
0 commit comments