Compare commits

...

2 Commits

Author SHA1 Message Date
Julian-Samuel Gebühr ab9625ea61
Merge pull request #5 from pauvos/prom-alert-labels
Handle missing labels of prometheus alerts more gracefully
2023-03-29 07:44:21 +02:00
Paul Voss f18d1a6550 Handle missing labels of prometheus alerts more gracefully 2023-03-28 19:33:48 +02:00
1 changed files with 8 additions and 7 deletions

View File

@ -162,14 +162,15 @@ def prometheus_alert_to_markdown(alert_data: dict) -> str:
:return: Alert as fomatted markdown
"""
messages = []
known_labels = ['alertname', 'instance', 'job']
for alert in alert_data["alerts"]:
message = (
f"""**{alert['status']}** {'💚' if alert['status'] == 'resolved' else '🔥'}: {alert['annotations']['description']}
* **Alertname:** {alert["labels"]['alertname']}
* **Instance:** {alert["labels"]['instance']}
* **Job:** {alert["labels"]['job']}
"""
)
title = alert['annotations']['description'] if hasattr(alert['annotations'], 'description') else alert['annotations']['summary']
message = f"""**{alert['status']}** {'💚' if alert['status'] == 'resolved' else '🔥'}: {title}"""
for label_name in known_labels:
try:
message += "\n* **{0}**: {1}".format(label_name.capitalize(), alert["labels"][label_name])
except:
pass
messages.append(message)
return messages