Skip to content

Commit 7c37afa

Browse files
fix for otel logs flattening
1 parent 16bf9b3 commit 7c37afa

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

server/src/handlers/http/otel.rs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,27 @@ pub fn flatten_otel_logs(body: &Bytes) -> Vec<BTreeMap<String, Value>> {
131131
let mut vec_otel_json: Vec<BTreeMap<String, Value>> = Vec::new();
132132
let body_str = std::str::from_utf8(body).unwrap();
133133
let message: LogsData = serde_json::from_str(body_str).unwrap();
134-
for records in message.resource_logs.iter() {
134+
135+
if let Some(records) = message.resource_logs.as_ref() {
136+
let mut vec_resource_logs_json: Vec<BTreeMap<String, Value>> = Vec::new();
135137
for record in records.iter() {
136-
let mut otel_json: BTreeMap<String, Value> = BTreeMap::new();
137-
for resource in record.resource.iter() {
138-
let attributes = &resource.attributes;
139-
for attributes in attributes.iter() {
138+
let mut resource_log_json: BTreeMap<String, Value> = BTreeMap::new();
139+
140+
if let Some(resource) = record.resource.as_ref() {
141+
if let Some(attributes) = resource.attributes.as_ref() {
140142
for attribute in attributes {
141143
let key = &attribute.key;
142144
let value = &attribute.value;
143145
let value_json =
144146
collect_json_from_values(value, &format!("resource_{}", key));
145147
for key in value_json.keys() {
146-
otel_json.insert(key.to_owned(), value_json[key].to_owned());
148+
resource_log_json.insert(key.to_owned(), value_json[key].to_owned());
147149
}
148150
}
149151
}
152+
150153
if resource.dropped_attributes_count.is_some() {
151-
otel_json.insert(
154+
resource_log_json.insert(
152155
"resource_dropped_attributes_count".to_string(),
153156
Value::Number(serde_json::Number::from(
154157
resource.dropped_attributes_count.unwrap(),
@@ -157,48 +160,59 @@ pub fn flatten_otel_logs(body: &Bytes) -> Vec<BTreeMap<String, Value>> {
157160
}
158161
}
159162

160-
for scope_logs in record.scope_logs.iter() {
163+
if let Some(scope_logs) = record.scope_logs.as_ref() {
164+
let mut vec_scope_log_json: Vec<BTreeMap<String, Value>> = Vec::new();
161165
for scope_log in scope_logs.iter() {
162-
for instrumentation_scope in scope_log.scope.iter() {
166+
let mut scope_log_json: BTreeMap<String, Value> = BTreeMap::new();
167+
if scope_log.scope.is_some() {
168+
let instrumentation_scope = scope_log.scope.as_ref().unwrap();
163169
if instrumentation_scope.name.is_some() {
164-
otel_json.insert(
170+
scope_log_json.insert(
165171
"instrumentation_scope_name".to_string(),
166172
Value::String(
167173
instrumentation_scope.name.as_ref().unwrap().to_string(),
168174
),
169175
);
170176
}
171177
if instrumentation_scope.version.is_some() {
172-
otel_json.insert(
178+
scope_log_json.insert(
173179
"instrumentation_scope_version".to_string(),
174180
Value::String(
175181
instrumentation_scope.version.as_ref().unwrap().to_string(),
176182
),
177183
);
178184
}
179-
let attributes = &instrumentation_scope.attributes;
180-
for attributes in attributes.iter() {
181-
for attribute in attributes {
185+
186+
if let Some(attributes) = instrumentation_scope.attributes.as_ref() {
187+
for attribute in attributes.iter() {
182188
let key = &attribute.key;
183189
let value = &attribute.value;
184190
let value_json = collect_json_from_values(
185191
value,
186192
&format!("instrumentation_scope_{}", key),
187193
);
188194
for key in value_json.keys() {
189-
otel_json.insert(key.to_owned(), value_json[key].to_owned());
195+
scope_log_json
196+
.insert(key.to_owned(), value_json[key].to_owned());
190197
}
191198
}
192199
}
200+
193201
if instrumentation_scope.dropped_attributes_count.is_some() {
194-
otel_json.insert(
202+
scope_log_json.insert(
195203
"instrumentation_scope_dropped_attributes_count".to_string(),
196204
Value::Number(serde_json::Number::from(
197205
instrumentation_scope.dropped_attributes_count.unwrap(),
198206
)),
199207
);
200208
}
201209
}
210+
if scope_log.schema_url.is_some() {
211+
scope_log_json.insert(
212+
"scope_log_schema_url".to_string(),
213+
Value::String(scope_log.schema_url.as_ref().unwrap().to_string()),
214+
);
215+
}
202216

203217
for log_record in scope_log.log_records.iter() {
204218
let mut log_record_json: BTreeMap<String, Value> = BTreeMap::new();
@@ -254,7 +268,7 @@ pub fn flatten_otel_logs(body: &Bytes) -> Vec<BTreeMap<String, Value>> {
254268
}
255269
}
256270

257-
for attributes in log_record.attributes.iter() {
271+
if let Some(attributes) = log_record.attributes.as_ref() {
258272
for attribute in attributes {
259273
let key = &attribute.key;
260274
let value = &attribute.value;
@@ -302,25 +316,31 @@ pub fn flatten_otel_logs(body: &Bytes) -> Vec<BTreeMap<String, Value>> {
302316
);
303317
}
304318
for key in log_record_json.keys() {
305-
otel_json.insert(key.to_owned(), log_record_json[key].to_owned());
319+
scope_log_json.insert(key.to_owned(), log_record_json[key].to_owned());
306320
}
307-
vec_otel_json.push(otel_json.clone());
308-
}
309-
310-
if scope_log.schema_url.is_some() {
311-
otel_json.insert(
312-
"scope_log_schema_url".to_string(),
313-
Value::String(scope_log.schema_url.as_ref().unwrap().to_string()),
314-
);
321+
vec_scope_log_json.push(scope_log_json.clone());
315322
}
316323
}
324+
for scope_log_json in vec_scope_log_json.iter() {
325+
vec_resource_logs_json.push(scope_log_json.clone());
326+
}
317327
}
318328
if record.schema_url.is_some() {
319-
otel_json.insert(
320-
"resource_schema_url".to_string(),
329+
resource_log_json.insert(
330+
"schema_url".to_string(),
321331
Value::String(record.schema_url.as_ref().unwrap().to_string()),
322332
);
323333
}
334+
335+
for resource_logs_json in vec_resource_logs_json.iter_mut() {
336+
for key in resource_log_json.keys() {
337+
resource_logs_json.insert(key.to_owned(), resource_log_json[key].to_owned());
338+
}
339+
}
340+
}
341+
342+
for resource_logs_json in vec_resource_logs_json.iter() {
343+
vec_otel_json.push(resource_logs_json.clone());
324344
}
325345
}
326346
vec_otel_json

0 commit comments

Comments
 (0)