Skip to content

Commit e61a46b

Browse files
authored
fix(aci): Make associate_new_group_with_detector more robust (#103418)
Since we know Detectors can go away (even before we have a chance to process a new occurrence) and we have a convention for representing that with DetectorGroup, update associate_new_group_with_detector accordingly.
1 parent 2faaca2 commit e61a46b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/sentry/workflow_engine/processors/detector.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ def associate_new_group_with_detector(group: Group, detector_id: int | None = No
382382
},
383383
)
384384
return False
385+
386+
# Check if the detector exists. If not, create DetectorGroup with null detector_id
387+
# to make it clear that we were associated with a detector that no longer exists.
388+
if not Detector.objects.filter(id=detector_id).exists():
389+
metrics.incr(
390+
"workflow_engine.associate_new_group_with_detector",
391+
tags={"group_type": group.type, "result": "detector_missing"},
392+
)
393+
logger.warning(
394+
"associate_new_group_with_detector_detector_missing",
395+
extra={
396+
"group_id": group.id,
397+
"group_type": group.type,
398+
"detector_id": detector_id,
399+
},
400+
)
401+
DetectorGroup.objects.get_or_create(
402+
detector_id=None,
403+
group_id=group.id,
404+
)
405+
return True
406+
385407
DetectorGroup.objects.get_or_create(
386408
detector_id=detector_id,
387409
group_id=group.id,

tests/sentry/workflow_engine/processors/test_detector.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,3 +1058,15 @@ def test_feedback_group_returns_false(self) -> None:
10581058
group = self.create_group(project=self.project, type=FeedbackGroup.type_id)
10591059
assert not associate_new_group_with_detector(group)
10601060
assert not DetectorGroup.objects.filter(group_id=group.id).exists()
1061+
1062+
def test_deleted_detector_creates_null_association(self) -> None:
1063+
group = self.create_group(project=self.project, type=MetricIssue.type_id)
1064+
deleted_detector_id = self.metric_detector.id
1065+
1066+
self.metric_detector.delete()
1067+
1068+
assert associate_new_group_with_detector(group, deleted_detector_id)
1069+
1070+
detector_group = DetectorGroup.objects.get(group_id=group.id)
1071+
assert detector_group.detector_id is None
1072+
assert detector_group.group_id == group.id

0 commit comments

Comments
 (0)