Skip to content

Commit ceef938

Browse files
authored
ref(replay): only handle iso timestamps in summary trace-connected errrors (#97729)
Followup on #97647 (comment). Verified from a production query and search_issues snuba config, that both timestamp columns only store ISO. As a result we can simplify this code a bit
1 parent 3af6166 commit ceef938

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

src/sentry/replays/lib/summarize.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from collections.abc import Generator, Iterator
33
from datetime import datetime
4-
from typing import Any, Literal, TypedDict
4+
from typing import Any, TypedDict
55
from urllib.parse import urlparse
66

77
import sentry_sdk
@@ -59,18 +59,18 @@ def fetch_error_details(project_id: int, error_ids: list[str]) -> list[EventDict
5959
return []
6060

6161

62-
def _parse_snuba_timestamp_to_ms(timestamp: str | float, input_unit: Literal["s", "ms"]) -> float:
62+
def _parse_iso_timestamp_to_ms(timestamp: str | None) -> float:
6363
"""
64-
Parse a numeric or ISO timestamp to float milliseconds. `input_unit` is only used for numeric inputs.
64+
Parses a nullable ISO timestamp to float milliseconds. Errors default to 0.
6565
"""
66-
if isinstance(timestamp, str):
67-
try:
68-
dt = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
69-
return dt.timestamp() * 1000
70-
except (ValueError, AttributeError):
71-
return 0.0
66+
if not timestamp:
67+
return 0.0
7268

73-
return float(timestamp) * 1000 if input_unit == "s" else float(timestamp)
69+
try:
70+
dt = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
71+
return dt.timestamp() * 1000
72+
except (ValueError, AttributeError):
73+
return 0.0
7474

7575

7676
@sentry_sdk.trace
@@ -135,11 +135,9 @@ def fetch_trace_connected_errors(
135135
error_data = query.process_results(result)["data"]
136136

137137
for event in error_data:
138-
snuba_ts_ms = event.get("timestamp_ms", 0.0)
139-
snuba_ts_s = event.get("timestamp", 0.0)
140-
timestamp = _parse_snuba_timestamp_to_ms(
141-
snuba_ts_ms, "ms"
142-
) or _parse_snuba_timestamp_to_ms(snuba_ts_s, "s")
138+
timestamp = _parse_iso_timestamp_to_ms(
139+
event.get("timestamp_ms")
140+
) or _parse_iso_timestamp_to_ms(event.get("timestamp"))
143141

144142
if timestamp:
145143
error_events.append(

tests/sentry/replays/unit/lib/test_summarize.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sentry.models.project import Project
44
from sentry.replays.lib.summarize import (
55
EventDict,
6-
_parse_snuba_timestamp_to_ms,
6+
_parse_iso_timestamp_to_ms,
77
as_log_message,
88
get_summary_logs,
99
)
@@ -255,27 +255,20 @@ def test_as_log_message() -> None:
255255
assert as_log_message({}) is None
256256

257257

258-
def test_parse_snuba_timestamp() -> None:
259-
# Test numeric input
260-
assert _parse_snuba_timestamp_to_ms(123.456, "ms") == 123.456
261-
assert _parse_snuba_timestamp_to_ms(123, "s") == 123000
258+
def test_parse_iso_timestamp_to_ms() -> None:
259+
# Without timezone
260+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00") == 1672574400000
261+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00.123") == 1672574400123
262262

263-
# Note input unit is ignored for string inputs.
263+
# With timezone offset
264+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00+00:00") == 1672574400000
265+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00.123+00:00") == 1672574400123
264266

265-
# Test string input with ISO format without timezone
266-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00", "ms") == 1672574400000
267-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00", "s") == 1672574400000
267+
# With 'Z' timezone suffix
268+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00Z") == 1672574400000
269+
assert _parse_iso_timestamp_to_ms("2023-01-01T12:00:00.123Z") == 1672574400123
268270

269-
# Test string input with ISO format with timezone offset
270-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00+00:00", "ms") == 1672574400000
271-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00.123+00:00", "ms") == 1672574400123
272-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00+00:00", "s") == 1672574400000
273-
274-
# Test string input with ISO format with 'Z' timezone suffix
275-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00Z", "s") == 1672574400000
276-
assert _parse_snuba_timestamp_to_ms("2023-01-01T12:00:00.123Z", "ms") == 1672574400123
277-
278-
# Test invalid input
279-
assert _parse_snuba_timestamp_to_ms("invalid timestamp", "ms") == 0.0
280-
assert _parse_snuba_timestamp_to_ms("", "ms") == 0.0
281-
assert _parse_snuba_timestamp_to_ms("2023-13-01T12:00:00Z", "ms") == 0.0
271+
# Invalid input
272+
assert _parse_iso_timestamp_to_ms("invalid timestamp") == 0.0
273+
assert _parse_iso_timestamp_to_ms("") == 0.0
274+
assert _parse_iso_timestamp_to_ms("2023-13-01T12:00:00Z") == 0.0

0 commit comments

Comments
 (0)