Skip to content

perf(workflows): Avoid excessive querying in WorkflowEngineRuleSerializer#111942

Merged
kcons merged 3 commits into
masterfrom
kcons/lessq
Mar 31, 2026
Merged

perf(workflows): Avoid excessive querying in WorkflowEngineRuleSerializer#111942
kcons merged 3 commits into
masterfrom
kcons/lessq

Conversation

@kcons
Copy link
Copy Markdown
Member

@kcons kcons commented Mar 31, 2026

The previous logic had us potentially sending 100+ workflow queries, which can add 300ms+ to the request.
By only fetching ids, we avoid considerable unnecessary work.

@kcons kcons requested a review from ceorourke March 31, 2026 20:09
@kcons kcons requested a review from a team as a code owner March 31, 2026 20:09
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Mar 31, 2026
Comment thread src/sentry/api/serializers/models/rule.py Outdated
Comment thread src/sentry/api/serializers/models/rule.py
Comment thread src/sentry/api/serializers/models/rule.py Outdated
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Missing project crashes workflow serialization
    • Changed direct dict indexing to use .get() with a None check to gracefully skip projects that are not returned by get_many_from_cache().

Create PR

Or push these changes by commenting:

@cursor push fc2aeaafd0
Preview (fc2aeaafd0)
diff --git a/src/sentry/api/serializers/models/rule.py b/src/sentry/api/serializers/models/rule.py
--- a/src/sentry/api/serializers/models/rule.py
+++ b/src/sentry/api/serializers/models/rule.py
@@ -410,7 +410,9 @@
         )
         projects_by_id = {project.id: project for project in projects}
         for workflow_id, project_id in workflows_to_project_ids:
-            workflow_to_projects[workflow_id].add(projects_by_id[project_id])
+            project = projects_by_id.get(project_id)
+            if project is not None:
+                workflow_to_projects[workflow_id].add(project)
 
         return workflow_to_projects

This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

Comment thread src/sentry/api/serializers/models/rule.py Outdated
@kcons kcons enabled auto-merge (squash) March 31, 2026 20:25
@github-actions
Copy link
Copy Markdown
Contributor

Backend Test Failures

Failures on 94be229 in this run:

tests/sentry/api/serializers/test_rule.py::WorkflowRuleSerializerTest::test_fetch_workflow_projectslog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/api/serializers/test_rule.py:207: in test_fetch_workflow_projects
    assert workflow_projects == {
E   AssertionError: assert defaultdict(<...9006796832>}}) == {<Workflow at...89006796832>}}
E     
E     Left contains 2 more items:
E     {198: {<Project at 0x7f3b810ded00: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>},
E      199: {<Project at 0x7f3b78a91570: id=4557889006796833, team_id=None, name='Exciting Sparrow', slug='exciting-sparrow', organization_id=4557889006796832>,
E            <Project at 0x7f3b810dfad0: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>}}
E     Right contains 2 more items:
E     {<Workflow at 0x7f3b762ef690: id=199, organization_id=4557889006796835>: {<Project at 0x7f3b752d2b60: id=4557889006796833, team_id=None, name='Exciting Sparrow', slug='exciting-sparrow', organization_id=4557889006796832>,
E                                                                               <Project at 0x7f3b7a06e680: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>},
E      <Workflow at 0x7f3b7858a970: id=198, organization_id=4557889006796834>: {<Project at 0x7f3b7a06e680: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>}}
E     
E     Full diff:
E     + defaultdict(<class 'set'>, {
E     +     198: {
E     - {
E     -     <Workflow at 0x7f3b762ef690: id=199, organization_id=4557889006796835>: {
E     -         <Project at 0x7f3b752d2b60: id=4557889006796833, team_id=None, name='Exciting Sparrow', slug='exciting-sparrow', organization_id=4557889006796832>,
E     -         <Project at 0x7f3b7a06e680: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>,
E     ?                           ^^ ^ ^^
E     +         <Project at 0x7f3b810ded00: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>,
E     ?                           ^^ ^ ^^
E           },
E     -     <Workflow at 0x7f3b7858a970: id=198, organization_id=4557889006796834>: {
E     +     199: {
E     +         <Project at 0x7f3b78a91570: id=4557889006796833, team_id=None, name='Exciting Sparrow', slug='exciting-sparrow', organization_id=4557889006796832>,
E     -         <Project at 0x7f3b7a06e680: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>,
E     ?                           ^ ^^^^^
E     +         <Project at 0x7f3b810dfad0: id=4557889006796832, team_id=None, name='Bar', slug='bar', organization_id=4557889006796832>,
E     ?                           ^^^^^ ^
E           },
E     - }
E     + })

@kcons kcons requested a review from a team as a code owner March 31, 2026 20:51
@kcons kcons disabled auto-merge March 31, 2026 20:53
@kcons kcons enabled auto-merge (squash) March 31, 2026 20:53
@kcons kcons merged commit e260849 into master Mar 31, 2026
64 of 65 checks passed
@kcons kcons deleted the kcons/lessq branch March 31, 2026 21:13
dashed pushed a commit that referenced this pull request Apr 1, 2026
…izer (#111942)

The previous logic had us potentially sending 100+ workflow queries,
which can add 300ms+ to the request.
By only fetching ids, we avoid considerable unnecessary work.
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants