zulip/zerver/webhooks/librato/view.py

186 lines
7.2 KiB
Python
Raw Normal View History

from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Tuple
2016-08-31 21:23:20 +02:00
import orjson
2017-11-16 00:43:10 +01:00
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
2016-08-31 21:23:20 +02:00
from zerver.decorator import webhook_view
from zerver.lib.exceptions import JsonableError
2017-11-16 00:43:10 +01:00
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.validator import check_dict
from zerver.lib.webhooks.common import check_send_webhook_message
from zerver.models import UserProfile
2016-08-31 21:23:20 +02:00
ALERT_CLEAR = "clear"
ALERT_VIOLATION = "violations"
SNAPSHOT = "image_url"
2016-08-31 21:23:20 +02:00
class LibratoWebhookParser:
2016-08-31 21:23:20 +02:00
ALERT_URL_TEMPLATE = "https://metrics.librato.com/alerts#/{alert_id}"
def __init__(self, payload: Dict[str, Any], attachments: List[Dict[str, Any]]) -> None:
2016-08-31 21:23:20 +02:00
self.payload = payload
self.attachments = attachments
def generate_alert_url(self, alert_id: int) -> str:
2016-08-31 21:23:20 +02:00
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"]
2016-08-31 21:23:20 +02:00
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", "")
2016-08-31 21:23:20 +02:00
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.fromtimestamp((violation["recorded_at"]), tz=timezone.utc).strftime(
"%Y-%m-%d %H:%M:%S"
)
2016-08-31 21:23:20 +02:00
return metric_name, recorded_at
def parse_conditions(self) -> List[Dict[str, Any]]:
conditions = self.payload["conditions"]
2016-08-31 21:23:20 +02:00
return conditions
def parse_violations(self) -> List[Dict[str, Any]]:
violations = self.payload["violations"]["test-source"]
2016-08-31 21:23:20 +02:00
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"],
)
2016-08-31 21:23:20 +02:00
return author_name, image_url, title
2016-08-31 21:23:20 +02:00
class LibratoWebhookHandler(LibratoWebhookParser):
def __init__(self, payload: Dict[str, Any], attachments: List[Dict[str, Any]]) -> None:
super().__init__(payload, attachments)
2016-08-31 21:23:20 +02:00
self.payload_available_types = {
ALERT_CLEAR: self.handle_alert_clear_message,
ALERT_VIOLATION: self.handle_alert_violation_message,
2016-08-31 21:23:20 +02:00
}
self.attachments_available_types = {
SNAPSHOT: self.handle_snapshots,
2016-08-31 21:23:20 +02:00
}
def find_handle_method(self) -> Callable[[], str]:
2016-08-31 21:23:20 +02:00
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 self.attachments[0].get(available_type):
return self.attachments_available_types[available_type]
raise Exception("Unexcepted message type")
def handle(self) -> str:
2016-08-31 21:23:20 +02:00
return self.find_handle_method()()
def generate_topic(self) -> str:
2016-08-31 21:23:20 +02:00
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} UTC!"
trigger_time = datetime.fromtimestamp(
(self.payload["trigger_time"]), tz=timezone.utc
).strftime("%Y-%m-%d %H:%M:%S")
2016-08-31 21:23:20 +02:00
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
)
2016-08-31 21:23:20 +02:00
return content
def handle_snapshots(self) -> str:
content = ""
2016-08-31 21:23:20 +02:00
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})."
2016-08-31 21:23:20 +02:00
author_name, image_url, title = self.parse_snapshot(snapshot)
content = snapshot_template.format(
author_name=author_name, image_url=image_url, title=title
)
2016-08-31 21:23:20 +02:00
return content
def handle_alert_violation_message(self) -> str:
alert_violation_template = "Alert [alert_name]({alert_url}) has triggered! "
2016-08-31 21:23:20 +02:00
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}):"
2016-08-31 21:23:20 +02:00
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:
2016-08-31 21:23:20 +02:00
conditions = self.parse_conditions()
violations = self.parse_violations()
content = ""
2016-08-31 21:23:20 +02:00
for condition, violation in zip(conditions, violations):
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:
2016-08-31 21:23:20 +02:00
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}"
)
2016-08-31 21:23:20 +02:00
content = metric_condition_template.format(
metric_name=metric_name,
summary_function=summary_function,
condition_type=condition_type,
threshold=threshold,
)
2016-08-31 21:23:20 +02:00
if duration:
content += f" by {duration}s"
content += f", recorded at {recorded_at} UTC."
2016-08-31 21:23:20 +02:00
return content
@webhook_view("Librato")
2016-08-31 21:23:20 +02:00
@has_request_variables
def api_librato_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Dict[str, Any] = REQ(json_validator=check_dict(), default={}),
) -> HttpResponse:
2016-08-31 21:23:20 +02:00
try:
attachments = orjson.loads(request.body).get("attachments", [])
except orjson.JSONDecodeError:
2016-08-31 21:23:20 +02:00
attachments = []
if not attachments and not payload:
raise JsonableError(_("Malformed JSON input"))
2016-08-31 21:23:20 +02:00
message_handler = LibratoWebhookHandler(payload, attachments)
topic = message_handler.generate_topic()
2016-08-31 21:23:20 +02:00
try:
content = message_handler.handle()
except Exception as e:
raise JsonableError(str(e))
2016-08-31 21:23:20 +02:00
check_send_webhook_message(request, user_profile, topic, content)
return json_success(request)