-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(clock-drift): Apply clock drift normalization only if sent_at
is defined
#3405
Merged
iambriccardo
merged 8 commits into
master
from
riccardo/fix/correctly-apply-clock-drift
Apr 12, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ec8bdc
Fix
iambriccardo 1b0adb7
Fix
iambriccardo 975f04d
Add tests
iambriccardo c653c34
Update changelog
iambriccardo 0609ce7
Merge
iambriccardo 1dc4a6f
Fix
iambriccardo ae04f5f
Improve
iambriccardo 66341a2
Improve
iambriccardo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from datetime import timedelta, datetime, timezone | ||
|
||
from sentry_sdk.envelope import Envelope | ||
|
||
|
||
def mock_transaction(timestamp): | ||
return { | ||
"type": "transaction", | ||
"timestamp": format_date(timestamp), | ||
"start_timestamp": format_date(timestamp - timedelta(seconds=2)), | ||
"spans": [], | ||
"contexts": { | ||
"trace": { | ||
"op": "http", | ||
"trace_id": "a0fa8803753e40fd8124b21eeb2986b5", | ||
"span_id": "968cff94913ebb07", | ||
} | ||
}, | ||
"transaction": "/hello", | ||
} | ||
|
||
|
||
def format_date(date): | ||
return date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") | ||
|
||
|
||
def test_clock_drift_applied_when_timestamp_is_too_old(mini_sentry, relay): | ||
relay = relay(mini_sentry) | ||
project_id = 42 | ||
mini_sentry.add_basic_project_config(project_id) | ||
|
||
now = datetime.now(tz=timezone.utc) | ||
one_month_ago = now - timedelta(days=30) | ||
|
||
envelope = Envelope(headers={"sent_at": format_date(one_month_ago)}) | ||
envelope.add_transaction(mock_transaction(one_month_ago)) | ||
|
||
relay.send_envelope(project_id, envelope, headers={"Accept-Encoding": "gzip"}) | ||
|
||
transaction_event = mini_sentry.captured_events.get( | ||
timeout=1 | ||
).get_transaction_event() | ||
|
||
error_name, error_metadata = transaction_event["_meta"]["timestamp"][""]["err"][0] | ||
assert error_name == "clock_drift" | ||
assert ( | ||
one_month_ago.timestamp() | ||
< transaction_event["timestamp"] | ||
< datetime.now(tz=timezone.utc).timestamp() | ||
) | ||
|
||
|
||
def test_clock_drift_not_applied_when_timestamp_is_recent(mini_sentry, relay): | ||
relay = relay(mini_sentry) | ||
project_id = 42 | ||
mini_sentry.add_basic_project_config(project_id) | ||
|
||
now = datetime.now(tz=timezone.utc) | ||
five_minutes_ago = now - timedelta(minutes=5) | ||
|
||
envelope = Envelope(headers={"sent_at": format_date(five_minutes_ago)}) | ||
envelope.add_transaction(mock_transaction(five_minutes_ago)) | ||
|
||
relay.send_envelope(project_id, envelope, headers={"Accept-Encoding": "gzip"}) | ||
|
||
transaction_event = mini_sentry.captured_events.get( | ||
timeout=1 | ||
).get_transaction_event() | ||
assert "_meta" not in transaction_event | ||
assert transaction_event["timestamp"] == five_minutes_ago.timestamp() | ||
|
||
|
||
def test_clock_drift_not_applied_when_sent_at_is_not_supplied(mini_sentry, relay): | ||
relay = relay(mini_sentry) | ||
project_id = 42 | ||
mini_sentry.add_basic_project_config(project_id) | ||
|
||
now = datetime.now(tz=timezone.utc) | ||
one_month_ago = now - timedelta(days=30) | ||
|
||
envelope = Envelope() | ||
envelope.add_transaction(mock_transaction(one_month_ago)) | ||
|
||
relay.send_envelope(project_id, envelope, headers={"Accept-Encoding": "gzip"}) | ||
|
||
transaction_event = mini_sentry.captured_events.get( | ||
timeout=1 | ||
).get_transaction_event() | ||
error_name, error_metadata = transaction_event["_meta"]["timestamp"][""]["err"][0] | ||
# In case clock drift is not run, we expect timestamps normalization to go into effect to mark timestamps as | ||
# past or future if they surpass a certain threshold. | ||
assert error_name == "past_timestamp" | ||
assert ( | ||
one_month_ago.timestamp() | ||
< transaction_event["timestamp"] | ||
< datetime.now(tz=timezone.utc).timestamp() | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also assert on the actual timestamps in the event? (Same for tests below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering it, my only problem was that
received_at
seems to be inferred by Relay on receive time, so I might have to either mock things or freeze time but it seems like this is not possible in an integration test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I know is that
sdk_time
will beone_month_ago
andserver_time
will bereceived_at
and the new timestamp is + the difference of the above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opted for a middle ground, I do test for
>
in the timestamps which should be normalized, and=
when the timestamps must remain the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assert could be a much smaller window though, right?
transaction_event["timestamp"] > now.timestamp()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there was a way to freeze or hardcode the
received_at
time, I could compute the exact test expectations.If we can't, to make the test more precise, we could assert
transaction_event["timestamp"] == now
within some bounds but I am not super confident to define them since they are dependent on the time it takes for the integration test to run and I would not want to introduce a flaky test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't exactly assert, but if you compare to the
now
value taken at the beginning of the test, that should be good, additionally you could also take another "now" value at the end and check whether the expected value is between those. I wouldn't try to freeze time etc. that will just break at some point. Being within a few seconds should be good enough.