@@ -23,9 +23,11 @@ use opentelemetry_proto::tonic::logs::v1::SeverityNumber;
23
23
use serde_json:: Map ;
24
24
use serde_json:: Value ;
25
25
26
+ use super :: otel_utils:: add_other_attributes_if_not_empty;
26
27
use super :: otel_utils:: collect_json_from_values;
27
28
use super :: otel_utils:: convert_epoch_nano_to_timestamp;
28
29
use super :: otel_utils:: insert_attributes;
30
+ use super :: otel_utils:: merge_attributes_in_json;
29
31
30
32
pub const OTEL_LOG_KNOWN_FIELD_LIST : [ & str ; 6 ] = [
31
33
"time_unix_nano" ,
@@ -58,6 +60,7 @@ fn flatten_severity(severity_number: i32) -> Map<String, Value> {
58
60
/// this function is called recursively for each log record object in the otel logs
59
61
pub fn flatten_log_record ( log_record : & LogRecord ) -> Map < String , Value > {
60
62
let mut log_record_json: Map < String , Value > = Map :: new ( ) ;
63
+ let mut other_attributes = Map :: new ( ) ;
61
64
log_record_json. insert (
62
65
"time_unix_nano" . to_string ( ) ,
63
66
Value :: String ( convert_epoch_nano_to_timestamp (
@@ -80,7 +83,11 @@ pub fn flatten_log_record(log_record: &LogRecord) -> Map<String, Value> {
80
83
log_record_json. insert ( key. to_owned ( ) , body_json[ key] . to_owned ( ) ) ;
81
84
}
82
85
}
83
- insert_attributes ( & mut log_record_json, & log_record. attributes ) ;
86
+ insert_attributes (
87
+ & mut log_record_json,
88
+ & log_record. attributes ,
89
+ & mut other_attributes,
90
+ ) ;
84
91
log_record_json. insert (
85
92
"log_record_dropped_attributes_count" . to_string ( ) ,
86
93
Value :: Number ( log_record. dropped_attributes_count . into ( ) ) ,
@@ -99,6 +106,9 @@ pub fn flatten_log_record(log_record: &LogRecord) -> Map<String, Value> {
99
106
Value :: String ( hex:: encode ( & log_record. trace_id ) ) ,
100
107
) ;
101
108
109
+ // Add the `other_attributes` to the log record json
110
+ add_other_attributes_if_not_empty ( & mut log_record_json, & other_attributes) ;
111
+
102
112
log_record_json
103
113
}
104
114
@@ -107,14 +117,18 @@ pub fn flatten_log_record(log_record: &LogRecord) -> Map<String, Value> {
107
117
fn flatten_scope_log ( scope_log : & ScopeLogs ) -> Vec < Map < String , Value > > {
108
118
let mut vec_scope_log_json = Vec :: new ( ) ;
109
119
let mut scope_log_json = Map :: new ( ) ;
110
-
120
+ let mut other_attributes = Map :: new ( ) ;
111
121
if let Some ( scope) = & scope_log. scope {
112
122
scope_log_json. insert ( "scope_name" . to_string ( ) , Value :: String ( scope. name . clone ( ) ) ) ;
113
123
scope_log_json. insert (
114
124
"scope_version" . to_string ( ) ,
115
125
Value :: String ( scope. version . clone ( ) ) ,
116
126
) ;
117
- insert_attributes ( & mut scope_log_json, & scope. attributes ) ;
127
+ insert_attributes (
128
+ & mut scope_log_json,
129
+ & scope. attributes ,
130
+ & mut other_attributes,
131
+ ) ;
118
132
scope_log_json. insert (
119
133
"scope_dropped_attributes_count" . to_string ( ) ,
120
134
Value :: Number ( scope. dropped_attributes_count . into ( ) ) ,
@@ -132,18 +146,26 @@ fn flatten_scope_log(scope_log: &ScopeLogs) -> Vec<Map<String, Value>> {
132
146
vec_scope_log_json. push ( combined_json) ;
133
147
}
134
148
149
+ // Add the `other_attributes` to the scope log json
150
+ merge_attributes_in_json ( other_attributes, & mut vec_scope_log_json) ;
151
+
135
152
vec_scope_log_json
136
153
}
137
154
138
155
/// this function performs the custom flattening of the otel logs
139
156
/// and returns a `Vec` of `Value::Object` of the flattened json
140
157
pub fn flatten_otel_logs ( message : & LogsData ) -> Vec < Value > {
141
158
let mut vec_otel_json = Vec :: new ( ) ;
159
+
142
160
for record in & message. resource_logs {
143
161
let mut resource_log_json = Map :: new ( ) ;
144
-
162
+ let mut other_attributes = Map :: new ( ) ;
145
163
if let Some ( resource) = & record. resource {
146
- insert_attributes ( & mut resource_log_json, & resource. attributes ) ;
164
+ insert_attributes (
165
+ & mut resource_log_json,
166
+ & resource. attributes ,
167
+ & mut other_attributes,
168
+ ) ;
147
169
resource_log_json. insert (
148
170
"resource_dropped_attributes_count" . to_string ( ) ,
149
171
Value :: Number ( resource. dropped_attributes_count . into ( ) ) ,
@@ -163,8 +185,10 @@ pub fn flatten_otel_logs(message: &LogsData) -> Vec<Value> {
163
185
resource_logs_json. extend ( resource_log_json. clone ( ) ) ;
164
186
}
165
187
188
+ // Add the `other_attributes` to the resource log json
189
+ merge_attributes_in_json ( other_attributes, & mut vec_resource_logs_json) ;
190
+
166
191
vec_otel_json. extend ( vec_resource_logs_json) ;
167
192
}
168
-
169
193
vec_otel_json. into_iter ( ) . map ( Value :: Object ) . collect ( )
170
194
}
0 commit comments