Add link to Prometheus UI in alert description
- extract from generatorURL field
- add ALERTS{alertname="<name>",alertstate="firing"} query
- unify alert subject format with email and slack
Change-Id: Ic16e09552dc447861dc552f60c9e313234dadd00
Related-PROD: PRODX-7212
diff --git a/sf_notifier/helpers.py b/sf_notifier/helpers.py
index e9c221b..1c99a0a 100644
--- a/sf_notifier/helpers.py
+++ b/sf_notifier/helpers.py
@@ -14,7 +14,7 @@
# under the License.
import os
-
+import urllib
RESOLVED_STATUSES = ('UP', 'OK', 'resolved')
@@ -25,7 +25,12 @@
return file_path
-def _format_subject(alert):
+def _format_subject(alert, add_links=False):
+ if add_links:
+ severity = alert['labels']['severity'].upper()
+ name = alert['labels']['alertname']
+ return '[{}] {}'.format(severity, name)
+
subject = alert['annotations']['summary']
host = alert['labels'].get('host')
if host is not None:
@@ -37,11 +42,35 @@
return description.split(' Modify the')[0]
-def _format_description(alert):
- return _remove_advice(alert['annotations']['description'])
+def _get_prometheus_link(alert):
+ # switch from Console to Graph tab to show trends
+ condition_url = alert['generatorURL'].replace('g0.tab=1', 'g0.tab=0')
+
+ # list alert instances in the cluster
+ # because of Python formatting - double braces were used
+ alert_query = ('g1.range_input=1h&'
+ 'g1.expr=ALERTS{{alertname="{alertname}",'
+ 'alertstate="firing"}}&'
+ 'g1.tab=0')
+ alert_query = alert_query.format(**alert['labels'])
+ alert_query = urllib.quote(alert_query)
+ return '{}&{}'.format(condition_url, alert_query)
-def alert_fields_and_action(alert):
+def _format_description(alert, add_links=False):
+ description_old = _remove_advice(alert['annotations']['description'])
+
+ if not add_links:
+ return description_old
+
+ msg = ('To check trends of the alert underlying condition metric '
+ ' and alert occurrence click on Prometheus UI link')
+
+ prometheus_url = _get_prometheus_link(alert)
+ return '{}\n\n{}:\n{}'.format(description_old, msg, prometheus_url)
+
+
+def alert_fields_and_action(alert, add_links=False):
fields = []
if alert['status'] in RESOLVED_STATUSES:
@@ -50,7 +79,7 @@
else:
action = 'create_case'
# Order of keys matters
- fields.append(_format_subject(alert))
- fields.append(_format_description(alert))
+ fields.append(_format_subject(alert, add_links))
+ fields.append(_format_description(alert, add_links))
fields.append(alert['labels'])
return fields, action