Skip to content

Commit

Permalink
#241 Add runbook_url action (excluding it from extra_annotations field)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperera committed Feb 5, 2021
1 parent 5d5e977 commit 26f8555
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 13 deletions.
3 changes: 2 additions & 1 deletion prom2teams/app/versions/v1/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

annotations = api_v1.model('annotations', {
'description': fields.String(default='disk usage 93% on rootfs device'),
'summary': fields.String(default='Disk usage alert on CS30.evilcorp')
'summary': fields.String(default='Disk usage alert on CS30.evilcorp'),
'runbook_url': fields.String(default='')
})

labels = api_v1.model('labels', {
Expand Down
3 changes: 2 additions & 1 deletion prom2teams/app/versions/v2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

annotations = api_v2.model('annotations', {
'description': fields.String(default='disk usage 93% on rootfs device'),
'summary': fields.String(default='Disk usage alert on CS30.evilcorp')
'summary': fields.String(default='Disk usage alert on CS30.evilcorp'),
'runbook_url': fields.String(default='')
})

labels = api_v2.model('labels', {
Expand Down
8 changes: 5 additions & 3 deletions prom2teams/prometheus/message_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_alerts(self, message):

base_labels = ('alertname', 'device', 'fstype', 'instance', 'mountpoint', 'severity')
excluded = base_labels + self.exclude_fields
base_annotations = ('description', 'summary')
base_annotations = ('description', 'summary', 'runbook_url')
excluded_annotations = base_annotations + self.exclude_annotations

for alert in message['alerts']:
Expand All @@ -39,6 +39,7 @@ def get_alerts(self, message):
name = alert['labels']['alertname']
description = alert['annotations']['description']
severity = alert['labels']['severity']
runbook_url = alert['annotations'].get('runbook_url', '')
fingerprint = alert.get('fingerprint', None)
extra_labels = dict()
extra_annotations = dict()
Expand All @@ -57,7 +58,7 @@ def get_alerts(self, message):
if key not in excluded_annotations and annotation_is_not_dict:
extra_annotations[key] = annotation

alert = PrometheusAlert(name, status, severity, summary, instance, description, fingerprint, extra_labels, extra_annotations)
alert = PrometheusAlert(name, status, severity, summary, instance, description, fingerprint, runbook_url, extra_labels, extra_annotations)
prom_alerts.append(alert)
return prom_alerts

Expand Down Expand Up @@ -89,13 +90,14 @@ class AnnotationSchema(Schema):


class PrometheusAlert:
def __init__(self, name, status, severity, summary, instance, description, fingerprint, extra_labels=None, extra_annotations=None):
def __init__(self, name, status, severity, summary, instance, description, fingerprint, runbook_url, extra_labels=None, extra_annotations=None):
self.name = name
self.status = status
self.severity = severity
self.summary = summary
self.instance = instance
self.description = description
self.fingerprint = fingerprint
self.runbook_url = runbook_url
self.extra_labels = extra_labels
self.extra_annotations = extra_annotations
13 changes: 13 additions & 0 deletions prom2teams/resources/templates/teams.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@
}{% endfor %}{% endif %}],
"markdown": true
}]
{% if msg_text.runbook_url %}
,
"potentialAction": [
{
"@context": "http://schema.org",
"@type": "ViewAction",
"name": "Runbook",
"target": [
"{{ msg_text.runbook_url }}"
]
}
]
{% endif %}
}
6 changes: 3 additions & 3 deletions prom2teams/teams/alert_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from prom2teams.teams.teams_alert_schema import TeamsAlert, TeamsAlertSchema

GROUPABLE_FIELDS = ['name', 'description', 'instance',
'severity', 'status', 'summary', 'fingerprint']
'severity', 'status', 'summary', 'fingerprint', 'runbook_url']
EXTRA_FIELDS = ['extra_labels', 'extra_annotations']
FIELD_SEPARATOR = ',\n\n\n'

Expand All @@ -16,7 +16,7 @@ def map_prom_alerts_to_teams_alerts(alerts):
for alert in alerts[same_status_alerts]:
alert = TeamsAlert(alert.name, alert.status.lower(), alert.severity,
alert.summary, alert.instance, alert.description,
alert.fingerprint, alert.extra_labels,
alert.fingerprint, alert.runbook_url, alert.extra_labels,
alert.extra_annotations)
json_alert = schema.dump(alert)
teams_alerts.append(json_alert)
Expand Down Expand Up @@ -62,7 +62,7 @@ def _combine_alerts_to_alert(alerts):

def _map_dict_alert_to_alert(alert):
return TeamsAlert(alert['name'], alert['status'].lower(), alert['severity'], alert['summary'],
alert['instance'], alert['description'], alert['fingerprint'],
alert['instance'], alert['description'], alert['fingerprint'], alert['runbook_url'],
alert['extra_labels'], alert['extra_annotations'])


Expand Down
4 changes: 3 additions & 1 deletion prom2teams/teams/teams_alert_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Meta:
description = fields.Str()
name = fields.Str()
fingerprint = fields.Str()
runbook_url = fields.Str()
extra_labels = fields.Dict(
keys=fields.Str(),
values=fields.Str(),
Expand All @@ -26,13 +27,14 @@ class Meta:


class TeamsAlert:
def __init__(self, name, status, severity, summary, instance, description, fingerprint, extra_labels, extra_annotations):
def __init__(self, name, status, severity, summary, instance, description, fingerprint, runbook_url, extra_labels, extra_annotations):
self.name = name
self.status = status
self.severity = severity
self.summary = summary
self.instance = instance
self.description = description
self.fingerprint = fingerprint
self.runbook_url = runbook_url
self.extra_labels = extra_labels
self.extra_annotations = extra_annotations
14 changes: 10 additions & 4 deletions tests/data/json_files/teams_alert_all_ok_extra_annotations.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@
{
"name": "Status",
"value": "resolved"
},
{
"name": "runbook_url",
"value": "https://cs30-evilcorp-runbooks.com/rootfs-device-high-disk-usage"
}
],
"markdown": true
}
],
"potentialAction": [
{
"@context": "http://schema.org",
"@type": "ViewAction",
"name": "Runbook",
"target": [
"https://cs30-evilcorp-runbooks.com/rootfs-device-high-disk-usage"
]
}
]
}
10 changes: 10 additions & 0 deletions tests/data/json_files/teams_alert_with_common_items.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@
],
"markdown": true
}
],
"potentialAction": [
{
"@context": "http://schema.org",
"@type": "ViewAction",
"name": "Runbook",
"target": [
"https://cs30-evilcorp-runbooks.com/rootfs-device-high-disk-usage"
]
}
]
}

0 comments on commit 26f8555

Please sign in to comment.