-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(aci): Ensure GroupOpenPeriod close times aren't before start time #102345
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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,58 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from django.utils import timezone | ||
|
|
||
| from sentry.incidents.grouptype import MetricIssue | ||
| from sentry.models.activity import Activity | ||
| from sentry.models.groupopenperiod import GroupOpenPeriod, create_open_period | ||
| from sentry.testutils.cases import TestCase | ||
| from sentry.types.activity import ActivityType | ||
|
|
||
|
|
||
| class CloseOpenPeriodTest(TestCase): | ||
| def setUp(self) -> None: | ||
| super().setUp() | ||
| self.organization = self.create_organization() | ||
| self.project = self.create_project(organization=self.organization) | ||
| self.group = self.create_group(project=self.project, type=MetricIssue.type_id) | ||
|
|
||
| # Create an open period | ||
| self.start_time = timezone.now() - timedelta(hours=2) | ||
| create_open_period(self.group, self.start_time) | ||
| open_period = GroupOpenPeriod.objects.filter(group=self.group).first() | ||
| assert open_period is not None | ||
| assert open_period.date_ended is None | ||
| self.open_period = open_period | ||
|
|
||
| def test_close_open_period_resolution_time_before_start_time(self) -> None: | ||
| """ | ||
| Test that when resolution_time is before date_started, we use current time | ||
| as the close time. This prevents DataError from PostgreSQL range constraint. | ||
| Since open periods track Sentry's internal view of when issues are open, | ||
| using current time is more accurate than creating a zero-duration period. | ||
| """ | ||
| # Resolution time is 3 hours before the start time (problematic) | ||
| resolution_time = self.start_time - timedelta(hours=3) | ||
| activity = Activity.objects.create( | ||
| group=self.group, | ||
| project=self.project, | ||
| type=ActivityType.SET_RESOLVED.value, | ||
| user_id=self.user.id, | ||
| ) | ||
|
|
||
| before_close = timezone.now() | ||
| self.open_period.close_open_period( | ||
| resolution_activity=activity, | ||
| resolution_time=resolution_time, | ||
| ) | ||
| after_close = timezone.now() | ||
|
|
||
| # Should use current time as the close time, not the provided resolution_time | ||
| self.open_period.refresh_from_db() | ||
| assert self.open_period.date_ended is not None | ||
| assert self.open_period.date_ended != resolution_time | ||
| assert self.open_period.date_ended >= self.start_time | ||
| # Verify it's approximately now (within the test execution window) | ||
| assert before_close <= self.open_period.date_ended <= after_close | ||
| assert self.open_period.resolution_activity == activity | ||
| assert self.open_period.user_id == self.user.id |
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.
🤔 not 100% sure we should close in these scenarios. curious why we are getting into these states in general? a little nervous that we could be getting something out of order, and we're closing a period that wasn't meant to be closed yet.
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.
These seem to occur because for uptime monitors we create the open period through occurrence processing queues using a datetime.now() (so, with some delay, using Sentry-app-based clock), while they are closed using a timestamp provided as part of the check-in request (from Relay?) and I think that timestamp is when we actually did the checkin on another service, so it can be before we actually opened it in some cases.
So, they are two different timestamp sources identifying different things. I'm not sure which one we want to use, but we probably would prefer to be consistent. This change uses a valid and reasonable timestamp when the one being offered isn't valid/reasonable. I still think we want to address the inconsistency; this just does a reasonable thing when the input is unreasonable.