snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
This commit is contained in:
0
zerver/webhooks/librato/__init__.py
Normal file
0
zerver/webhooks/librato/__init__.py
Normal file
27
zerver/webhooks/librato/doc.md
Normal file
27
zerver/webhooks/librato/doc.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Zulip AppOptics/Librato integration
|
||||
|
||||
Get Zulip notifications for your AppOptics/Librato alerts!
|
||||
|
||||
{start_tabs}
|
||||
|
||||
1. {!create-an-incoming-webhook.md!}
|
||||
|
||||
1. {!generate-webhook-url-basic.md!}
|
||||
|
||||
1. Go to your AppOptics/Librato homepage, and click on **Settings**.
|
||||
Select **Notification Services**, and click **Webhooks**.
|
||||
|
||||
1. Set **Title** to a title of your choice, such as `Zulip`. Set **URL**
|
||||
to the URL generated above, and click **Add**. When you create a
|
||||
new **Alert**, you can enable this webhook under the **Notification
|
||||
Services** tab.
|
||||
|
||||
{end_tabs}
|
||||
|
||||
{!congrats.md!}
|
||||
|
||||

|
||||
|
||||
### Related documentation
|
||||
|
||||
{!webhooks-url-specification.md!}
|
||||
56
zerver/webhooks/librato/fixtures/alert.json
Normal file
56
zerver/webhooks/librato/fixtures/alert.json
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"alert": {
|
||||
"id": 6294535,
|
||||
"name": "alert.name",
|
||||
"runbook_url": "http://www.google.pl",
|
||||
"version": 2,
|
||||
"description": "descrription"
|
||||
},
|
||||
"account": "lizonr@gmail.com",
|
||||
"trigger_time": 1459415502,
|
||||
"conditions": [
|
||||
{
|
||||
"id": 3543146,
|
||||
"type": "below",
|
||||
"threshold": 44,
|
||||
"summary_function": "sum",
|
||||
"duration": 300
|
||||
},
|
||||
{
|
||||
"id": 3651148,
|
||||
"type": "absent",
|
||||
"summary_function": "average",
|
||||
"duration": 300
|
||||
},
|
||||
{
|
||||
"id": 3651902,
|
||||
"type": "above",
|
||||
"threshold": 9,
|
||||
"summary_function": "derivative",
|
||||
"duration": 300
|
||||
}
|
||||
],
|
||||
"violations": {
|
||||
"test-source": [
|
||||
{
|
||||
"metric": "librato.cpu.percent.idle",
|
||||
"value": 2,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3543146
|
||||
},
|
||||
{
|
||||
"metric": "librato.swap.swap.cached",
|
||||
"value": 42,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3651148
|
||||
},
|
||||
{
|
||||
"metric": "librato.swap.swap.cached",
|
||||
"value": 51,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3651902
|
||||
}
|
||||
]
|
||||
},
|
||||
"triggered_by_user_test": true
|
||||
}
|
||||
11
zerver/webhooks/librato/fixtures/alert_cleared.json
Normal file
11
zerver/webhooks/librato/fixtures/alert_cleared.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"alert": {
|
||||
"id": 6309313,
|
||||
"name": "Alert_name",
|
||||
"runbook_url": "",
|
||||
"version": 2
|
||||
},
|
||||
"account": "mail@gmail.com",
|
||||
"trigger_time": 1460466704,
|
||||
"clear": "normal"
|
||||
}
|
||||
2
zerver/webhooks/librato/fixtures/bad.json
Normal file
2
zerver/webhooks/librato/fixtures/bad.json
Normal file
@@ -0,0 +1,2 @@
|
||||
{
|
||||
"alert": {
|
||||
56
zerver/webhooks/librato/fixtures/bad_msg_type.json
Normal file
56
zerver/webhooks/librato/fixtures/bad_msg_type.json
Normal file
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"alert": {
|
||||
"id": 6294535,
|
||||
"name": "alert.name",
|
||||
"runbook_url": "http://www.google.pl",
|
||||
"version": 2,
|
||||
"description": "descrription"
|
||||
},
|
||||
"account": "lizonr@gmail.com",
|
||||
"trigger_time": 1459415502,
|
||||
"conditions": [
|
||||
{
|
||||
"id": 3543146,
|
||||
"type": "below",
|
||||
"threshold": 44,
|
||||
"summary_function": "sum",
|
||||
"duration": 300
|
||||
},
|
||||
{
|
||||
"id": 3651148,
|
||||
"type": "absent",
|
||||
"summary_function": "average",
|
||||
"duration": 300
|
||||
},
|
||||
{
|
||||
"id": 3651902,
|
||||
"type": "above",
|
||||
"threshold": 9,
|
||||
"summary_function": "derivative",
|
||||
"duration": 300
|
||||
}
|
||||
],
|
||||
"violations2": {
|
||||
"test-source": [
|
||||
{
|
||||
"metric": "librato.cpu.percent.idle",
|
||||
"value": 2,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3543146
|
||||
},
|
||||
{
|
||||
"metric": "librato.swap.swap.cached",
|
||||
"value": 42,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3651148
|
||||
},
|
||||
{
|
||||
"metric": "librato.swap.swap.cached",
|
||||
"value": 51,
|
||||
"recorded_at": 1459415502,
|
||||
"condition_violated": 3651902
|
||||
}
|
||||
]
|
||||
},
|
||||
"triggered_by_user_test": true
|
||||
}
|
||||
15
zerver/webhooks/librato/fixtures/snapshot.json
Normal file
15
zerver/webhooks/librato/fixtures/snapshot.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"attachments": [
|
||||
{
|
||||
"author_name": "Hamlet",
|
||||
"author_link": "mailto:hamlet@zulip .com",
|
||||
"author_icon": "https://secure.gravatar.com/avatar/99ca91cc7c0d8ef72fbd69a03a871a12?s=32&d=mm",
|
||||
"title": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409",
|
||||
"title_link": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409",
|
||||
"fallback": "https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409 by Rafal: http://snapshots.librato.com/chart/nr5l3n0c-82162.png",
|
||||
"image_url": "http://snapshots.librato.com/chart/nr5l3n0c-82162.png",
|
||||
"text": null,
|
||||
"color": "#0881AE"
|
||||
}
|
||||
]
|
||||
}
|
||||
55
zerver/webhooks/librato/fixtures/three_conditions_alert.json
Normal file
55
zerver/webhooks/librato/fixtures/three_conditions_alert.json
Normal file
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"alert": {
|
||||
"id": 6294535,
|
||||
"name": "TooHighTemperature",
|
||||
"runbook_url": "http://www.use.water.pl",
|
||||
"version": 2,
|
||||
"description": "Measurement of temperature in your computer"
|
||||
},
|
||||
"account": "lizonr@gmail.com",
|
||||
"trigger_time": 1460407214,
|
||||
"conditions": [
|
||||
{
|
||||
"id": 4158783,
|
||||
"type": "above",
|
||||
"threshold": 4,
|
||||
"summary_function": "absolute_value",
|
||||
"duration": 300
|
||||
},
|
||||
{
|
||||
"id": 4299396,
|
||||
"type": "above",
|
||||
"threshold": 99,
|
||||
"summary_function": "max"
|
||||
},
|
||||
{
|
||||
"id": 3651148,
|
||||
"type": "absent",
|
||||
"summary_function": "average",
|
||||
"duration": 60
|
||||
}
|
||||
],
|
||||
"violations": {
|
||||
"test-source": [
|
||||
{
|
||||
"metric": "collectd.interface.eth0.if_octets.tx",
|
||||
"value": 46,
|
||||
"recorded_at": 1460407214,
|
||||
"condition_violated": 4158783
|
||||
},
|
||||
{
|
||||
"metric": "collectd.load.load.longterm",
|
||||
"value": 141,
|
||||
"recorded_at": 1460407214,
|
||||
"condition_violated": 4299396
|
||||
},
|
||||
{
|
||||
"metric": "librato.swap.swap.cached",
|
||||
"value": 42,
|
||||
"recorded_at": 1460407214,
|
||||
"condition_violated": 3651148
|
||||
}
|
||||
]
|
||||
},
|
||||
"triggered_by_user_test": true
|
||||
}
|
||||
90
zerver/webhooks/librato/tests.py
Normal file
90
zerver/webhooks/librato/tests.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from typing_extensions import override
|
||||
|
||||
from zerver.lib.test_classes import WebhookTestCase
|
||||
|
||||
|
||||
class LibratoHookTests(WebhookTestCase):
|
||||
IS_ATTACHMENT = False
|
||||
|
||||
@override
|
||||
def get_body(self, fixture_name: str) -> str:
|
||||
if self.IS_ATTACHMENT:
|
||||
return self.webhook_fixture_data("librato", fixture_name, file_type="json")
|
||||
return urlencode(
|
||||
{"payload": self.webhook_fixture_data("librato", fixture_name, file_type="json")}
|
||||
)
|
||||
|
||||
def test_alert_message_with_default_topic(self) -> None:
|
||||
expected_topic_name = "Alert alert.name"
|
||||
expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>."
|
||||
self.check_webhook(
|
||||
"alert",
|
||||
expected_topic_name,
|
||||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
|
||||
def test_alert_message_with_custom_topic(self) -> None:
|
||||
custom_topic_name = "custom_name"
|
||||
self.url = self.build_webhook_url(topic=custom_topic_name)
|
||||
expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.google.pl):\n * Metric `librato.cpu.percent.idle`, sum was below 44 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 300s, recorded at <time:2016-03-31T09:11:42+00:00>.\n * Metric `librato.swap.swap.cached`, derivative was above 9 by 300s, recorded at <time:2016-03-31T09:11:42+00:00>."
|
||||
self.check_webhook(
|
||||
"alert",
|
||||
custom_topic_name,
|
||||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
|
||||
def test_three_conditions_alert_message(self) -> None:
|
||||
expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6294535) has triggered! [Reaction steps](http://www.use.water.pl):\n * Metric `collectd.interface.eth0.if_octets.tx`, absolute_value was above 4 by 300s, recorded at <time:2016-04-11T20:40:14+00:00>.\n * Metric `collectd.load.load.longterm`, max was above 99, recorded at <time:2016-04-11T20:40:14+00:00>.\n * Metric `librato.swap.swap.cached`, average was absent by 60s, recorded at <time:2016-04-11T20:40:14+00:00>."
|
||||
expected_topic_name = "Alert TooHighTemperature"
|
||||
self.check_webhook(
|
||||
"three_conditions_alert",
|
||||
expected_topic_name,
|
||||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
|
||||
def test_alert_clear(self) -> None:
|
||||
expected_topic_name = "Alert Alert_name"
|
||||
expected_message = "Alert [alert_name](https://metrics.librato.com/alerts#/6309313) has cleared at <time:2016-04-12T13:11:44+00:00>!"
|
||||
self.check_webhook(
|
||||
"alert_cleared",
|
||||
expected_topic_name,
|
||||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
|
||||
def test_snapshot(self) -> None:
|
||||
self.IS_ATTACHMENT = True
|
||||
expected_topic_name = "Snapshots"
|
||||
expected_message = "**Hamlet** sent a [snapshot](http://snapshots.librato.com/chart/nr5l3n0c-82162.png) of [metric](https://metrics.librato.com/s/spaces/167315/explore/1731491?duration=72039&end_time=1460569409)."
|
||||
self.check_webhook(
|
||||
"snapshot",
|
||||
expected_topic_name,
|
||||
expected_message,
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
self.IS_ATTACHMENT = False
|
||||
|
||||
def test_bad_request(self) -> None:
|
||||
with self.assertRaises(AssertionError) as e:
|
||||
self.check_webhook(
|
||||
"bad",
|
||||
"",
|
||||
"",
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertIn("Malformed JSON input", e.exception.args[0])
|
||||
|
||||
def test_bad_msg_type(self) -> None:
|
||||
with self.assertRaises(AssertionError) as e:
|
||||
self.check_webhook(
|
||||
"bad_msg_type",
|
||||
"",
|
||||
"",
|
||||
content_type="application/x-www-form-urlencoded",
|
||||
)
|
||||
self.assertIn("Unexpected message type", e.exception.args[0])
|
||||
183
zerver/webhooks/librato/view.py
Normal file
183
zerver/webhooks/librato/view.py
Normal file
@@ -0,0 +1,183 @@
|
||||
from collections.abc import Callable, Mapping
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.translation import gettext as _
|
||||
from pydantic import Json
|
||||
|
||||
from zerver.decorator import webhook_view
|
||||
from zerver.lib.exceptions import JsonableError
|
||||
from zerver.lib.response import json_success
|
||||
from zerver.lib.timestamp import datetime_to_global_time, timestamp_to_datetime
|
||||
from zerver.lib.typed_endpoint import typed_endpoint
|
||||
from zerver.lib.webhooks.common import check_send_webhook_message
|
||||
from zerver.models import UserProfile
|
||||
|
||||
ALERT_CLEAR = "clear"
|
||||
ALERT_VIOLATION = "violations"
|
||||
SNAPSHOT = "image_url"
|
||||
|
||||
|
||||
class LibratoWebhookParser:
|
||||
ALERT_URL_TEMPLATE = "https://metrics.librato.com/alerts#/{alert_id}"
|
||||
|
||||
def __init__(self, payload: Mapping[str, Any], attachments: list[dict[str, Any]]) -> None:
|
||||
self.payload = payload
|
||||
self.attachments = attachments
|
||||
|
||||
def generate_alert_url(self, alert_id: int) -> str:
|
||||
return self.ALERT_URL_TEMPLATE.format(alert_id=alert_id)
|
||||
|
||||
def parse_alert(self) -> tuple[int, str, str, str]:
|
||||
alert = self.payload["alert"]
|
||||
alert_id = alert["id"]
|
||||
return alert_id, alert["name"], self.generate_alert_url(alert_id), alert["runbook_url"]
|
||||
|
||||
def parse_condition(self, condition: dict[str, Any]) -> tuple[str, str, str, str]:
|
||||
summary_function = condition["summary_function"]
|
||||
threshold = condition.get("threshold", "")
|
||||
condition_type = condition["type"]
|
||||
duration = condition.get("duration", "")
|
||||
return summary_function, threshold, condition_type, duration
|
||||
|
||||
def parse_violation(self, violation: dict[str, Any]) -> tuple[str, str]:
|
||||
metric_name = violation["metric"]
|
||||
recorded_at = datetime_to_global_time(timestamp_to_datetime(violation["recorded_at"]))
|
||||
return metric_name, recorded_at
|
||||
|
||||
def parse_conditions(self) -> list[dict[str, Any]]:
|
||||
conditions = self.payload["conditions"]
|
||||
return conditions
|
||||
|
||||
def parse_violations(self) -> list[dict[str, Any]]:
|
||||
violations = self.payload["violations"]["test-source"]
|
||||
return violations
|
||||
|
||||
def parse_snapshot(self, snapshot: dict[str, Any]) -> tuple[str, str, str]:
|
||||
author_name, image_url, title = (
|
||||
snapshot["author_name"],
|
||||
snapshot["image_url"],
|
||||
snapshot["title"],
|
||||
)
|
||||
return author_name, image_url, title
|
||||
|
||||
|
||||
class LibratoWebhookHandler(LibratoWebhookParser):
|
||||
def __init__(self, payload: Mapping[str, Any], attachments: list[dict[str, Any]]) -> None:
|
||||
super().__init__(payload, attachments)
|
||||
self.payload_available_types = {
|
||||
ALERT_CLEAR: self.handle_alert_clear_message,
|
||||
ALERT_VIOLATION: self.handle_alert_violation_message,
|
||||
}
|
||||
|
||||
self.attachments_available_types = {
|
||||
SNAPSHOT: self.handle_snapshots,
|
||||
}
|
||||
|
||||
def find_handle_method(self) -> Callable[[], str]:
|
||||
for available_type in self.payload_available_types:
|
||||
if self.payload.get(available_type):
|
||||
return self.payload_available_types[available_type]
|
||||
for available_type in self.attachments_available_types:
|
||||
if len(self.attachments) > 0 and self.attachments[0].get(available_type):
|
||||
return self.attachments_available_types[available_type]
|
||||
raise Exception("Unexpected message type")
|
||||
|
||||
def handle(self) -> str:
|
||||
return self.find_handle_method()()
|
||||
|
||||
def generate_topic(self) -> str:
|
||||
if self.attachments:
|
||||
return "Snapshots"
|
||||
topic_template = "Alert {alert_name}"
|
||||
_alert_id, alert_name, _alert_url, _alert_runbook_url = self.parse_alert()
|
||||
return topic_template.format(alert_name=alert_name)
|
||||
|
||||
def handle_alert_clear_message(self) -> str:
|
||||
alert_clear_template = "Alert [alert_name]({alert_url}) has cleared at {trigger_time}!"
|
||||
trigger_time = datetime_to_global_time(timestamp_to_datetime(self.payload["trigger_time"]))
|
||||
_alert_id, alert_name, alert_url, _alert_runbook_url = self.parse_alert()
|
||||
content = alert_clear_template.format(
|
||||
alert_name=alert_name, alert_url=alert_url, trigger_time=trigger_time
|
||||
)
|
||||
return content
|
||||
|
||||
def handle_snapshots(self) -> str:
|
||||
content = ""
|
||||
for attachment in self.attachments:
|
||||
content += self.handle_snapshot(attachment)
|
||||
return content
|
||||
|
||||
def handle_snapshot(self, snapshot: dict[str, Any]) -> str:
|
||||
snapshot_template = "**{author_name}** sent a [snapshot]({image_url}) of [metric]({title})."
|
||||
author_name, image_url, title = self.parse_snapshot(snapshot)
|
||||
content = snapshot_template.format(
|
||||
author_name=author_name, image_url=image_url, title=title
|
||||
)
|
||||
return content
|
||||
|
||||
def handle_alert_violation_message(self) -> str:
|
||||
alert_violation_template = "Alert [alert_name]({alert_url}) has triggered! "
|
||||
_alert_id, alert_name, alert_url, alert_runbook_url = self.parse_alert()
|
||||
content = alert_violation_template.format(alert_name=alert_name, alert_url=alert_url)
|
||||
if alert_runbook_url:
|
||||
alert_runbook_template = "[Reaction steps]({alert_runbook_url}):"
|
||||
content += alert_runbook_template.format(alert_runbook_url=alert_runbook_url)
|
||||
content += self.generate_conditions_and_violations()
|
||||
return content
|
||||
|
||||
def generate_conditions_and_violations(self) -> str:
|
||||
conditions = self.parse_conditions()
|
||||
violations = self.parse_violations()
|
||||
content = ""
|
||||
for condition, violation in zip(conditions, violations, strict=False):
|
||||
content += self.generate_violated_metric_condition(violation, condition)
|
||||
return content
|
||||
|
||||
def generate_violated_metric_condition(
|
||||
self, violation: dict[str, Any], condition: dict[str, Any]
|
||||
) -> str:
|
||||
summary_function, threshold, condition_type, duration = self.parse_condition(condition)
|
||||
metric_name, recorded_at = self.parse_violation(violation)
|
||||
metric_condition_template = (
|
||||
"\n * Metric `{metric_name}`, {summary_function} was {condition_type} {threshold}"
|
||||
)
|
||||
content = metric_condition_template.format(
|
||||
metric_name=metric_name,
|
||||
summary_function=summary_function,
|
||||
condition_type=condition_type,
|
||||
threshold=threshold,
|
||||
)
|
||||
if duration:
|
||||
content += f" by {duration}s"
|
||||
content += f", recorded at {recorded_at}."
|
||||
return content
|
||||
|
||||
|
||||
@webhook_view("Librato")
|
||||
@typed_endpoint
|
||||
def api_librato_webhook(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
*,
|
||||
payload: Json[Mapping[str, Any]] = {}, # noqa: B006 # Mapping is indeed immutable, but Json's type annotation drops that information
|
||||
) -> HttpResponse:
|
||||
try:
|
||||
attachments = orjson.loads(request.body).get("attachments", [])
|
||||
except orjson.JSONDecodeError:
|
||||
attachments = []
|
||||
|
||||
if not attachments and not payload:
|
||||
raise JsonableError(_("Malformed JSON input"))
|
||||
|
||||
message_handler = LibratoWebhookHandler(payload, attachments)
|
||||
topic_name = message_handler.generate_topic()
|
||||
|
||||
try:
|
||||
content = message_handler.handle()
|
||||
except Exception as e:
|
||||
raise JsonableError(str(e))
|
||||
|
||||
check_send_webhook_message(request, user_profile, topic_name, content)
|
||||
return json_success(request)
|
||||
Reference in New Issue
Block a user