Add actions and more data

This commit is contained in:
Abel Luck 2024-07-18 16:20:35 +02:00
parent 4c5a33d72f
commit 63b351771a
2 changed files with 27 additions and 12 deletions

View file

@ -30,11 +30,9 @@ def prometheus_alert_to_markdown(
logging.debug(f"alertmanager payload: {alert_data}")
for alert in alert_data["alerts"]:
title = (
alert["annotations"]["description"]
if hasattr(alert["annotations"], "description")
else alert["annotations"]["summary"]
)
title = alert["labels"]["alertname"]
summary = alert["annotations"].get("summary")
description = alert["annotations"].get("description")
logging.debug(f"processing alert: '{title}'")
labels = alert.get("labels", {})
severity = labels.get("severity", "unknown")
@ -46,14 +44,12 @@ def prometheus_alert_to_markdown(
plain = f"{status.upper()}[{severity}]: {title}"
header_str = f"{status.upper()}[{severity}]"
url = alert.get("generatorURL")
if url and url != "":
title_linked = f"[{title}]({url})"
else:
title_linked = title
formatted = (
f"<strong><font color={color}>{header_str}</font></strong>: {title_linked}"
f"<strong><font color={color}>{header_str}</font></strong>: {title}"
)
if summary:
formatted += f" - {summary}"
plain += f" - {summary}"
label_strings = []
for label_name, label_value in labels.items():
logging.debug(f"got label: {label_name} = {label_value}")
@ -64,7 +60,25 @@ def prometheus_alert_to_markdown(
label_strings.append((f"**{label_name.capitalize()}**: {label_value}"))
labels_final = ", ".join(label_strings)
plain += f" \n{labels_final}"
if description:
formatted += f"<br/>Description: {description}"
plain += f" \n{description}"
formatted += f"<br/>{labels_final}"
generatorURL = alert.get("generatorURL")
runbookURL = alert["annotations"].get("runbook_url")
dashboardURL = alert.get("dashboardURL")
silenceURL = alert.get("silenceURL")
actions = []
if silenceURL:
actions.append(f"[🔕 Mute]({silenceURL})")
if runbookURL:
actions.append(f"[📗 Runbook]({runbookURL})")
if dashboardURL:
actions.append(f"[📈 Dashboard]({dashboardURL})")
if generatorURL:
actions.append(f"[🔍 Inspect]({generatorURL})")
formatted += "<br/>" + " | ".join(actions)
messages.append((plain, formatted))
return messages

View file

@ -55,11 +55,12 @@ def test_alertmanager():
plain, formatted = r[0]
print(formatted)
assert (
"FIRING" in plain
and "Instance" in plain
and "webserver.example.com" in plain
and "Instance webserver.example.com down" in plain
and "THIS IS A TEST" in plain
and "critical" in plain
)
assert (