Skip to content

Commit c0f5bec

Browse files
authored
🐛 fix(aci): update legacy metric alert action firing logic & fix ff check for activity notifs (#97355)
1 parent 74cdf17 commit c0f5bec

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

src/sentry/incidents/tasks.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def handle_trigger_action(
6565
metric_value: int | None = None,
6666
**kwargs: Any,
6767
) -> None:
68+
from sentry.incidents.grouptype import MetricIssue
69+
from sentry.notifications.notification_action.utils import should_fire_workflow_actions
70+
6871
try:
6972
action = AlertRuleTriggerAction.objects.select_related(
7073
"alert_rule_trigger", "alert_rule_trigger__alert_rule"
@@ -92,20 +95,22 @@ def handle_trigger_action(
9295
if notification_uuid is None:
9396
metrics.incr("incidents.alert_rules.action.incident_activity_missing")
9497

95-
metrics.incr(
96-
"incidents.alert_rules.action.{}.{}".format(
97-
AlertRuleTriggerAction.Type(action.type).name.lower(), method
98+
# We should only fire using the legacy registry if we are not using the workflow engine
99+
if not should_fire_workflow_actions(incident.organization, MetricIssue.type_id):
100+
metrics.incr(
101+
"incidents.alert_rules.action.{}.{}".format(
102+
AlertRuleTriggerAction.Type(action.type).name.lower(), method
103+
)
98104
)
99-
)
100105

101-
getattr(action, method)(
102-
action,
103-
incident,
104-
project,
105-
metric_value=metric_value,
106-
new_status=IncidentStatus(new_status),
107-
notification_uuid=notification_uuid,
108-
)
106+
getattr(action, method)(
107+
action,
108+
incident,
109+
project,
110+
metric_value=metric_value,
111+
new_status=IncidentStatus(new_status),
112+
notification_uuid=notification_uuid,
113+
)
109114

110115

111116
@instrumented_task(

src/sentry/workflow_engine/handlers/workflow/workflow_status_update_handler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from sentry import features
21
from sentry.issues.status_change_consumer import group_status_update_registry
32
from sentry.issues.status_change_message import StatusChangeMessageData
43
from sentry.models.activity import Activity
@@ -20,6 +19,9 @@ def workflow_status_update_handler(
2019
Since this handler is called in process for the activity, we want
2120
to queue a task to process workflows asynchronously.
2221
"""
22+
23+
from sentry.notifications.notification_action.utils import should_fire_workflow_actions
24+
2325
metrics.incr(
2426
"workflow_engine.tasks.process_workflows.activity_update",
2527
tags={"activity_type": activity.type},
@@ -36,11 +38,8 @@ def workflow_status_update_handler(
3638
metrics.incr("workflow_engine.tasks.error.no_detector_id")
3739
return
3840

39-
if features.has(
40-
"organizations:workflow-engine-single-process-metric-issues", group.organization
41-
) or features.has(
42-
"organizations:workflow-engine-process-metric-issue-workflows", group.organization
43-
):
41+
# We should only fire actions for activity updates if we should be firing actions
42+
if should_fire_workflow_actions(group.organization, group.type):
4443
process_workflow_activity.delay(
4544
activity_id=activity.id,
4645
group_id=group.id,

tests/sentry/incidents/test_tasks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from sentry.incidents.tasks import handle_trigger_action
1616
from sentry.testutils.cases import TestCase
1717
from sentry.testutils.helpers.alert_rule import TemporaryAlertRuleTriggerActionRegistry
18+
from sentry.testutils.helpers.features import with_feature
1819
from sentry.testutils.skips import requires_kafka, requires_snuba
1920

2021
pytestmark = [pytest.mark.sentry_metrics, requires_snuba, requires_kafka]
@@ -99,3 +100,24 @@ def test(self) -> None:
99100
metric_value=metric_value,
100101
notification_uuid=str(activity.notification_uuid),
101102
)
103+
104+
@with_feature("organizations:workflow-engine-single-process-metric-issues")
105+
def test_when_workflow_engine_is_enabled(self) -> None:
106+
with TemporaryAlertRuleTriggerActionRegistry.registry_patched():
107+
mock_handler = Mock()
108+
AlertRuleTriggerAction.register_type("email", AlertRuleTriggerAction.Type.EMAIL, [])(
109+
mock_handler
110+
)
111+
incident = self.create_incident()
112+
metric_value = 1234
113+
with self.tasks():
114+
handle_trigger_action.delay(
115+
self.action.id,
116+
incident.id,
117+
self.project.id,
118+
"fire",
119+
IncidentStatus.CRITICAL.value,
120+
metric_value=metric_value,
121+
)
122+
# We should not fire the action if the workflow engine is enabled
123+
mock_handler.assert_not_called()

tests/sentry/workflow_engine/test_task.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sentry_sdk
44
from google.api_core.exceptions import RetryError
55

6+
from sentry.incidents.grouptype import MetricIssue
67
from sentry.issues.status_change_consumer import process_status_change_message, update_status
78
from sentry.issues.status_change_message import StatusChangeMessageData
89
from sentry.models.activity import Activity
@@ -91,10 +92,10 @@ def test__feature_flag(self) -> None:
9192
workflow_status_update_handler(group, message, activity)
9293
mock_delay.assert_not_called()
9394

94-
@with_feature("organizations:workflow-engine-process-metric-issue-workflows")
95+
@with_feature("organizations:workflow-engine-single-process-metric-issues")
9596
def test(self) -> None:
9697
detector = self.create_detector(project=self.project)
97-
group = self.create_group(project=self.project)
98+
group = self.create_group(project=self.project, type=MetricIssue.type_id)
9899
activity = Activity(
99100
project=self.project,
100101
group=group,
@@ -124,15 +125,15 @@ def test(self) -> None:
124125

125126
class TestProcessWorkflowActivity(TestCase):
126127
def setUp(self) -> None:
127-
self.group = self.create_group(project=self.project)
128+
self.group = self.create_group(project=self.project, type=MetricIssue.type_id)
128129
self.activity = Activity(
129130
project=self.project,
130131
group=self.group,
131132
type=ActivityType.SET_RESOLVED.value,
132133
data={"fingerprint": ["test_fingerprint"]},
133134
)
134135
self.activity.save()
135-
self.detector = self.create_detector()
136+
self.detector = self.create_detector(type=MetricIssue.slug)
136137

137138
def test_process_workflow_activity__no_workflows(self) -> None:
138139
with mock.patch(
@@ -208,7 +209,7 @@ def test_process_workflow_activity(self, mock_filter_actions: mock.MagicMock) ->
208209

209210
mock_filter_actions.assert_called_once_with({self.action_group}, expected_event_data)
210211

211-
@with_feature("organizations:workflow-engine-process-metric-issue-workflows")
212+
@with_feature("organizations:workflow-engine-single-process-metric-issues")
212213
@mock.patch("sentry.workflow_engine.tasks.workflows.metrics.incr")
213214
def test__e2e__issue_plat_to_processed(self, mock_incr: mock.MagicMock) -> None:
214215
self.message = StatusChangeMessageData(
@@ -248,6 +249,7 @@ def test__e2e__issue_plat_to_processed(self, mock_incr: mock.MagicMock) -> None:
248249
sample_rate=1.0,
249250
)
250251

252+
@with_feature("organizations:workflow-engine-single-process-metric-issues")
251253
@with_feature("organizations:workflow-engine-process-metric-issue-workflows")
252254
@mock.patch("sentry.issues.status_change_consumer.get_group_from_fingerprint")
253255
@mock.patch("sentry.workflow_engine.tasks.workflows.metrics.incr")

0 commit comments

Comments
 (0)