-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add SourcemapDetectorHandler with atomic state transitions #109633
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
Open
wedamija
wants to merge
1
commit into
danf/refactor-eventerror-strenum
Choose a base branch
from
danf/sourcemap-config-issues/pr2-detector-handler
base: danf/refactor-eventerror-strenum
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+349
−2
Open
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,147 @@ | ||
| from typing import Any | ||
|
|
||
| from sentry.issues.issue_occurrence import IssueOccurrence | ||
| from sentry.processing_errors.grouptype import ( | ||
| SourcemapCheckStatus, | ||
| SourcemapConfigurationType, | ||
| SourcemapDetectorHandler, | ||
| SourcemapPacketValue, | ||
| ) | ||
| from sentry.testutils.cases import TestCase | ||
| from sentry.workflow_engine.models.data_condition import Condition | ||
| from sentry.workflow_engine.models.data_source import DataPacket | ||
| from sentry.workflow_engine.models.detector import Detector | ||
| from sentry.workflow_engine.models.detector_state import DetectorState | ||
| from sentry.workflow_engine.types import DetectorEvaluationResult, DetectorPriorityLevel | ||
|
|
||
|
|
||
| class TestSourcemapDetectorHandler(TestCase): | ||
| def create_sourcemap_detector( | ||
| self, | ||
| detector_state: DetectorPriorityLevel = DetectorPriorityLevel.OK, | ||
| ) -> Detector: | ||
| condition_group = self.create_data_condition_group( | ||
| organization=self.project.organization, | ||
| ) | ||
| self.create_data_condition( | ||
| comparison=SourcemapCheckStatus.FAILURE, | ||
| type=Condition.EQUAL, | ||
| condition_result=DetectorPriorityLevel.HIGH, | ||
| condition_group=condition_group, | ||
| ) | ||
| self.create_data_condition( | ||
| comparison=SourcemapCheckStatus.SUCCESS, | ||
| type=Condition.EQUAL, | ||
| condition_result=DetectorPriorityLevel.OK, | ||
| condition_group=condition_group, | ||
| ) | ||
| detector = self.create_detector( | ||
| type=SourcemapConfigurationType.slug, | ||
| project=self.project, | ||
| name="Sourcemap Configuration", | ||
| config={}, | ||
| workflow_condition_group=condition_group, | ||
| ) | ||
| self.create_detector_state( | ||
| detector=detector, | ||
| state=detector_state, | ||
| is_triggered=detector_state == DetectorPriorityLevel.HIGH, | ||
| ) | ||
| return detector | ||
|
|
||
| def make_packet( | ||
| self, | ||
| errors: list | None = None, | ||
| event_id: str = "abc123", | ||
| platform: str = "javascript", | ||
| ) -> DataPacket[SourcemapPacketValue]: | ||
| if errors is None: | ||
| errors = [] | ||
| event_data: dict[str, Any] = { | ||
| "errors": errors, | ||
| "platform": platform, | ||
| "sdk": {"name": "sentry.javascript.browser", "version": "7.0.0"}, | ||
| } | ||
| return DataPacket( | ||
| source_id=str(self.project.id), | ||
| packet=SourcemapPacketValue( | ||
| event_id=event_id, | ||
| event_data=event_data, | ||
| ), | ||
| ) | ||
|
|
||
| def handle_result( | ||
| self, detector: Detector, data_packet: DataPacket[SourcemapPacketValue] | ||
| ) -> DetectorEvaluationResult | None: | ||
| handler = SourcemapDetectorHandler(detector) | ||
| evaluation = handler.evaluate(data_packet) | ||
| if None not in evaluation: | ||
| return None | ||
| return evaluation[None] | ||
|
|
||
| def test_failure_creates_occurrence(self) -> None: | ||
| detector = self.create_sourcemap_detector() | ||
|
|
||
| errors = [ | ||
| {"type": "js_no_source", "url": "https://example.com/app.js"}, | ||
| {"type": "js_invalid_source", "url": "https://example.com/vendor.js"}, | ||
| ] | ||
|
|
||
| result = self.handle_result( | ||
| detector, | ||
| self.make_packet(errors=errors, event_id="test-event-123", platform="javascript"), | ||
| ) | ||
|
|
||
| assert result is not None | ||
| assert result.priority == DetectorPriorityLevel.HIGH | ||
| assert isinstance(result.result, IssueOccurrence) | ||
| assert result.result.issue_title == "Broken source maps detected" | ||
| assert result.result.evidence_data["error_types"] == ["js_invalid_source", "js_no_source"] | ||
| assert result.result.evidence_data["sample_event_id"] == "test-event-123" | ||
| assert result.event_data is not None | ||
| assert result.event_data["platform"] == "javascript" | ||
|
|
||
| state = DetectorState.objects.get(detector=detector) | ||
| assert state.is_triggered is True | ||
| assert state.state == str(DetectorPriorityLevel.HIGH) | ||
|
|
||
| def test_duplicate_failure_does_not_trigger(self) -> None: | ||
| detector = self.create_sourcemap_detector() | ||
| packet = self.make_packet( | ||
| errors=[{"type": "js_no_source", "url": "https://example.com/app.js"}], | ||
| ) | ||
|
|
||
| result = self.handle_result(detector, packet) | ||
| assert result is not None | ||
| assert isinstance(result.result, IssueOccurrence) | ||
|
|
||
| result = self.handle_result(detector, packet) | ||
| assert result is None | ||
|
|
||
| def test_no_sourcemap_errors_does_not_trigger(self) -> None: | ||
| detector = self.create_sourcemap_detector() | ||
|
|
||
| assert self.handle_result(detector, self.make_packet(errors=[])) is None | ||
| assert ( | ||
| self.handle_result( | ||
| detector, | ||
| self.make_packet(errors=[{"type": "native_missing_dsym", "image": "libfoo.so"}]), | ||
| ) | ||
| is None | ||
| ) | ||
|
|
||
| def test_failure_without_detector_state_creates_it(self) -> None: | ||
| detector = self.create_sourcemap_detector() | ||
| DetectorState.objects.filter(detector=detector).delete() | ||
|
|
||
| result = self.handle_result( | ||
| detector, | ||
| self.make_packet( | ||
| errors=[{"type": "js_no_source", "url": "https://example.com/app.js"}], | ||
| ), | ||
| ) | ||
|
|
||
| assert result is not None | ||
| assert isinstance(result.result, IssueOccurrence) | ||
| state = DetectorState.objects.get(detector=detector) | ||
| assert state.is_triggered is True |
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.
Missing
transaction.atomic()aroundcreate()catchingIntegrityErrorMedium Severity
The
IntegrityErrorfromDetectorState.objects.create()is caught without wrapping the call intransaction.atomic(). In PostgreSQL, a failed statement inside a transaction aborts the entire transaction. While this works in autocommit mode (current production path), if_try_state_transitionis ever called inside atransaction.atomic()block — including Django'sTestCasewhich wraps every test in one — the caughtIntegrityErrorleaves the connection in a broken state, causing subsequent DB operations to fail withTransactionManagementError. The codebase already follows the correct pattern inprocessors/action.py, whereIntegrityErroris caught from atransaction.atomic()block.