-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(workflows): Handle duplicated AlertRuleWorkflow entries for a Workflow more gracefully #113138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,11 +53,17 @@ | |
| return results | ||
|
|
||
|
|
||
| def get_rule_workflow_ids(target: Workflow | Rule) -> IdPair: | ||
| def get_rule_workflow_ids(target: Workflow | Rule, project_id: int | None = None) -> IdPair: | ||
| if isinstance(target, Workflow): | ||
| workflow_id = target.id | ||
| try: | ||
| alert_rule_workflow = AlertRuleWorkflow.objects.get(workflow=target) | ||
| qs = AlertRuleWorkflow.objects.filter(workflow=target) | ||
| if project_id is not None: | ||
| # rule_id is not a Django FK, so we use an __in subquery to | ||
| # scope to the project. Django evaluates this as a single SQL | ||
| # statement (subquery), not a separate round-trip. | ||
| qs = qs.filter(rule_id__in=Rule.objects.filter(project_id=project_id).values("id")) | ||
| alert_rule_workflow = qs.get() | ||
| rule_id = alert_rule_workflow.rule_id | ||
| except AlertRuleWorkflow.DoesNotExist: | ||
| rule_id = None | ||
|
Comment on lines
67
to
69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The call to Suggested FixModify the Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
@@ -186,8 +192,9 @@ | |
| end: datetime, | ||
| cursor: Cursor | None = None, | ||
| per_page: int = 25, | ||
| project_id: int | None = None, | ||
| ) -> CursorResult[RuleGroupHistory]: | ||
| workflow_id, rule_id = get_rule_workflow_ids(target) | ||
| workflow_id, rule_id = get_rule_workflow_ids(target, project_id=project_id) | ||
|
Check warning on line 197 in src/sentry/rules/history/backends/postgres.py
|
||
|
|
||
| if not workflow_id and isinstance(target, Rule): | ||
| logger.warning("No workflow associated with rule", extra={"rule_id": rule_id}) | ||
|
|
@@ -257,12 +264,13 @@ | |
| target: Rule | Workflow, | ||
| start: datetime, | ||
| end: datetime, | ||
| project_id: int | None = None, | ||
| ) -> Sequence[TimeSeriesValue]: | ||
| start = start.replace(tzinfo=timezone.utc) | ||
| end = end.replace(tzinfo=timezone.utc) | ||
| existing_data: dict[datetime, TimeSeriesValue] = {} | ||
|
|
||
| workflow_id, rule_id = get_rule_workflow_ids(target) | ||
| workflow_id, rule_id = get_rule_workflow_ids(target, project_id=project_id) | ||
|
Check warning on line 273 in src/sentry/rules/history/backends/postgres.py
|
||
| if not workflow_id and isinstance(target, Rule): | ||
| logger.warning("No workflow associated with rule", extra={"rule_id": target.id}) | ||
| existing_data = self._fetch_rule_fire_history_hourly_stats(target, start, end) | ||
|
|
||
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.
MultipleObjectsReturnednot caught despite graceful-handling intentMedium Severity
The
qs.get()call can still raiseMultipleObjectsReturnedif more than oneAlertRuleWorkflowmatches after filtering — for example, if two distinct rules in the same project both point to the same workflow. Thetry/exceptonly catchesAlertRuleWorkflow.DoesNotExist, so aMultipleObjectsReturnedexception would propagate as an unhandled 500 error. Since the PR's stated goal is to "avoid crashing," catching (or otherwise handling)MultipleObjectsReturned— e.g., by usingqs.first()or adding it to theexceptclause — would make the fix more robust.Reviewed by Cursor Bugbot for commit 926e629. Configure here.
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.
it's true, but shouldn't convention avoid this and we'd want to be notified about this as a different error?
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.
yep.