Skip to content

Commit

Permalink
Merge branch 'potel-base' into potel-base-run-all-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Dec 6, 2024
2 parents 112efaf + a545ec0 commit cfc6ac8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
capture_internal_exception,
capture_internal_exceptions,
ContextVar,
datetime_from_isoformat,
disable_capture_event,
event_from_exception,
exc_info_from_error,
Expand Down Expand Up @@ -1264,7 +1265,7 @@ def _apply_breadcrumbs_to_event(self, event, hint, options):
try:
for crumb in event["breadcrumbs"]["values"]:
if isinstance(crumb["timestamp"], str):
crumb["timestamp"] = datetime.fromisoformat(crumb["timestamp"])
crumb["timestamp"] = datetime_from_isoformat(crumb["timestamp"])

event["breadcrumbs"]["values"].sort(key=lambda crumb: crumb["timestamp"])
except Exception as err:
Expand Down
25 changes: 25 additions & 0 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,3 +1929,28 @@ def _serialize_span_attribute(value):
return str(value)
except Exception:
return None


ISO_TZ_SEPARATORS = frozenset(("+", "-"))


def datetime_from_isoformat(value):
# type: (str) -> datetime
try:
result = datetime.fromisoformat(value)
except (AttributeError, ValueError):
# py 3.6
timestamp_format = (
"%Y-%m-%dT%H:%M:%S.%f" if "." in value else "%Y-%m-%dT%H:%M:%S"
)
if value.endswith("Z"):
value = value[:-1] + "+0000"

if value[-6] in ISO_TZ_SEPARATORS:
timestamp_format += "%z"
value = value[:-3] + value[-2:]
elif value[-5] in ISO_TZ_SEPARATORS:
timestamp_format += "%z"

result = datetime.strptime(value, timestamp_format)
return result.astimezone(timezone.utc)
6 changes: 3 additions & 3 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.stdlib import StdlibIntegration
from sentry_sdk.scope import add_global_event_processor
from sentry_sdk.utils import get_sdk_name, reraise
from sentry_sdk.utils import datetime_from_isoformat, get_sdk_name, reraise
from sentry_sdk.tracing_utils import has_tracing_enabled


Expand Down Expand Up @@ -348,7 +348,7 @@ def test_breadcrumb_ordering(sentry_init, capture_events):

assert len(event["breadcrumbs"]["values"]) == len(timestamps)
timestamps_from_event = [
datetime.fromisoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"]
datetime_from_isoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"]
]
assert timestamps_from_event == sorted(timestamps)

Expand Down Expand Up @@ -389,7 +389,7 @@ def test_breadcrumb_ordering_different_types(sentry_init, capture_events):

assert len(event["breadcrumbs"]["values"]) == len(timestamps)
timestamps_from_event = [
datetime.fromisoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"]
datetime_from_isoformat(x["timestamp"]) for x in event["breadcrumbs"]["values"]
]
assert timestamps_from_event == sorted(timestamps)

Expand Down
50 changes: 50 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sentry_sdk.utils import (
Components,
Dsn,
datetime_from_isoformat,
env_to_bool,
format_timestamp,
get_current_thread_meta,
Expand Down Expand Up @@ -933,3 +934,52 @@ def __str__(self):
)
def test_serialize_span_attribute(value, result):
assert _serialize_span_attribute(value) == result


@pytest.mark.parametrize(
("input_str", "expected_output"),
(
(
"2021-01-01T00:00:00.000000Z",
datetime(2021, 1, 1, tzinfo=timezone.utc),
), # UTC time
(
"2021-01-01T00:00:00.000000",
datetime(2021, 1, 1, tzinfo=datetime.now().astimezone().tzinfo),
), # No TZ -- assume UTC
(
"2021-01-01T00:00:00Z",
datetime(2021, 1, 1, tzinfo=timezone.utc),
), # UTC - No milliseconds
(
"2021-01-01T00:00:00.000000+00:00",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000-00:00",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000+0000",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2021-01-01T00:00:00.000000-0000",
datetime(2021, 1, 1, tzinfo=timezone.utc),
),
(
"2020-12-31T00:00:00.000000+02:00",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=2))),
), # UTC+2 time
(
"2020-12-31T00:00:00.000000-0200",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
), # UTC-2 time
(
"2020-12-31T00:00:00-0200",
datetime(2020, 12, 31, tzinfo=timezone(timedelta(hours=-2))),
), # UTC-2 time - no milliseconds
),
)
def test_datetime_from_isoformat(input_str, expected_output):
assert datetime_from_isoformat(input_str) == expected_output, input_str

0 comments on commit cfc6ac8

Please sign in to comment.