Skip to content

Commit b628649

Browse files
add comments and refactor
1 parent 217e968 commit b628649

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

src/otel/logs.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use opentelemetry_proto::tonic::logs::v1::SeverityNumber;
2323
use serde_json::Map;
2424
use serde_json::Value;
2525

26+
use super::otel_utils::add_other_attributes_if_not_empty;
2627
use super::otel_utils::collect_json_from_values;
2728
use super::otel_utils::convert_epoch_nano_to_timestamp;
28-
use super::otel_utils::fetch_attributes_string;
2929
use super::otel_utils::insert_attributes;
3030
use super::otel_utils::merge_attributes_in_json;
3131

@@ -107,13 +107,7 @@ pub fn flatten_log_record(log_record: &LogRecord) -> Map<String, Value> {
107107
);
108108

109109
// Add the `other_attributes` to the log record json
110-
if !other_attributes.is_empty() {
111-
let other_attributes = fetch_attributes_string(&other_attributes);
112-
log_record_json.insert(
113-
"other_attributes".to_string(),
114-
Value::String(other_attributes),
115-
);
116-
}
110+
add_other_attributes_if_not_empty(&mut log_record_json, &other_attributes);
117111

118112
log_record_json
119113
}

src/otel/metrics.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use opentelemetry_proto::tonic::metrics::v1::{
2424
use serde_json::{Map, Value};
2525

2626
use super::otel_utils::{
27-
convert_epoch_nano_to_timestamp, fetch_attributes_string, insert_attributes,
27+
add_other_attributes_if_not_empty, convert_epoch_nano_to_timestamp, insert_attributes,
2828
insert_number_if_some, merge_attributes_in_json,
2929
};
3030

@@ -83,13 +83,7 @@ fn flatten_exemplar(exemplars: &[Exemplar]) -> Vec<Map<String, Value>> {
8383
}
8484
}
8585
}
86-
if !other_attributes.is_empty() {
87-
let other_attributes = fetch_attributes_string(&other_attributes);
88-
exemplar_json.insert(
89-
"other_attributes".to_string(),
90-
Value::String(other_attributes),
91-
);
92-
}
86+
add_other_attributes_if_not_empty(&mut exemplar_json, &other_attributes);
9387
exemplar_json
9488
})
9589
.collect()
@@ -131,11 +125,8 @@ fn flatten_number_data_points(data_points: &[NumberDataPoint]) -> Vec<Map<String
131125
data_point_json.insert(key, value);
132126
}
133127
}
134-
} else if !other_attributes.is_empty() {
135-
data_point_json.insert(
136-
"other_attributes".to_string(),
137-
Value::String(fetch_attributes_string(&other_attributes)),
138-
);
128+
} else {
129+
add_other_attributes_if_not_empty(&mut data_point_json, &other_attributes);
139130
}
140131

141132
data_point_json.extend(flatten_data_point_flags(data_point.flags));
@@ -270,11 +261,8 @@ fn flatten_histogram(histogram: &Histogram) -> Vec<Map<String, Value>> {
270261
data_point_json.insert(key, value);
271262
}
272263
}
273-
} else if !other_attributes.is_empty() {
274-
data_point_json.insert(
275-
"other_attributes".to_string(),
276-
Value::String(fetch_attributes_string(&other_attributes)),
277-
);
264+
} else {
265+
add_other_attributes_if_not_empty(&mut data_point_json, &other_attributes);
278266
}
279267

280268
data_point_json.extend(flatten_data_point_flags(data_point.flags));
@@ -372,11 +360,8 @@ fn flatten_exp_histogram(exp_histogram: &ExponentialHistogram) -> Vec<Map<String
372360
data_point_json.insert(key, value);
373361
}
374362
}
375-
} else if !other_attributes.is_empty() {
376-
data_point_json.insert(
377-
"other_attributes".to_string(),
378-
Value::String(fetch_attributes_string(&other_attributes)),
379-
);
363+
} else {
364+
add_other_attributes_if_not_empty(&mut data_point_json, &other_attributes);
380365
}
381366

382367
data_points_json.push(data_point_json);
@@ -460,12 +445,7 @@ fn flatten_summary(summary: &Summary) -> Vec<Map<String, Value>> {
460445
),
461446
);
462447

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-
}
448+
add_other_attributes_if_not_empty(&mut data_point_json, &other_attributes);
469449

470450
data_points_json.push(data_point_json);
471451
}

src/otel/otel_utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use opentelemetry_proto::tonic::common::v1::{
2222
};
2323
use serde_json::{Map, Value};
2424

25+
/// Prefixes of attribute keys that should be preserved as individual fields in flattened output.
26+
/// Other attributes will be collected in a separate JSON object under `other_attributes`.
2527
const KNOWN_ATTRIBUTES_PREFIX: [&str; 6] = ["http", "url", "service", "os", "host", "telemetry"];
2628

2729
// Value can be one of types - String, Bool, Int, Double, ArrayValue, AnyValue, KeyValueList, Byte
@@ -73,6 +75,9 @@ pub fn collect_json_from_value(key: &String, value: OtelValue) -> Map<String, Va
7375
value_json
7476
}
7577

78+
/// Recursively converts an ArrayValue into a JSON Value
79+
/// This handles nested array values and key-value lists by recursively
80+
/// converting them to JSON
7681
fn collect_json_from_array_value(array_value: ArrayValue) -> Value {
7782
let mut json_array = Vec::new();
7883
for value in array_value.values {
@@ -107,6 +112,9 @@ fn collect_json_from_array_value(array_value: ArrayValue) -> Value {
107112
Value::Array(json_array)
108113
}
109114

115+
/// Recursively converts an OpenTelemetry KeyValueList into a JSON Map
116+
/// The function iterates through the key-value pairs in the list
117+
/// and collects their JSON representations into a single Map
110118
fn collect_json_from_key_value_list(key_value_list: KeyValueList) -> Map<String, Value> {
111119
let mut kv_list_json: Map<String, Value> = Map::new();
112120
for key_value in key_value_list.values {
@@ -273,3 +281,16 @@ pub fn fetch_attributes_string(attributes: &Map<String, Value>) -> String {
273281
}
274282
}
275283
}
284+
285+
/// add `other_attributes` to the JSON object
286+
/// if `other_attributes` is not empty
287+
/// and return the JSON object
288+
pub fn add_other_attributes_if_not_empty(
289+
json: &mut Map<String, Value>,
290+
other_attributes: &Map<String, Value>,
291+
) {
292+
if !other_attributes.is_empty() {
293+
let attrs_str = fetch_attributes_string(other_attributes);
294+
json.insert("other_attributes".to_string(), Value::String(attrs_str));
295+
}
296+
}

src/otel/traces.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use opentelemetry_proto::tonic::trace::v1::Status;
2424
use opentelemetry_proto::tonic::trace::v1::TracesData;
2525
use serde_json::{Map, Value};
2626

27+
use super::otel_utils::add_other_attributes_if_not_empty;
2728
use super::otel_utils::convert_epoch_nano_to_timestamp;
2829
use super::otel_utils::fetch_attributes_from_json;
29-
use super::otel_utils::fetch_attributes_string;
3030
use super::otel_utils::insert_attributes;
3131
use super::otel_utils::merge_attributes_in_json;
3232

@@ -159,13 +159,7 @@ fn flatten_events(events: &[Event]) -> Vec<Map<String, Value>> {
159159
Value::Number(event.dropped_attributes_count.into()),
160160
);
161161

162-
if !other_attributes.is_empty() {
163-
let other_attributes = fetch_attributes_string(&other_attributes);
164-
event_json.insert(
165-
"other_attributes".to_string(),
166-
Value::String(other_attributes),
167-
);
168-
}
162+
add_other_attributes_if_not_empty(&mut event_json, &other_attributes);
169163
event_json
170164
})
171165
.collect()
@@ -195,13 +189,7 @@ fn flatten_links(links: &[Link]) -> Vec<Map<String, Value>> {
195189
Value::Number(link.dropped_attributes_count.into()),
196190
);
197191

198-
if !other_attributes.is_empty() {
199-
let other_attributes = fetch_attributes_string(&other_attributes);
200-
link_json.insert(
201-
"other_attributes".to_string(),
202-
Value::String(other_attributes),
203-
);
204-
}
192+
add_other_attributes_if_not_empty(&mut link_json, &other_attributes);
205193
link_json
206194
})
207195
.collect()
@@ -354,8 +342,7 @@ fn flatten_span_record(span_record: &Span) -> Vec<Map<String, Value>> {
354342
for (key, value) in &links_other_attributes {
355343
other_attributes.insert(key.clone(), value.clone());
356344
}
357-
let attrs_str = fetch_attributes_string(&other_attributes);
358-
span_record_json.insert("other_attributes".to_string(), Value::String(attrs_str));
345+
add_other_attributes_if_not_empty(&mut span_record_json, &other_attributes);
359346
}
360347
span_record_json.insert(
361348
"span_dropped_links_count".to_string(),

0 commit comments

Comments
 (0)