Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
alertmanager: Tolerate alerts missing the generatorURL field.
Aman Agrawal6c4432f7c1d7
diff --git a/zerver/webhooks/alertmanager/fixtures/single_alert_no_url.json b/zerver/webhooks/alertmanager/fixtures/single_alert_no_url.jsonnew file mode 100644index 00000000000..6c5499e243c--- /dev/null+++ b/zerver/webhooks/alertmanager/fixtures/single_alert_no_url.json@@ -0,0 +1,33 @@+{+ "receiver": "ops-zulip",+ "status": "firing",+ "groupLabels": {},+ "commonLabels": {+ "alertname": "High CPU Temperature",+ "severity": "critical"+ },+ "commonAnnotations": {+ "summary": "High CPU core temperature"+ },+ "externalURL": "http://ops1:9093",+ "version": "4",+ "groupKey": "{}/{}:{}",+ "alerts": [+ {+ "status": "firing",+ "labels": {+ "alertname": "High CPU Temperature",+ "host": "andromeda",+ "severity": "critical"+ },+ "annotations": {+ "description": "CPU core temperature is 34.75C",+ "topic": "andromeda",+ "summary": "High CPU core temperature"+ },+ "startsAt": "2020-02-06T00:17:58.432911368Z",+ "endsAt": "0001-01-01T00:00:00Z",+ "fingerprint": "a1bc48475b20e73d"+ }+ ]+}diff --git a/zerver/webhooks/alertmanager/tests.py b/zerver/webhooks/alertmanager/tests.pyindex 6e9fda076ba..170e815f696 100644--- a/zerver/webhooks/alertmanager/tests.py+++ b/zerver/webhooks/alertmanager/tests.py@@ -31,3 +31,14 @@ class AlertmanagerHookTests(WebhookTestCase):expected_message,"application/json",)++ def test_single_alert_no_generator_url(self) -> None:+ expected_topic_name = "andromeda"+ expected_message = ":alert: **FIRING** CPU core temperature is 34.75C"++ self.check_webhook(+ "single_alert_no_url",+ expected_topic_name,+ expected_message,+ "application/json",+ )diff --git a/zerver/webhooks/alertmanager/view.py b/zerver/webhooks/alertmanager/view.pyindex cea1183262a..cbc82a80b94 100644--- a/zerver/webhooks/alertmanager/view.py+++ b/zerver/webhooks/alertmanager/view.py@@ -33,9 +33,11 @@ def api_alertmanager_webhook(desc_field, annotations.get(desc_field, f"<missing field: {desc_field}>")).tame(check_string)- url = alert["generatorURL"].tame(check_string).replace("tab=1", "tab=0")-- body = f"{desc} ([source]({url}))"+ if "generatorURL" in alert:+ url = alert["generatorURL"].tame(check_string).replace("tab=1", "tab=0")+ body = f"{desc} ([source]({url}))"+ else:+ body = descif name not in topics:topics[name] = {"firing": [], "resolved": []}topics[name][alert["status"].tame(check_string)].append(body)