Skip to content

Commit 52da33f

Browse files
test(otel): add unit tests for empty events/links flattening (#1198) (#1584)
Document the expected behaviour of flatten_events and flatten_links when called with empty slices, and the fall-back logic inside flatten_span_record that the PR #1196 fix relies on: - test_flatten_events_empty_slice: flatten_events(&[]) returns [] - test_flatten_links_empty_slice: flatten_links(&[]) returns [] - test_flatten_span_record_events_only_no_links: span with events but no links produces one record per event, each enriched with span fields - test_flatten_span_record_links_only_no_events: span with links but no events produces one record per link, each enriched with span fields Co-authored-by: Nikhil Sinha <131262146+nikhilsinhaparseable@users.noreply.github.com>
1 parent ed85a1a commit 52da33f

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

src/otel/traces.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,129 @@ mod tests {
10311031
);
10321032
}
10331033
}
1034+
1035+
#[test]
1036+
fn test_flatten_events_empty_slice() {
1037+
// flatten_events on an empty slice must return an empty Vec —
1038+
// the caller (flatten_span_record) relies on this to detect
1039+
// "no events" and fall back to a single span-only record.
1040+
let result = flatten_events(&[], 1640995200000000000);
1041+
assert!(
1042+
result.is_empty(),
1043+
"flatten_events on an empty slice should return an empty Vec"
1044+
);
1045+
}
1046+
1047+
#[test]
1048+
fn test_flatten_links_empty_slice() {
1049+
// flatten_links on an empty slice must return an empty Vec —
1050+
// the caller (flatten_span_record) relies on this to detect
1051+
// "no links" and fall back to a single span-only record.
1052+
let result = flatten_links(&[]);
1053+
assert!(
1054+
result.is_empty(),
1055+
"flatten_links on an empty slice should return an empty Vec"
1056+
);
1057+
}
1058+
1059+
#[test]
1060+
fn test_flatten_span_record_events_only_no_links() {
1061+
// A span with events but no links should produce one record per event,
1062+
// each enriched with all span-level fields.
1063+
let span = Span {
1064+
trace_id: sample_trace_id(),
1065+
span_id: sample_span_id(),
1066+
trace_state: "".to_string(),
1067+
parent_span_id: vec![],
1068+
flags: 0,
1069+
name: "events-only-span".to_string(),
1070+
kind: 1,
1071+
start_time_unix_nano: 1640995200000000000,
1072+
end_time_unix_nano: 1640995201000000000,
1073+
attributes: vec![],
1074+
dropped_attributes_count: 0,
1075+
events: vec![
1076+
Event {
1077+
time_unix_nano: 1640995200100000000,
1078+
name: "evt-a".to_string(),
1079+
attributes: vec![],
1080+
dropped_attributes_count: 0,
1081+
},
1082+
Event {
1083+
time_unix_nano: 1640995200200000000,
1084+
name: "evt-b".to_string(),
1085+
attributes: vec![],
1086+
dropped_attributes_count: 0,
1087+
},
1088+
],
1089+
dropped_events_count: 0,
1090+
links: vec![],
1091+
dropped_links_count: 0,
1092+
status: None,
1093+
};
1094+
1095+
let result = flatten_span_record(&span);
1096+
1097+
assert_eq!(result.len(), 2, "One record per event when links are empty");
1098+
assert_eq!(
1099+
result[0].get("event_name").unwrap(),
1100+
&Value::String("evt-a".to_string()),
1101+
);
1102+
assert_eq!(
1103+
result[1].get("event_name").unwrap(),
1104+
&Value::String("evt-b".to_string()),
1105+
);
1106+
// Each event record must also carry the span-level fields.
1107+
for record in &result {
1108+
assert_eq!(
1109+
record.get("span_name").unwrap(),
1110+
&Value::String("events-only-span".to_string()),
1111+
"Event records must be enriched with span fields"
1112+
);
1113+
}
1114+
}
1115+
1116+
#[test]
1117+
fn test_flatten_span_record_links_only_no_events() {
1118+
// A span with links but no events should produce one record per link,
1119+
// each enriched with all span-level fields.
1120+
let span = Span {
1121+
trace_id: sample_trace_id(),
1122+
span_id: sample_span_id(),
1123+
trace_state: "".to_string(),
1124+
parent_span_id: vec![],
1125+
flags: 0,
1126+
name: "links-only-span".to_string(),
1127+
kind: 3,
1128+
start_time_unix_nano: 1640995200000000000,
1129+
end_time_unix_nano: 1640995201000000000,
1130+
attributes: vec![],
1131+
dropped_attributes_count: 0,
1132+
events: vec![],
1133+
dropped_events_count: 0,
1134+
links: vec![Link {
1135+
trace_id: sample_trace_id(),
1136+
span_id: sample_span_id(),
1137+
trace_state: "".to_string(),
1138+
attributes: vec![],
1139+
dropped_attributes_count: 0,
1140+
flags: 0,
1141+
}],
1142+
dropped_links_count: 0,
1143+
status: None,
1144+
};
1145+
1146+
let result = flatten_span_record(&span);
1147+
1148+
assert_eq!(result.len(), 1, "One record per link when events are empty");
1149+
assert!(
1150+
result[0].contains_key("link_trace_id"),
1151+
"Link record must contain link fields"
1152+
);
1153+
assert_eq!(
1154+
result[0].get("span_name").unwrap(),
1155+
&Value::String("links-only-span".to_string()),
1156+
"Link records must be enriched with span fields"
1157+
);
1158+
}
10341159
}

0 commit comments

Comments
 (0)