Skip to content

Commit

Permalink
fix(opsgenie): Actually resolve metric alerts (#54314)
Browse files Browse the repository at this point in the history
Fix an issue where metric alert resolution payload was being overwritten
due to bad control flow.
  • Loading branch information
mifu67 authored Aug 7, 2023
1 parent ac9964f commit 497d745
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/sentry/integrations/opsgenie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def build_incident_attachment(incident: Incident, new_status: IncidentStatus, me
alert_key = f"incident_{incident.organization_id}_{incident.identifier}"
if new_status == IncidentStatus.CLOSED:
payload = {"identifier": alert_key}
return payload

priority = "P1"
if new_status == IncidentStatus.WARNING:
priority = "P2"
Expand Down
33 changes: 24 additions & 9 deletions tests/sentry/incidents/action_handlers/test_opsgenie.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,36 @@ def test_build_incident_attachment(self):
def run_test(self, incident, method):
from sentry.integrations.opsgenie.utils import build_incident_attachment

responses.add(
responses.POST,
url="https://api.opsgenie.com/v2/alerts",
json={},
status=202,
)
alias = f"incident_{incident.organization_id}_{incident.identifier}"

if method == "resolve":
responses.add(
responses.POST,
url=f"https://api.opsgenie.com/v2/alerts/{alias}/acknowledge?identifierType=alias",
json={},
status=202,
)
expected_payload = {}
else:
update_incident_status(
incident, IncidentStatus.CRITICAL, status_method=IncidentStatusMethod.RULE_TRIGGERED
)
responses.add(
responses.POST,
url="https://api.opsgenie.com/v2/alerts",
json={},
status=202,
)
expected_payload = build_incident_attachment(
incident, IncidentStatus(incident.status), metric_value=1000
)
handler = OpsgenieActionHandler(self.action, incident, self.project)
metric_value = 1000
with self.tasks():
getattr(handler, method)(metric_value, IncidentStatus(incident.status))
data = responses.calls[0].request.body

assert json.loads(data) == build_incident_attachment(
incident, IncidentStatus(incident.status), metric_value
)
assert json.loads(data) == expected_payload

@responses.activate
def test_fire_metric_alert(self):
Expand Down

0 comments on commit 497d745

Please sign in to comment.