8000 fix: More precise timestamps (#425) · etherscan-io/sentry-python@e28e211 · GitHub
[go: up one dir, main page]

Skip to content

Commit e28e211

Browse files
authored
fix: More precise timestamps (getsentry#425)
* fix: More precise timestamps * fix: Loosen assertions
1 parent 0384ce0 commit e28e211

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

sentry_sdk/serializer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
23
from datetime import datetime
34

45
from sentry_sdk.utils import (
@@ -285,7 +286,7 @@ def _serialize_node_impl(self, obj, max_depth, max_breadth):
285286
return obj
286287

287288
if isinstance(obj, datetime):
288-
return text_type(obj.strftime("%Y-%m-%dT%H:%M:%SZ"))
289+
return text_type(obj.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
289290

290291
if isinstance(obj, bytes):
291292
obj = obj.decode("utf-8", "replace")

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def inner(event):
156156
)
157157
_no_errors_in_semaphore_response(output)
158158
output.pop("_meta", None)
159+
return output
159160

160161
return inner
161162

tests/test_serializer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from datetime import datetime
2+
3+
from hypothesis import given, assume, example
4+
import hypothesis.strategies as st
5+
6+
from sentry_sdk.serializer import Serializer
7+
8+
9+
@given(dt=st.datetimes(timezones=st.just(None)))
10+
@example(dt=datetime(2001, 1, 1, 0, 0, 0, 999500))
11+
def test_datetime_precision(dt, assert_semaphore_acceptance):
12+
assume(dt.year > 2000)
13+
serializer = Serializer()
14+
15+
event = serializer.serialize_event({"timestamp": dt})
16+
normalized = assert_semaphore_acceptance(event)
17+
18+
dt2 = datetime.utcfromtimestamp(normalized["timestamp"])
19+
20+
# Float glitches can happen, and more glitches can happen
21+
# because we try to work around some float glitches in semaphore
22+
assert (dt - dt2).total_seconds() < 1.0

0 commit comments

Comments
 (0)
0