Skip to content

Conversation

@cathteng
Copy link
Member

@cathteng cathteng commented Nov 14, 2025

Backfill metric issue DetectorGroup rows.

If a metric issue's detector no longer exists, we create a row with a null Detector.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 14, 2025
Detector = apps.get_model("workflow_engine", "Detector")

for group in RangeQuerySetWrapper(
Group.objects.filter(type=8001, detectorgroup__isnull=True)
Copy link
Member

Choose a reason for hiding this comment

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

I think this will end up being super slow and timing out using RangeQuerySetWrapper. Probably you just need to perform the detectorgroup query separately in the loop.

If you're concerned about performance, you could fetch groups in batches of 100 and fetch the detectorgroup rows in bulk.

Alternatively, there are only 26k rows with type 8001 in general. You could just bring them all into memory and skip the range wrapper completely

@github-actions
Copy link
Contributor

This PR has a migration; here is the generated SQL for src/sentry/workflow_engine/migrations/0098_detectorgroup_detector_set_null.py src/sentry/workflow_engine/migrations/0099_backfill_metric_issue_detectorgroup.py

for 0098_detectorgroup_detector_set_null in workflow_engine

--
-- Alter field detector on detectorgroup
--
SET CONSTRAINTS "workflow_engine_dete_detector_id_0c22c088_fk_workflow_" IMMEDIATE; ALTER TABLE "workflow_engine_detectorgroup" DROP CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_";
ALTER TABLE "workflow_engine_detectorgroup" ALTER COLUMN "detector_id" DROP NOT NULL;
ALTER TABLE "workflow_engine_detectorgroup" ADD CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_" FOREIGN KEY ("detector_id") REFERENCES "workflow_engine_detector" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
ALTER TABLE "workflow_engine_detectorgroup" VALIDATE CONSTRAINT "workflow_engine_dete_detector_id_0c22c088_fk_workflow_";

for 0099_backfill_metric_issue_detectorgroup in workflow_engine

--
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL

@codecov
Copy link

codecov bot commented Nov 14, 2025

Codecov Report

❌ Patch coverage is 90.74074% with 5 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ations/0099_backfill_metric_issue_detectorgroup.py 90.74% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #103399      +/-   ##
===========================================
- Coverage   80.67%    80.59%   -0.09%     
===========================================
  Files        9242      9243       +1     
  Lines      394806    394860      +54     
  Branches    25147     25147              
===========================================
- Hits       318526    318238     -288     
- Misses      75833     76175     +342     
  Partials      447       447              

@getsentry getsentry deleted a comment from semgrep-code-getsentry bot Nov 14, 2025
@getsentry getsentry deleted a comment from semgrep-code-getsentry bot Nov 14, 2025
Base automatically changed from cathy/aci/detectorgroup-detector-set-null to master November 14, 2025 21:53
@cathteng cathteng force-pushed the cathy/aci/backfill-detectorgroup-metric-issue branch from a47440d to 05b3d5b Compare November 14, 2025 21:53
@cathteng cathteng marked this pull request as ready for review November 14, 2025 21:54
@cathteng cathteng requested review from a team as code owners November 14, 2025 21:54
@github-actions
Copy link
Contributor

This PR has a migration; here is the generated SQL for src/sentry/workflow_engine/migrations/0099_backfill_metric_issue_detectorgroup.py

for 0099_backfill_metric_issue_detectorgroup in workflow_engine

--
-- Raw Python operation
--
-- THIS OPERATION CANNOT BE WRITTEN AS SQL

@cathteng cathteng force-pushed the cathy/aci/backfill-detectorgroup-metric-issue branch from f33ed70 to e7f3c54 Compare November 14, 2025 22:22
]


def get_oldest_or_latest_event(
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a "copied from group.py with minimal edits" comment, otherwise the flexibility feels a bit unnecessary.

)
continue

DetectorGroup.objects.create(
Copy link
Member

Choose a reason for hiding this comment

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

I think technically Group is observable before DetectorGroup for a short period, so a get_or_create might be safer, but I doubt it'll come up.

Copy link
Member Author

Choose a reason for hiding this comment

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

It should error in that case because DetectorGroup is now unique on Group, but we can rerun in that case

@cathteng cathteng requested a review from a team as a code owner November 14, 2025 23:30
DetectorGroup.objects.create(
group_id=group.id,
detector_id=None,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Safe concurrent object creation prevents errors.

Using DetectorGroup.objects.create() without handling potential race conditions can cause IntegrityError since DetectorGroup has a unique constraint on group. If another process creates a DetectorGroup for the same group between the filter check and the create call, the migration will fail. Using get_or_create() would safely handle this scenario.

Fix in Cursor Fix in Web

Copy link
Contributor

@mifu67 mifu67 left a comment

Choose a reason for hiding this comment

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

Backfill logic for metric issues looks good to me! With only 827 entries, this should be pretty fast.

@cathteng cathteng merged commit 5d4cb34 into master Nov 18, 2025
66 of 67 checks passed
@cathteng cathteng deleted the cathy/aci/backfill-detectorgroup-metric-issue branch November 18, 2025 15:46
@sentry
Copy link

sentry bot commented Nov 18, 2025

Issues attributed to commits in this pull request

This pull request was merged and Sentry observed the following issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants