Skip to content

Commit aada5ce

Browse files
committed
Fix tests for observed time, add larger bounds around the expected check since there is some mismatch between pythons datetime and rust's time as noted in a previous PR.
1 parent 47cd04e commit aada5ce

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

tests/integration/test_ourlogs.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def test_ourlog_extraction_with_otel_logs(
9696

9797
timestamp_proto.FromSeconds(int(timestamp))
9898

99+
start_timestamp_nanos = int((start.timestamp() - 1) * 1e9)
100+
end = datetime.now(timezone.utc)
101+
end_timestamp_nanos = int((end.timestamp() + 1) * 1e9)
102+
99103
expected_logs = [
100104
MessageToDict(
101105
TraceItem(
@@ -136,12 +140,16 @@ def test_ourlog_extraction_with_otel_logs(
136140
logs = [MessageToDict(log) for log in ourlogs_consumer.get_ourlogs()]
137141

138142
for log, expected_log in zip(logs, expected_logs):
139-
# we can't generate uuid7 with a specific timestamp
140-
# in Python just yet so we're overriding it
141143
expected_log["itemId"] = log["itemId"]
142144
expected_log["received"] = time_within_delta()
143145

144-
# This field is set by Relay so we need to remove it
146+
observed_timestamp_str = log["attributes"]["sentry.observed_timestamp_nanos"][
147+
"stringValue"
148+
]
149+
observed_timestamp = int(observed_timestamp_str)
150+
assert observed_timestamp > 0
151+
assert start_timestamp_nanos < observed_timestamp < end_timestamp_nanos
152+
145153
del log["attributes"]["sentry.observed_timestamp_nanos"]
146154

147155
assert logs == expected_logs
@@ -266,6 +274,10 @@ def test_ourlog_extraction_with_sentry_logs(
266274

267275
relay.send_envelope(project_id, envelope)
268276

277+
start_timestamp_nanos = int((start.timestamp() - 1) * 1e9)
278+
end = datetime.now(timezone.utc)
279+
end_timestamp_nanos = int((end.timestamp() + 1) * 1e9)
280+
269281
timestamp_nanos = int(timestamp * 1e6) * 1000
270282
timestamp_proto = Timestamp()
271283

@@ -293,9 +305,6 @@ def test_ourlog_extraction_with_sentry_logs(
293305
"sentry.severity_text": AnyValue(string_value="error"),
294306
"sentry.span_id": AnyValue(string_value="eee19b7ec3c1b175"),
295307
"sentry.trace_flags": AnyValue(int_value=0),
296-
"sentry.observed_timestamp_nanos": AnyValue(
297-
string_value=str(timestamp_nanos)
298-
),
299308
"sentry.timestamp_precise": AnyValue(int_value=timestamp_nanos),
300309
"sentry.timestamp_nanos": AnyValue(
301310
string_value=str(timestamp_nanos)
@@ -328,9 +337,6 @@ def test_ourlog_extraction_with_sentry_logs(
328337
"sentry.severity_text": AnyValue(string_value="info"),
329338
"sentry.trace_flags": AnyValue(int_value=0),
330339
"sentry.span_id": AnyValue(string_value="eee19b7ec3c1b174"),
331-
"sentry.observed_timestamp_nanos": AnyValue(
332-
string_value=str(timestamp_nanos)
333-
),
334340
"string.attribute": AnyValue(string_value="some string"),
335341
"valid_string_with_other": AnyValue(string_value="test"),
336342
"sentry.timestamp_precise": AnyValue(int_value=timestamp_nanos),
@@ -348,11 +354,18 @@ def test_ourlog_extraction_with_sentry_logs(
348354
logs = [MessageToDict(log) for log in ourlogs_consumer.get_ourlogs()]
349355

350356
for log, expected_log in zip(logs, expected_logs):
351-
# we can't generate uuid7 with a specific timestamp
352-
# in Python just yet so we're overriding it
353357
expected_log["itemId"] = log["itemId"]
354358
expected_log["received"] = time_within_delta()
355359

360+
observed_timestamp_str = log["attributes"]["sentry.observed_timestamp_nanos"][
361+
"stringValue"
362+
]
363+
observed_timestamp = int(observed_timestamp_str)
364+
assert observed_timestamp > 0
365+
assert start_timestamp_nanos < observed_timestamp < end_timestamp_nanos
366+
367+
del log["attributes"]["sentry.observed_timestamp_nanos"]
368+
356369
assert logs == expected_logs
357370

358371
ourlogs_consumer.assert_empty()
@@ -383,6 +396,10 @@ def test_ourlog_extraction_with_sentry_logs_with_missing_fields(
383396

384397
relay.send_envelope(project_id, envelope)
385398

399+
start_timestamp_nanos = int((start.timestamp() - 1) * 1e9)
400+
end = datetime.now(timezone.utc)
401+
end_timestamp_nanos = int((end.timestamp() + 1) * 1e9)
402+
386403
timestamp_nanos = int(timestamp * 1e6) * 1000
387404
timestamp_proto = Timestamp()
388405

@@ -405,9 +422,6 @@ def test_ourlog_extraction_with_sentry_logs_with_missing_fields(
405422
"sentry.body": AnyValue(string_value="Example log record 2"),
406423
"sentry.browser.name": AnyValue(string_value="Python Requests"),
407424
"sentry.browser.version": AnyValue(string_value="2.32"),
408-
"sentry.observed_timestamp_nanos": AnyValue(
409-
string_value=str(timestamp_nanos)
410-
),
411425
"sentry.severity_number": AnyValue(int_value=13),
412426
"sentry.severity_text": AnyValue(string_value="warn"),
413427
"sentry.timestamp_nanos": AnyValue(
@@ -426,11 +440,18 @@ def test_ourlog_extraction_with_sentry_logs_with_missing_fields(
426440
logs = [MessageToDict(log) for log in ourlogs_consumer.get_ourlogs()]
427441

428442
for log, expected_log in zip(logs, expected_logs):
429-
# we can't generate uuid7 with a specific timestamp
430-
# in Python just yet so we're overriding it
431443
expected_log["itemId"] = log["itemId"]
432444
expected_log["received"] = time_within_delta()
433445

446+
observed_timestamp_str = log["attributes"]["sentry.observed_timestamp_nanos"][
447+
"stringValue"
448+
]
449+
observed_timestamp = int(observed_timestamp_str)
450+
assert observed_timestamp > 0
451+
assert start_timestamp_nanos < observed_timestamp < end_timestamp_nanos
452+
453+
del log["attributes"]["sentry.observed_timestamp_nanos"]
454+
434455
assert logs == expected_logs
435456

436457
ourlogs_consumer.assert_empty()
@@ -469,8 +490,8 @@ def test_ourlog_extraction_is_disabled_without_feature(
469490
"131.0.0",
470491
),
471492
(
472-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
473-
"Chrome",
493+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
494+
"Edge",
474495
"120.0.0",
475496
),
476497
# Firefox desktop
@@ -544,6 +565,10 @@ def test_browser_name_version_extraction(
544565

545566
relay.send_envelope(project_id, envelope, headers={"User-Agent": user_agent})
546567

568+
start_timestamp_nanos = int((start.timestamp() - 1) * 1e9)
569+
end = datetime.now(timezone.utc)
570+
end_timestamp_nanos = int((end.timestamp() + 1) * 1e9)
571+
547572
timestamp_nanos = int(timestamp * 1e6) * 1000
548573
timestamp_proto = Timestamp()
549574

@@ -571,9 +596,6 @@ def test_browser_name_version_extraction(
571596
"sentry.severity_text": AnyValue(string_value="error"),
572597
"sentry.span_id": AnyValue(string_value="eee19b7ec3c1b175"),
573598
"sentry.trace_flags": AnyValue(int_value=0),
574-
"sentry.observed_timestamp_nanos": AnyValue(
575-
string_value=str(timestamp_nanos)
576-
),
577599
"sentry.timestamp_precise": AnyValue(int_value=timestamp_nanos),
578600
"sentry.timestamp_nanos": AnyValue(string_value=str(timestamp_nanos)),
579601
},
@@ -588,11 +610,18 @@ def test_browser_name_version_extraction(
588610
assert len(logs) == 1
589611
log = logs[0]
590612

591-
# we can't generate uuid7 with a specific timestamp
592-
# in Python just yet so we're overriding it
593613
expected_log["itemId"] = log["itemId"]
594614
expected_log["received"] = time_within_delta()
595615

616+
observed_timestamp_str = log["attributes"]["sentry.observed_timestamp_nanos"][
617+
"stringValue"
618+
]
619+
observed_timestamp = int(observed_timestamp_str)
620+
assert observed_timestamp > 0
621+
assert start_timestamp_nanos < observed_timestamp < end_timestamp_nanos
622+
623+
del log["attributes"]["sentry.observed_timestamp_nanos"]
624+
596625
assert log == expected_log
597626

598627
ourlogs_consumer.assert_empty()

0 commit comments

Comments
 (0)