Skip to content

Commit 217e968

Browse files
refactor and store other attributes as map
1 parent 6a0fc57 commit 217e968

File tree

3 files changed

+68
-44
lines changed

3 files changed

+68
-44
lines changed

src/otel/metrics.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn flatten_number_data_points(data_points: &[NumberDataPoint]) -> Vec<Map<String
131131
data_point_json.insert(key, value);
132132
}
133133
}
134-
} else {
134+
} else if !other_attributes.is_empty() {
135135
data_point_json.insert(
136136
"other_attributes".to_string(),
137137
Value::String(fetch_attributes_string(&other_attributes)),
@@ -270,7 +270,7 @@ fn flatten_histogram(histogram: &Histogram) -> Vec<Map<String, Value>> {
270270
data_point_json.insert(key, value);
271271
}
272272
}
273-
} else {
273+
} else if !other_attributes.is_empty() {
274274
data_point_json.insert(
275275
"other_attributes".to_string(),
276276
Value::String(fetch_attributes_string(&other_attributes)),
@@ -372,7 +372,7 @@ fn flatten_exp_histogram(exp_histogram: &ExponentialHistogram) -> Vec<Map<String
372372
data_point_json.insert(key, value);
373373
}
374374
}
375-
} else {
375+
} else if !other_attributes.is_empty() {
376376
data_point_json.insert(
377377
"other_attributes".to_string(),
378378
Value::String(fetch_attributes_string(&other_attributes)),
@@ -459,6 +459,14 @@ fn flatten_summary(summary: &Summary) -> Vec<Map<String, Value>> {
459459
.collect(),
460460
),
461461
);
462+
463+
if !other_attributes.is_empty() {
464+
data_point_json.insert(
465+
"other_attributes".to_string(),
466+
Value::String(fetch_attributes_string(&other_attributes)),
467+
);
468+
}
469+
462470
data_points_json.push(data_point_json);
463471
}
464472
data_points_json
@@ -548,7 +556,7 @@ pub fn flatten_otel_metrics(message: MetricsData) -> Vec<Value> {
548556
let mut vec_scope_metrics_json = Vec::new();
549557
for scope_metric in &record.scope_metrics {
550558
let mut scope_metrics_json = Map::new();
551-
let mut other_attributes = Map::new();
559+
let mut scope_other_attributes = Map::new();
552560
for metrics_record in &scope_metric.metrics {
553561
vec_scope_metrics_json.extend(flatten_metrics_record(metrics_record));
554562
}
@@ -562,7 +570,7 @@ pub fn flatten_otel_metrics(message: MetricsData) -> Vec<Value> {
562570
insert_attributes(
563571
&mut scope_metrics_json,
564572
&scope.attributes,
565-
&mut other_attributes,
573+
&mut scope_other_attributes,
566574
);
567575
scope_metrics_json.insert(
568576
"scope_dropped_attributes_count".to_string(),
@@ -579,7 +587,7 @@ pub fn flatten_otel_metrics(message: MetricsData) -> Vec<Value> {
579587
scope_metric_json.insert(key.clone(), value.clone());
580588
}
581589
}
582-
merge_attributes_in_json(other_attributes, &mut vec_scope_metrics_json);
590+
merge_attributes_in_json(scope_other_attributes, &mut vec_scope_metrics_json);
583591
}
584592
resource_metrics_json.insert(
585593
"resource_metrics_schema_url".to_string(),

src/otel/otel_utils.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
207210
pub 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
245267
pub fn fetch_attributes_string(attributes: &Map<String, Value>) -> String {
246268
match serde_json::to_string(attributes) {
247269
Ok(s) => s,

src/otel/traces.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -343,25 +343,19 @@ fn flatten_span_record(span_record: &Span) -> Vec<Map<String, Value>> {
343343
// fetch all other_attributes from the links_json
344344
let links_other_attributes = fetch_attributes_from_json(&links_json);
345345
span_records_json.extend(links_json);
346-
if !other_attributes.is_empty() {
347-
let mut other_attributes = fetch_attributes_string(&other_attributes);
348-
if !events_other_attributes.is_empty() {
349-
other_attributes.push_str(&events_other_attributes);
346+
// merge all other_attributes from the events_json and links_json
347+
if !other_attributes.is_empty()
348+
|| !events_other_attributes.is_empty()
349+
|| !links_other_attributes.is_empty()
350+
{
351+
for (key, value) in &events_other_attributes {
352+
other_attributes.insert(key.clone(), value.clone());
350353
}
351-
if !links_other_attributes.is_empty() {
352-
other_attributes.push_str(&links_other_attributes);
354+
for (key, value) in &links_other_attributes {
355+
other_attributes.insert(key.clone(), value.clone());
353356
}
354-
span_record_json.insert(
355-
"other_attributes".to_string(),
356-
Value::String(other_attributes),
357-
);
358-
} else {
359-
span_record_json.insert(
360-
"other_attributes".to_string(),
361-
Value::String(format!(
362-
"{events_other_attributes} {links_other_attributes}"
363-
)),
364-
);
357+
let attrs_str = fetch_attributes_string(&other_attributes);
358+
span_record_json.insert("other_attributes".to_string(), Value::String(attrs_str));
365359
}
366360
span_record_json.insert(
367361
"span_dropped_links_count".to_string(),

0 commit comments

Comments
 (0)