@@ -23,21 +23,26 @@ use opentelemetry_proto::tonic::common::v1::{
2323use serde_json:: { Map , Value } ;
2424
2525// Value can be one of types - String, Bool, Int, Double, ArrayValue, AnyValue, KeyValueList, Byte
26- pub fn collect_json_from_value ( key : & String , value : OtelValue ) -> Map < String , Value > {
26+ pub fn collect_json_from_value ( key : & str , value : OtelValue ) -> Map < String , Value > {
2727 let mut value_json: Map < String , Value > = Map :: new ( ) ;
28+ insert_json_from_value ( & mut value_json, key, value) ;
29+ value_json
30+ }
31+
32+ fn insert_json_from_value ( map : & mut Map < String , Value > , key : & str , value : OtelValue ) {
2833 match value {
2934 OtelValue :: StringValue ( str_val) => {
30- value_json . insert ( key. to_string ( ) , Value :: String ( str_val) ) ;
35+ map . insert ( key. to_string ( ) , Value :: String ( str_val) ) ;
3136 }
3237 OtelValue :: BoolValue ( bool_val) => {
33- value_json . insert ( key. to_string ( ) , Value :: Bool ( bool_val) ) ;
38+ map . insert ( key. to_string ( ) , Value :: Bool ( bool_val) ) ;
3439 }
3540 OtelValue :: IntValue ( int_val) => {
36- value_json . insert ( key. to_string ( ) , Value :: String ( int_val. to_string ( ) ) ) ;
41+ map . insert ( key. to_string ( ) , Value :: String ( int_val. to_string ( ) ) ) ;
3742 }
3843 OtelValue :: DoubleValue ( double_val) => {
3944 if let Some ( number) = serde_json:: Number :: from_f64 ( double_val) {
40- value_json . insert ( key. to_string ( ) , Value :: Number ( number) ) ;
45+ map . insert ( key. to_string ( ) , Value :: Number ( number) ) ;
4146 }
4247 }
4348 OtelValue :: ArrayValue ( array_val) => {
@@ -51,17 +56,15 @@ pub fn collect_json_from_value(key: &String, value: OtelValue) -> Map<String, Va
5156 }
5257 } ;
5358 // Insert the array into the result map
54- value_json . insert ( key. to_string ( ) , Value :: String ( json_array_string) ) ;
59+ map . insert ( key. to_string ( ) , Value :: String ( json_array_string) ) ;
5560 }
5661 OtelValue :: KvlistValue ( kv_list_val) => {
5762 // Create a JSON object to store the key-value list
5863 let kv_object = collect_json_from_key_value_list ( kv_list_val) ;
59- for ( key, value) in kv_object. iter ( ) {
60- value_json. insert ( key. clone ( ) , value. clone ( ) ) ;
61- }
64+ map. extend ( kv_object) ;
6265 }
6366 OtelValue :: BytesValue ( bytes_val) => {
64- value_json . insert (
67+ map . insert (
6568 key. to_string ( ) ,
6669 Value :: String ( String :: from_utf8_lossy ( & bytes_val) . to_string ( ) ) ,
6770 ) ;
@@ -72,15 +75,13 @@ pub fn collect_json_from_value(key: &String, value: OtelValue) -> Map<String, Va
7275 ) ;
7376 }
7477 }
75-
76- value_json
7778}
7879
7980/// Recursively converts an ArrayValue into a JSON Value
8081/// This handles nested array values and key-value lists by recursively
8182/// converting them to JSON
8283fn collect_json_from_array_value ( array_value : & ArrayValue ) -> Value {
83- let mut json_array = Vec :: new ( ) ;
84+ let mut json_array = Vec :: with_capacity ( array_value . values . len ( ) ) ;
8485 for value in & array_value. values {
8586 if let Some ( val) = & value. value {
8687 match val {
@@ -122,12 +123,11 @@ fn collect_json_from_array_value(array_value: &ArrayValue) -> Value {
122123/// The function iterates through the key-value pairs in the list
123124/// and collects their JSON representations into a single Map
124125fn collect_json_from_key_value_list ( key_value_list : KeyValueList ) -> Map < String , Value > {
125- let mut kv_list_json: Map < String , Value > = Map :: new ( ) ;
126+ let mut kv_list_json: Map < String , Value > = Map :: with_capacity ( key_value_list . values . len ( ) ) ;
126127 for key_value in key_value_list. values {
127128 if let Some ( val) = key_value. value {
128129 if let Some ( val) = val. value {
129- let json_value = collect_json_from_value ( & key_value. key , val) ;
130- kv_list_json. extend ( json_value) ;
130+ insert_json_from_value ( & mut kv_list_json, & key_value. key , val) ;
131131 } else {
132132 tracing:: warn!( "Key '{}' has no value in key-value list" , key_value. key) ;
133133 }
@@ -136,37 +136,38 @@ fn collect_json_from_key_value_list(key_value_list: KeyValueList) -> Map<String,
136136 kv_list_json
137137}
138138
139- pub fn collect_json_from_anyvalue ( key : & String , value : AnyValue ) -> Map < String , Value > {
139+ pub fn collect_json_from_anyvalue ( key : & str , value : AnyValue ) -> Map < String , Value > {
140140 collect_json_from_value ( key, value. value . unwrap ( ) )
141141}
142142
143143//traverse through Value by calling function ollect_json_from_any_value
144- pub fn collect_json_from_values ( values : & Option < AnyValue > , key : & String ) -> Map < String , Value > {
145- let mut value_json: Map < String , Value > = Map :: new ( ) ;
144+ pub fn collect_json_from_values ( values : & Option < AnyValue > , key : & str ) -> Map < String , Value > {
145+ let mut value_json: Map < String , Value > = Map :: with_capacity ( 1 ) ;
146146
147147 for value in values. iter ( ) {
148- value_json = collect_json_from_anyvalue ( key, value. clone ( ) ) ;
148+ if let Some ( value) = value. value . clone ( ) {
149+ insert_json_from_value ( & mut value_json, key, value) ;
150+ }
149151 }
150152
151153 value_json
152154}
153155
154156pub fn value_to_string ( value : serde_json:: Value ) -> String {
155- match value. clone ( ) {
157+ match value {
156158 e @ Value :: Number ( _) | e @ Value :: Bool ( _) => e. to_string ( ) ,
157159 Value :: String ( s) => s,
158160 _ => "" . to_string ( ) ,
159161 }
160162}
161163
162164pub fn flatten_attributes ( attributes : & [ KeyValue ] ) -> Map < String , Value > {
163- let mut attributes_json: Map < String , Value > = Map :: new ( ) ;
165+ let mut attributes_json: Map < String , Value > = Map :: with_capacity ( attributes . len ( ) ) ;
164166 for attribute in attributes {
165- let key = & attribute. key ;
166- let value = & attribute. value ;
167- let value_json = collect_json_from_values ( value, & key. to_string ( ) ) ;
168- for ( attr_key, attr_val) in & value_json {
169- attributes_json. insert ( attr_key. clone ( ) , attr_val. clone ( ) ) ;
167+ if let Some ( value) = & attribute. value
168+ && let Some ( value) = value. value . clone ( )
169+ {
170+ insert_json_from_value ( & mut attributes_json, & attribute. key , value) ;
170171 }
171172 }
172173 attributes_json
@@ -193,9 +194,12 @@ pub fn insert_bool_if_some(map: &mut Map<String, Value>, key: &str, option: &Opt
193194}
194195
195196pub fn insert_attributes ( map : & mut Map < String , Value > , attributes : & [ KeyValue ] ) {
196- let attributes_json = flatten_attributes ( attributes) ;
197- for ( key, value) in attributes_json {
198- map. insert ( key, value) ;
197+ for attribute in attributes {
198+ if let Some ( value) = & attribute. value
199+ && let Some ( value) = value. value . clone ( )
200+ {
201+ insert_json_from_value ( map, & attribute. key , value) ;
202+ }
199203 }
200204}
201205
0 commit comments