Skip to content

Commit 4d7b58f

Browse files
committed
Improve unit tests for timestamp normalization
1 parent 15502f8 commit 4d7b58f

1 file changed

Lines changed: 34 additions & 54 deletions

File tree

bottlecap/src/otlp/processor.rs

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -137,78 +137,58 @@ mod tests {
137137
use serde_json::json;
138138

139139
#[test]
140-
fn test_normalize_timestamp_string_unchanged() {
141-
let mut value = json!("1581452772000000321");
142-
normalize_timestamp_value(&mut value);
143-
assert_eq!(value, json!("1581452772000000321"));
140+
fn test_integer_timestamp_converted_to_string() {
141+
let mut value = json!({"startTimeUnixNano": 1_581_452_772_000_000_321_u64});
142+
normalize_timestamps(&mut value);
143+
assert_eq!(value["startTimeUnixNano"], json!("1581452772000000321"));
144144
}
145145

146146
#[test]
147-
fn test_normalize_timestamp_integer_to_string() {
148-
let mut value = json!(1_581_452_772_000_000_321_u64);
149-
normalize_timestamp_value(&mut value);
150-
assert_eq!(value, json!("1581452772000000321"));
147+
fn test_split_object_timestamp_reconstructed() {
148+
// Some old JS SDKs send 64-bit ints as {"low": u32, "high": u32}
149+
let mut value = json!({"startTimeUnixNano": {"low": 1_029_784_000_u64, "high": 395_146_000_u64}});
150+
normalize_timestamps(&mut value);
151+
let expected = (395_146_000_u64 << 32) | 1_029_784_000_u64;
152+
assert_eq!(value["startTimeUnixNano"], json!(expected.to_string()));
151153
}
152154

153155
#[test]
154-
fn test_normalize_timestamp_object_to_string() {
155-
// {"low": 1029784000, "high": 395146000} represents a split 64-bit int
156-
// high << 32 | low = 395146000 << 32 | 1029784000 = 1697551827029784000
157-
let mut value = json!({"low": 1_029_784_000_u64, "high": 395_146_000_u64});
158-
normalize_timestamp_value(&mut value);
159-
let expected = (395_146_000_u64 << 32) | 1_029_784_000_u64;
160-
assert_eq!(value, json!(expected.to_string()));
156+
fn test_non_timestamp_integers_unchanged() {
157+
// Verify we only convert timestamp fields, not all integers
158+
let mut value = json!({
159+
"kind": 1,
160+
"droppedAttributesCount": 5,
161+
"attributes": [{"value": {"intValue": 42}}],
162+
"startTimeUnixNano": 12345_u64
163+
});
164+
normalize_timestamps(&mut value);
165+
166+
// These should remain as integers
167+
assert_eq!(value["kind"], json!(1));
168+
assert_eq!(value["droppedAttributesCount"], json!(5));
169+
assert_eq!(value["attributes"][0]["value"]["intValue"], json!(42));
170+
// Only this should be converted
171+
assert_eq!(value["startTimeUnixNano"], json!("12345"));
161172
}
162173

163174
#[test]
164-
fn test_normalize_timestamps_nested_structure() {
175+
fn test_nested_event_timestamps_normalized() {
165176
let mut value = json!({
166177
"resourceSpans": [{
167178
"scopeSpans": [{
168179
"spans": [{
169-
"name": "test-span",
170-
"startTimeUnixNano": 1_581_452_772_000_000_321_u64,
171-
"endTimeUnixNano": "1581452772000000999",
172-
"events": [{
173-
"timeUnixNano": {"low": 1_029_784_000_u64, "high": 395_146_000_u64}
174-
}]
180+
"startTimeUnixNano": 100_u64,
181+
"endTimeUnixNano": "200",
182+
"events": [{"timeUnixNano": 300_u64}]
175183
}]
176184
}]
177185
}]
178186
});
179-
180-
normalize_timestamps(&mut value);
181-
182-
// Check startTimeUnixNano was converted from integer to string
183-
let start_time =
184-
&value["resourceSpans"][0]["scopeSpans"][0]["spans"][0]["startTimeUnixNano"];
185-
assert_eq!(start_time, &json!("1581452772000000321"));
186-
187-
// Check endTimeUnixNano was left as string
188-
let end_time = &value["resourceSpans"][0]["scopeSpans"][0]["spans"][0]["endTimeUnixNano"];
189-
assert_eq!(end_time, &json!("1581452772000000999"));
190-
191-
// Check event timeUnixNano was converted from object to string
192-
let event_time =
193-
&value["resourceSpans"][0]["scopeSpans"][0]["spans"][0]["events"][0]["timeUnixNano"];
194-
let expected = (395_146_000_u64 << 32) | 1_029_784_000_u64;
195-
assert_eq!(event_time, &json!(expected.to_string()));
196-
}
197-
198-
#[test]
199-
fn test_normalize_timestamps_preserves_other_fields() {
200-
let mut value = json!({
201-
"name": "test",
202-
"kind": 1,
203-
"attributes": [{"key": "foo", "value": {"intValue": 42}}],
204-
"startTimeUnixNano": 12345_u64
205-
});
206-
207187
normalize_timestamps(&mut value);
208188

209-
assert_eq!(value["name"], json!("test"));
210-
assert_eq!(value["kind"], json!(1));
211-
assert_eq!(value["attributes"][0]["value"]["intValue"], json!(42));
212-
assert_eq!(value["startTimeUnixNano"], json!("12345"));
189+
let span = &value["resourceSpans"][0]["scopeSpans"][0]["spans"][0];
190+
assert_eq!(span["startTimeUnixNano"], json!("100"));
191+
assert_eq!(span["endTimeUnixNano"], json!("200")); // Already string
192+
assert_eq!(span["events"][0]["timeUnixNano"], json!("300"));
213193
}
214194
}

0 commit comments

Comments
 (0)