Skip to content

Commit e936167

Browse files
authored
🔧 chore(aci): gate actions task so it doesn't fire for metric issues (#95738)
1 parent 3e457b6 commit e936167

File tree

32 files changed

+152
-58
lines changed

32 files changed

+152
-58
lines changed

src/sentry/digests/notifications.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from collections.abc import Mapping, Sequence
66
from typing import Any, NamedTuple, TypeAlias
77

8+
import sentry_sdk
9+
810
from sentry import features, tsdb
911
from sentry.digests.types import IdentifierKey, Notification, Record, RecordWithRuleObjects
1012
from sentry.eventstore.models import Event
@@ -73,13 +75,16 @@ def event_to_record(
7375
if not rules:
7476
logger.warning("Creating record for %s that does not contain any rules!", event)
7577

78+
# TODO(iamrajjoshi): The typing on this function is wrong, the type should be GroupEvent
79+
# TODO(iamrajjoshi): Creating a PR to fix this
80+
assert event.group is not None
7681
rule_ids = []
7782
identifier_key = IdentifierKey.RULE
7883
if features.has("organizations:workflow-engine-ui-links", event.organization):
7984
identifier_key = IdentifierKey.WORKFLOW
8085
for rule in rules:
8186
rule_ids.append(int(get_key_from_rule_data(rule, "workflow_id")))
82-
elif should_fire_workflow_actions(event.organization):
87+
elif should_fire_workflow_actions(event.organization, event.group.type):
8388
for rule in rules:
8489
rule_ids.append(int(get_key_from_rule_data(rule, "legacy_rule_id")))
8590
else:
@@ -168,7 +173,6 @@ def _build_digest_impl(
168173

169174

170175
def get_rules_from_workflows(project: Project, workflow_ids: set[int]) -> dict[int, Rule]:
171-
from sentry.notifications.notification_action.utils import should_fire_workflow_actions
172176

173177
rules: dict[int, Rule] = {}
174178
if not workflow_ids:
@@ -223,15 +227,20 @@ def get_rules_from_workflows(project: Project, workflow_ids: set[int]) -> dict[i
223227

224228
assert rule.project_id == project.id, "Rule must belong to Project"
225229

226-
if should_fire_workflow_actions(project.organization):
230+
try:
227231
rule.data["actions"][0]["legacy_rule_id"] = rule.id
232+
except KeyError:
233+
# This shouldn't happen, but isn't a deal breaker if it does
234+
sentry_sdk.capture_exception(
235+
Exception(f"Rule {rule.id} does not have a legacy_rule_id"),
236+
level="warning",
237+
)
228238

229239
rules[workflow_id] = rule
230240
return rules
231241

232242

233243
def build_digest(project: Project, records: Sequence[Record]) -> DigestInfo:
234-
from sentry.notifications.notification_action.utils import should_fire_workflow_actions
235244

236245
if not records:
237246
return DigestInfo({}, {}, {})
@@ -259,9 +268,15 @@ def build_digest(project: Project, records: Sequence[Record]) -> DigestInfo:
259268
group_ids = list(groups)
260269
rules = Rule.objects.in_bulk(rule_ids)
261270

262-
if should_fire_workflow_actions(project.organization):
263-
for rule in rules.values():
271+
for rule in rules.values():
272+
try:
264273
rule.data["actions"][0]["legacy_rule_id"] = rule.id
274+
except KeyError:
275+
# This shouldn't happen, but isn't a deal breaker if it does
276+
sentry_sdk.capture_exception(
277+
Exception(f"Rule {rule.id} does not have a legacy_rule_id"),
278+
level="warning",
279+
)
265280

266281
rules.update(get_rules_from_workflows(project, workflow_ids))
267282

src/sentry/incidents/action_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
DetailedIncidentSerializerResponse,
2626
IncidentSerializer,
2727
)
28+
from sentry.incidents.grouptype import MetricIssue
2829
from sentry.incidents.models.alert_rule import (
2930
AlertRuleDetectionType,
3031
AlertRuleThresholdType,
@@ -581,7 +582,7 @@ def generate_incident_trigger_email_context(
581582
),
582583
query=urlencode(alert_link_params),
583584
)
584-
elif should_fire_workflow_actions(organization):
585+
elif should_fire_workflow_actions(organization, MetricIssue.type_id):
585586
# lookup the incident_id from the open_period_identifier
586587
try:
587588
incident_group_open_period = IncidentGroupOpenPeriod.objects.get(

src/sentry/integrations/discord/message_builder/issues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def build(self, notification_uuid: str | None = None) -> dict[str, object]:
6161
rule_environment_id = self.rules[0].environment_id
6262
if features.has("organizations:workflow-engine-ui-links", self.group.organization):
6363
rule_id = int(get_key_from_rule_data(self.rules[0], "workflow_id"))
64-
elif should_fire_workflow_actions(self.group.organization):
64+
elif should_fire_workflow_actions(self.group.organization, self.group.type):
6565
rule_id = int(get_key_from_rule_data(self.rules[0], "legacy_rule_id"))
6666
else:
6767
rule_id = self.rules[0].id

src/sentry/integrations/messaging/message_builder.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def fetch_environment_name(rule_env: int) -> str | None:
107107

108108

109109
def get_rule_environment_param_from_rule(
110-
rule_id: int, rule_environment_id: int | None, organization: Organization
110+
rule_id: int, rule_environment_id: int | None, organization: Organization, type_id: int
111111
) -> dict[str, str]:
112112
params = {}
113-
if should_fire_workflow_actions(organization):
113+
if should_fire_workflow_actions(organization, type_id):
114114
if (
115115
rule_environment_id is not None
116116
and (environment_name := fetch_environment_name(rule_environment_id)) is not None
@@ -147,7 +147,9 @@ def get_title_link(
147147
# add in rule id if we have it
148148
if rule_id:
149149
other_params.update(
150-
get_rule_environment_param_from_rule(rule_id, rule_environment_id, group.organization)
150+
get_rule_environment_param_from_rule(
151+
rule_id, rule_environment_id, group.organization, group.type
152+
)
151153
)
152154
# hard code for issue alerts
153155
other_params["alert_rule_id"] = str(rule_id)
@@ -270,7 +272,7 @@ def build_attachment_replay_link(
270272
def build_rule_url(rule: Any, group: Group, project: Project) -> str:
271273
org_slug = group.organization.slug
272274
project_slug = project.slug
273-
if should_fire_workflow_actions(group.organization):
275+
if should_fire_workflow_actions(group.organization, group.type):
274276
rule_id = get_key_from_rule_data(rule, "legacy_rule_id")
275277
rule_url = f"/organizations/{org_slug}/alerts/rules/{project_slug}/{rule_id}/details/"
276278
else:

src/sentry/integrations/metric_alerts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ def incident_attachment_info(
222222
if notification_uuid:
223223
title_link_params["notification_uuid"] = notification_uuid
224224

225-
if should_fire_workflow_actions(organization):
225+
from sentry.incidents.grouptype import MetricIssue
226+
227+
# TODO(iamrajjoshi): This will need to be updated once we plan out Metric Alerts rollout
228+
if should_fire_workflow_actions(organization, MetricIssue.type_id):
226229
try:
227230
alert_rule_id = AlertRuleDetector.objects.values_list("alert_rule_id", flat=True).get(
228231
detector_id=alert_context.action_identifier_id

src/sentry/integrations/opsgenie/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _get_rule_urls(self, group, rules):
5959
rule_urls = []
6060
for rule in rules:
6161
rule_id = rule.id
62-
if should_fire_workflow_actions(organization):
62+
if should_fire_workflow_actions(organization, group.type):
6363
rule_id = get_key_from_rule_data(rule, "legacy_rule_id")
6464

6565
path = f"/organizations/{organization.slug}/alerts/rules/{group.project.slug}/{rule_id}/details/"

src/sentry/integrations/slack/actions/notification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def send_notification_noa(event: GroupEvent, futures: Sequence[RuleFuture]) -> N
479479
},
480480
skip_internal=False,
481481
)
482-
if should_fire_workflow_actions(self.project.organization):
482+
if should_fire_workflow_actions(self.project.organization, event.group.type):
483483
yield self.future(send_notification_noa, key=key)
484484
else:
485485
yield self.future(send_notification, key=key)

src/sentry/integrations/slack/message_builder/issues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ def build(self, notification_uuid: str | None = None) -> SlackBlock:
616616
if self.rules:
617617
if features.has("organizations:workflow-engine-ui-links", self.group.organization):
618618
rule_id = int(get_key_from_rule_data(self.rules[0], "workflow_id"))
619-
elif should_fire_workflow_actions(self.group.organization):
619+
elif should_fire_workflow_actions(self.group.organization, self.group.type):
620620
rule_id = int(get_key_from_rule_data(self.rules[0], "legacy_rule_id"))
621621
else:
622622
rule_id = self.rules[0].id

src/sentry/integrations/slack/service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ def _notify_all_threads_for_activity(
248248
parent_notifications: Generator[
249249
NotificationActionNotificationMessage | IssueAlertNotificationMessage
250250
]
251-
will_fire_workflow_actions = should_fire_workflow_actions(group.organization)
251+
will_fire_workflow_actions = should_fire_workflow_actions(
252+
group.organization, group.type
253+
)
252254
if group.issue_category == GroupCategory.UPTIME:
253255
use_open_period_start = True
254256
open_period_start = open_period_start_for_group(group)

src/sentry/integrations/slack/utils/notifications.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from sentry.incidents.charts import build_metric_alert_chart
1616
from sentry.incidents.endpoints.serializers.alert_rule import AlertRuleSerializerResponse
1717
from sentry.incidents.endpoints.serializers.incident import DetailedIncidentSerializerResponse
18+
from sentry.incidents.grouptype import MetricIssue
1819
from sentry.incidents.models.incident import IncidentStatus
1920
from sentry.incidents.typings.metric_detector import (
2021
AlertContext,
@@ -436,7 +437,8 @@ def send_incident_alert_notification(
436437
notification_uuid=notification_uuid,
437438
)
438439

439-
if should_fire_workflow_actions(organization):
440+
# TODO(iamrajjoshi): This will need to be updated once we plan out Metric Alerts rollout
441+
if should_fire_workflow_actions(organization, MetricIssue.type_id):
440442
return _handle_workflow_engine_notification(
441443
organization=organization,
442444
notification_context=notification_context,

0 commit comments

Comments
 (0)