From 1ca7b9856fb53c5703e4a878e346c18ae9a5d7cf Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Fri, 10 Dec 2021 13:06:27 +0000 Subject: [PATCH] Work around Grafana's new alerting system not actually sending alerts (changes) but only alert statuses --- contrib/modules/grafana.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/contrib/modules/grafana.py b/contrib/modules/grafana.py index 2fde817..feda3e8 100644 --- a/contrib/modules/grafana.py +++ b/contrib/modules/grafana.py @@ -1,11 +1,30 @@ +import datetime import json +FIVE_MINUTES = datetime.timedelta(seconds = 300) + + +def parse_datetime(s): + assert s.endswith('Z') + if '.' in s: + s = s.split('.', 1)[0] + '+00:00' + else: + s = s[:-1] + '+00:00' + return datetime.datetime.fromisoformat(s) + + async def process(request): + now = datetime.datetime.now(datetime.timezone.utc) obj = json.loads(await request.text()) - evalMatchesStr = (': ' + ', '.join(f'{x["metric"]} = {x["value"]}' for x in obj['evalMatches'])) if 'evalMatches' in obj and obj['evalMatches'] else '' - if 'message' in obj: - message = obj['message'].replace('\n', ' ') - return f'{obj["title"]} - {message}{evalMatchesStr}' + assert obj['version'] == '1' + alerts = [] + for a in obj['alerts']: + startTime = parse_datetime(a['startsAt']) + endTime = parse_datetime(a['endsAt']) + if now - startTime < FIVE_MINUTES or now - endTime < FIVE_MINUTES: + alerts.append(f'{a["labels"]["alertname"]} {a["status"]}') + if alerts: + return f'[{obj["status"]}] {", ".join(alerts)}' else: - return f'{obj["title"]}{evalMatchesStr}' + return f'[{obj["status"]}]'