@@ -204,44 +204,66 @@ pub fn convert_epoch_nano_to_timestamp(epoch_ns: i64) -> String {
204204 dt. format ( "%Y-%m-%dT%H:%M:%S%.6fZ" ) . to_string ( )
205205}
206206
207+ /// fetch `other_attributes` from array of JSON objects
208+ /// merge them with the provided attributes
209+ /// and return the merged array of JSON object
207210pub fn merge_attributes_in_json (
208211 attributes : Map < String , Value > ,
209212 vec_json : & mut Vec < Map < String , Value > > ,
210213) {
211214 if !attributes. is_empty ( ) {
212- let attributes = fetch_attributes_string ( & attributes) ;
213215 for json in vec_json {
214- if json. contains_key ( "other_attributes" ) {
215- let other_attributes = json. get_mut ( "other_attributes" ) . unwrap ( ) ;
216- let other_attributes = other_attributes. as_str ( ) . unwrap_or_default ( ) ;
217- // append the other_attributes to the scope log json
218- let other_attributes = format ! ( "{other_attributes} {attributes}" ) ;
219- json. insert (
220- "other_attributes" . to_string ( ) ,
221- Value :: String ( other_attributes) ,
222- ) ;
223- } else {
224- json. insert (
225- "other_attributes" . to_string ( ) ,
226- Value :: String ( attributes. clone ( ) ) ,
227- ) ;
216+ if let Some ( other_attrs) = json. get ( "other_attributes" ) {
217+ if let Value :: String ( attrs_str) = other_attrs {
218+ if let Ok ( mut existing_attrs) =
219+ serde_json:: from_str :: < Map < String , Value > > ( attrs_str)
220+ {
221+ for ( key, value) in attributes. clone ( ) {
222+ existing_attrs. insert ( key, value) ;
223+ }
224+ if let Ok ( merged_str) = serde_json:: to_string ( & existing_attrs) {
225+ json. insert ( "other_attributes" . to_string ( ) , Value :: String ( merged_str) ) ;
226+ }
227+ } else if let Ok ( attrs_str) = serde_json:: to_string ( & attributes) {
228+ json. insert ( "other_attributes" . to_string ( ) , Value :: String ( attrs_str) ) ;
229+ }
230+ } else if let Value :: Object ( existing_attrs) = other_attrs {
231+ let mut merged_attrs = existing_attrs. clone ( ) ;
232+ for ( key, value) in attributes. clone ( ) {
233+ merged_attrs. insert ( key, value) ;
234+ }
235+ if let Ok ( merged_str) = serde_json:: to_string ( & merged_attrs) {
236+ json. insert ( "other_attributes" . to_string ( ) , Value :: String ( merged_str) ) ;
237+ }
238+ }
239+ } else if let Ok ( attrs_str) = serde_json:: to_string ( & attributes) {
240+ json. insert ( "other_attributes" . to_string ( ) , Value :: String ( attrs_str) ) ;
228241 }
229242 }
230243 }
231244}
232245
233- pub fn fetch_attributes_from_json ( json_arr : & Vec < Map < String , Value > > ) -> String {
234- let mut combined_attributes = String :: default ( ) ;
246+ /// fetch `other_attributes` from array of JSON objects
247+ /// and merge them into a single map
248+ /// and return the merged map
249+ pub fn fetch_attributes_from_json ( json_arr : & Vec < Map < String , Value > > ) -> Map < String , Value > {
250+ let mut merged_attributes = Map :: new ( ) ;
251+
235252 for json in json_arr {
236- if let Some ( other_attributes) = json. get ( "other_attributes" ) {
237- if let Some ( other_attributes) = other_attributes. as_str ( ) {
238- combined_attributes. push_str ( other_attributes) ;
253+ if let Some ( Value :: String ( attrs_str) ) = json. get ( "other_attributes" ) {
254+ if let Ok ( attrs) = serde_json:: from_str :: < Map < String , Value > > ( attrs_str) {
255+ for ( key, value) in attrs {
256+ merged_attributes. insert ( key, value) ;
257+ }
239258 }
240259 }
241260 }
242- combined_attributes
261+ merged_attributes
243262}
244263
264+ /// convert attributes map to a string
265+ /// and return the string
266+ /// if serialisation fails, return an empty string
245267pub fn fetch_attributes_string ( attributes : & Map < String , Value > ) -> String {
246268 match serde_json:: to_string ( attributes) {
247269 Ok ( s) => s,
0 commit comments