Просмотр исходного кода

ref(integrations): skip custom priority for resolved metric alerts (#66897)

Cathy Teng 1 год назад
Родитель
Сommit
60045e6deb

+ 5 - 3
src/sentry/integrations/opsgenie/utils.py

@@ -47,8 +47,10 @@ def build_incident_attachment(
     return payload
 
 
-def attach_custom_priority(data: dict[str, Any], action: AlertRuleTriggerAction) -> dict[str, Any]:
-    if action.sentry_app_config is None:
+def attach_custom_priority(
+    data: dict[str, Any], action: AlertRuleTriggerAction, new_status: IncidentStatus
+) -> dict[str, Any]:
+    if new_status == IncidentStatus.CLOSED or action.sentry_app_config is None:
         return data
 
     priority = action.sentry_app_config.get("priority", OPSGENIE_DEFAULT_PRIORITY)
@@ -96,7 +98,7 @@ def send_incident_alert_notification(
     )
     client = install.get_keyring_client(keyid=team["id"])
     attachment = build_incident_attachment(incident, new_status, metric_value, notification_uuid)
-    attachment = attach_custom_priority(attachment, action)
+    attachment = attach_custom_priority(attachment, action, new_status)
 
     try:
         resp = client.send_notification(attachment)

+ 5 - 3
src/sentry/integrations/pagerduty/utils.py

@@ -123,9 +123,11 @@ def build_incident_attachment(
     }
 
 
-def attach_custom_severity(data: dict[str, Any], action: AlertRuleTriggerAction) -> dict[str, Any]:
+def attach_custom_severity(
+    data: dict[str, Any], action: AlertRuleTriggerAction, new_status: IncidentStatus
+) -> dict[str, Any]:
     # use custom severity (overrides default in build_incident_attachment)
-    if action.sentry_app_config is None:
+    if new_status == IncidentStatus.CLOSED or action.sentry_app_config is None:
         return data
 
     severity = action.sentry_app_config.get("priority", None)
@@ -187,7 +189,7 @@ def send_incident_alert_notification(
     attachment = build_incident_attachment(
         incident, integration_key, new_status, metric_value, notification_uuid
     )
-    attachment = attach_custom_severity(attachment, action)
+    attachment = attach_custom_severity(attachment, action, new_status)
 
     try:
         client.send_trigger(attachment)

+ 10 - 6
tests/sentry/incidents/action_handlers/test_opsgenie.py

@@ -112,9 +112,11 @@ class OpsgenieActionHandlerTest(FireTest):
                 status=202,
             )
             expected_payload = {}
+            new_status = IncidentStatus.CLOSED
         else:
+            new_status = IncidentStatus.CRITICAL
             update_incident_status(
-                incident, IncidentStatus.CRITICAL, status_method=IncidentStatusMethod.RULE_TRIGGERED
+                incident, new_status, status_method=IncidentStatusMethod.RULE_TRIGGERED
             )
             responses.add(
                 responses.POST,
@@ -122,10 +124,8 @@ class OpsgenieActionHandlerTest(FireTest):
                 json={},
                 status=202,
             )
-            expected_payload = build_incident_attachment(
-                incident, IncidentStatus(incident.status), metric_value=1000
-            )
-            expected_payload = attach_custom_priority(expected_payload, self.action)
+            expected_payload = build_incident_attachment(incident, new_status, metric_value=1000)
+            expected_payload = attach_custom_priority(expected_payload, self.action, new_status)
 
         handler = OpsgenieActionHandler(self.action, incident, self.project)
         metric_value = 1000
@@ -230,5 +230,9 @@ class OpsgenieActionHandlerTest(FireTest):
     def test_custom_priority(self):
         # default critical incident priority is P1, custom set to P3
         self.action.update(sentry_app_config={"priority": "P3"})
-
         self.run_fire_test()
+
+    @responses.activate
+    def test_custom_priority_resolve(self):
+        self.action.update(sentry_app_config={"priority": "P3"})
+        self.run_fire_test("resolve")

+ 9 - 3
tests/sentry/incidents/action_handlers/test_pagerduty.py

@@ -109,14 +109,15 @@ class PagerDutyActionHandlerTest(FireTest):
         )
         handler = PagerDutyActionHandler(self.action, incident, self.project)
         metric_value = 1000
+        new_status = IncidentStatus(incident.status)
         with self.tasks():
-            getattr(handler, method)(metric_value, IncidentStatus(incident.status))
+            getattr(handler, method)(metric_value, new_status)
         data = responses.calls[0].request.body
 
         expected_payload = build_incident_attachment(
-            incident, self.service["integration_key"], IncidentStatus(incident.status), metric_value
+            incident, self.service["integration_key"], new_status, metric_value
         )
-        expected_payload = attach_custom_severity(expected_payload, self.action)
+        expected_payload = attach_custom_severity(expected_payload, self.action, new_status)
 
         assert json.loads(data) == expected_payload
 
@@ -192,3 +193,8 @@ class PagerDutyActionHandlerTest(FireTest):
         # default closed incident severity is info, custom set to critical
         self.action.update(sentry_app_config={"priority": "critical"})
         self.run_fire_test()
+
+    @responses.activate
+    def test_custom_severity_resolved(self):
+        self.action.update(sentry_app_config={"priority": "critical"})
+        self.run_fire_test("resolve")