-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(crons): Start dual writing DataSource
and Detector
rows for crons
#97542
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
Merged
Merged
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
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
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,120 @@ | ||
from unittest.mock import patch | ||
|
||
from django.db import IntegrityError | ||
|
||
from sentry.issues.grouptype import MonitorIncidentType | ||
from sentry.monitors.types import DATA_SOURCE_CRON_MONITOR | ||
from sentry.monitors.utils import ensure_cron_detector | ||
from sentry.testutils.cases import TestCase | ||
from sentry.workflow_engine.models import DataSource, DataSourceDetector, Detector | ||
|
||
|
||
class EnsureCronDetectorTest(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.monitor = self.create_monitor(owner_user_id=None) | ||
|
||
def test_creates_data_source_and_detector_for_new_monitor(self): | ||
assert not DataSource.objects.filter( | ||
type=DATA_SOURCE_CRON_MONITOR, | ||
organization_id=self.monitor.organization_id, | ||
source_id=str(self.monitor.id), | ||
).exists() | ||
|
||
ensure_cron_detector(self.monitor) | ||
data_source = DataSource.objects.get( | ||
type=DATA_SOURCE_CRON_MONITOR, | ||
organization_id=self.monitor.organization_id, | ||
source_id=str(self.monitor.id), | ||
) | ||
assert data_source is not None | ||
detector = Detector.objects.get( | ||
type=MonitorIncidentType.slug, | ||
project_id=self.monitor.project_id, | ||
name=self.monitor.name, | ||
) | ||
assert detector is not None | ||
assert detector.owner_user_id == self.monitor.owner_user_id | ||
assert detector.owner_team_id == self.monitor.owner_team_id | ||
assert DataSourceDetector.objects.filter( | ||
data_source=data_source, | ||
detector=detector, | ||
).exists() | ||
|
||
def test_idempotent_for_existing_data_source(self): | ||
ensure_cron_detector(self.monitor) | ||
data_source = DataSource.objects.get( | ||
type=DATA_SOURCE_CRON_MONITOR, | ||
organization_id=self.monitor.organization_id, | ||
source_id=str(self.monitor.id), | ||
) | ||
detector = Detector.objects.get( | ||
type=MonitorIncidentType.slug, | ||
project_id=self.monitor.project_id, | ||
name=self.monitor.name, | ||
) | ||
link = DataSourceDetector.objects.get( | ||
data_source=data_source, | ||
detector=detector, | ||
) | ||
ensure_cron_detector(self.monitor) | ||
data_source_after = DataSource.objects.get( | ||
type=DATA_SOURCE_CRON_MONITOR, | ||
organization_id=self.monitor.organization_id, | ||
source_id=str(self.monitor.id), | ||
) | ||
detector_after = Detector.objects.get( | ||
type=MonitorIncidentType.slug, | ||
project_id=self.monitor.project_id, | ||
name=self.monitor.name, | ||
) | ||
link_after = DataSourceDetector.objects.get( | ||
data_source=data_source, | ||
detector=detector, | ||
) | ||
assert data_source.id == data_source_after.id | ||
assert detector.id == detector_after.id | ||
assert link.id == link_after.id | ||
|
||
def test_with_owner_user(self): | ||
self.monitor.owner_user_id = self.user.id | ||
self.monitor.save() | ||
ensure_cron_detector(self.monitor) | ||
detector = Detector.objects.get( | ||
type=MonitorIncidentType.slug, | ||
project_id=self.monitor.project_id, | ||
) | ||
assert detector.owner_user_id == self.user.id | ||
assert detector.owner_team_id is None | ||
|
||
def test_with_no_owner(self): | ||
ensure_cron_detector(self.monitor) | ||
|
||
detector = Detector.objects.get( | ||
type=MonitorIncidentType.slug, | ||
project_id=self.monitor.project_id, | ||
) | ||
assert detector.owner_user_id is None | ||
assert detector.owner_team_id is None | ||
|
||
def test_handles_database_errors_gracefully(self): | ||
with ( | ||
patch("sentry.monitors.utils.logger") as mock_logger, | ||
patch("sentry.monitors.utils.DataSource.objects.get_or_create") as mock_get_or_create, | ||
): | ||
mock_get_or_create.side_effect = IntegrityError("Database error") | ||
|
||
ensure_cron_detector(self.monitor) | ||
mock_logger.exception.assert_called_once_with("Error creating cron detector") | ||
assert not DataSource.objects.filter( | ||
type=DATA_SOURCE_CRON_MONITOR, source_id=str(self.monitor.id) | ||
).exists() | ||
|
||
def test_atomic_transaction_rollback(self): | ||
with patch("sentry.monitors.utils.Detector.objects.create") as mock_create: | ||
mock_create.side_effect = IntegrityError("Cannot create detector") | ||
|
||
ensure_cron_detector(self.monitor) | ||
assert not DataSource.objects.filter( | ||
type=DATA_SOURCE_CRON_MONITOR, source_id=str(self.monitor.id) | ||
).exists() | ||
wedamija marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
I don't feel like much can go wrong here, but if we want to be more cautious we could put this creation behind a flag.