Skip to content

Commit 60045e6

Browse files
authored
ref(integrations): skip custom priority for resolved metric alerts (#66897)
1 parent 48da227 commit 60045e6

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/sentry/integrations/opsgenie/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ def build_incident_attachment(
4747
return payload
4848

4949

50-
def attach_custom_priority(data: dict[str, Any], action: AlertRuleTriggerAction) -> dict[str, Any]:
51-
if action.sentry_app_config is None:
50+
def attach_custom_priority(
51+
data: dict[str, Any], action: AlertRuleTriggerAction, new_status: IncidentStatus
52+
) -> dict[str, Any]:
53+
if new_status == IncidentStatus.CLOSED or action.sentry_app_config is None:
5254
return data
5355

5456
priority = action.sentry_app_config.get("priority", OPSGENIE_DEFAULT_PRIORITY)
@@ -96,7 +98,7 @@ def send_incident_alert_notification(
9698
)
9799
client = install.get_keyring_client(keyid=team["id"])
98100
attachment = build_incident_attachment(incident, new_status, metric_value, notification_uuid)
99-
attachment = attach_custom_priority(attachment, action)
101+
attachment = attach_custom_priority(attachment, action, new_status)
100102

101103
try:
102104
resp = client.send_notification(attachment)

src/sentry/integrations/pagerduty/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ def build_incident_attachment(
123123
}
124124

125125

126-
def attach_custom_severity(data: dict[str, Any], action: AlertRuleTriggerAction) -> dict[str, Any]:
126+
def attach_custom_severity(
127+
data: dict[str, Any], action: AlertRuleTriggerAction, new_status: IncidentStatus
128+
) -> dict[str, Any]:
127129
# use custom severity (overrides default in build_incident_attachment)
128-
if action.sentry_app_config is None:
130+
if new_status == IncidentStatus.CLOSED or action.sentry_app_config is None:
129131
return data
130132

131133
severity = action.sentry_app_config.get("priority", None)
@@ -187,7 +189,7 @@ def send_incident_alert_notification(
187189
attachment = build_incident_attachment(
188190
incident, integration_key, new_status, metric_value, notification_uuid
189191
)
190-
attachment = attach_custom_severity(attachment, action)
192+
attachment = attach_custom_severity(attachment, action, new_status)
191193

192194
try:
193195
client.send_trigger(attachment)

tests/sentry/incidents/action_handlers/test_opsgenie.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,20 @@ def run_test(self, incident, method):
112112
status=202,
113113
)
114114
expected_payload = {}
115+
new_status = IncidentStatus.CLOSED
115116
else:
117+
new_status = IncidentStatus.CRITICAL
116118
update_incident_status(
117-
incident, IncidentStatus.CRITICAL, status_method=IncidentStatusMethod.RULE_TRIGGERED
119+
incident, new_status, status_method=IncidentStatusMethod.RULE_TRIGGERED
118120
)
119121
responses.add(
120122
responses.POST,
121123
url="https://api.opsgenie.com/v2/alerts",
122124
json={},
123125
status=202,
124126
)
125-
expected_payload = build_incident_attachment(
126-
incident, IncidentStatus(incident.status), metric_value=1000
127-
)
128-
expected_payload = attach_custom_priority(expected_payload, self.action)
127+
expected_payload = build_incident_attachment(incident, new_status, metric_value=1000)
128+
expected_payload = attach_custom_priority(expected_payload, self.action, new_status)
129129

130130
handler = OpsgenieActionHandler(self.action, incident, self.project)
131131
metric_value = 1000
@@ -230,5 +230,9 @@ def test_alert_sent_recorded(self, mock_record):
230230
def test_custom_priority(self):
231231
# default critical incident priority is P1, custom set to P3
232232
self.action.update(sentry_app_config={"priority": "P3"})
233-
234233
self.run_fire_test()
234+
235+
@responses.activate
236+
def test_custom_priority_resolve(self):
237+
self.action.update(sentry_app_config={"priority": "P3"})
238+
self.run_fire_test("resolve")

tests/sentry/incidents/action_handlers/test_pagerduty.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ def run_test(self, incident, method):
109109
)
110110
handler = PagerDutyActionHandler(self.action, incident, self.project)
111111
metric_value = 1000
112+
new_status = IncidentStatus(incident.status)
112113
with self.tasks():
113-
getattr(handler, method)(metric_value, IncidentStatus(incident.status))
114+
getattr(handler, method)(metric_value, new_status)
114115
data = responses.calls[0].request.body
115116

116117
expected_payload = build_incident_attachment(
117-
incident, self.service["integration_key"], IncidentStatus(incident.status), metric_value
118+
incident, self.service["integration_key"], new_status, metric_value
118119
)
119-
expected_payload = attach_custom_severity(expected_payload, self.action)
120+
expected_payload = attach_custom_severity(expected_payload, self.action, new_status)
120121

121122
assert json.loads(data) == expected_payload
122123

@@ -192,3 +193,8 @@ def test_custom_severity(self):
192193
# default closed incident severity is info, custom set to critical
193194
self.action.update(sentry_app_config={"priority": "critical"})
194195
self.run_fire_test()
196+
197+
@responses.activate
198+
def test_custom_severity_resolved(self):
199+
self.action.update(sentry_app_config={"priority": "critical"})
200+
self.run_fire_test("resolve")

0 commit comments

Comments
 (0)