Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#21 Verify optional/mandatory fields #25

Merged
merged 11 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a ch

## [Unreleased](https://github.com/idealista/prom2teams/tree/develop)
### Fixed

- *[#21](https://github.com/idealista/prom2teams/issues/21) Made some JSON fields optional, avoiding later crashes* @jnogol
- *[#23](https://github.com/idealista/prom2teams/issues/23) Deleted redundant log message* @jnogol

## [1.1.2](https://github.com/idealista/prom2teams/tree/1.1.2)
Expand Down
32 changes: 24 additions & 8 deletions prom2teams/message/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

logger = logging.getLogger()

def check_fields(json_alerts_attr, json_alerts_labels_attr, json_alerts_annotations_attr):
mandatory_fields = ['alertname', 'status', 'instance', 'summary']
optional_fields = ['severity', 'description']
fields = mandatory_fields + optional_fields

alert_fields = {}

for field in fields:
alert_field_key = 'alert_' + field
if field in json_alerts_attr:
alert_fields[alert_field_key] = json_alerts_attr[field]
elif field in json_alerts_labels_attr:
alert_fields[alert_field_key] = json_alerts_labels_attr[field]
elif field in json_alerts_annotations_attr:
alert_fields[alert_field_key] = json_alerts_annotations_attr[field]
# If the field isn't in the JSON but it's a mandatory field, then we send an error message
elif field in mandatory_fields:
alert_fields['alert_severity'] = 'severe'
alert_fields['alert_status'] = 'incorrect'
alert_fields['alert_summary'] = 'Incorrect JSON received. At least one mandatory field ('+field+') is absent.'
return alert_fields

return alert_fields

def parse(json_str):
json_values = json.loads(json_str)
Expand All @@ -12,11 +35,4 @@ def parse(json_str):
json_alerts_labels_attr = json_alerts_attr['labels']
json_alerts_annotations_attr = json_alerts_attr['annotations']

return {
'alert_name': json_alerts_labels_attr['alertname'],
'alert_instance': json_alerts_labels_attr['instance'],
'alert_severity': json_alerts_labels_attr['severity'],
'alert_summary': json_alerts_annotations_attr['summary'],
'alert_description': json_alerts_annotations_attr['description'],
'alert_status': json_alerts_attr['status']
}
return check_fields(json_alerts_attr, json_alerts_labels_attr, json_alerts_annotations_attr)
16 changes: 8 additions & 8 deletions prom2teams/teams/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
"@context": "http://schema.org/extensions",
"themeColor": "{% if alert_status=='resolved' %} {{ theme_colors.resolved }} {% else %} {{ theme_colors[msg_text.alert_severity] }} {% endif %}",
"summary": "{{ msg_text.alert_summary }}",
"title": "Prometheus alarm {% if alert_status=='resolved' %} (Resolved) {% endif %}",
"title": "Prometheus alarm {% if alert_status=='resolved' %} (Resolved) {% elif alert_status=='incorrect' %} (Incorrect) {% endif %}",
"sections": [{
"activityTitle": "{{ msg_text.alert_summary }}",
"facts": [{
"facts": [{% if msg_text.alert_alertname %}{
"name": "Alarm",
"value": "{{ msg_text.alert_name }}"
}, {
"value": "{{ msg_text.alert_alertname }}"
},{% endif %}{% if msg_text.alert_instance %}{
"name": "In host",
"value": "{{ msg_text.alert_instance }}"
}, {
},{% endif %}{% if msg_text.alert_severity %}{
"name": "Severity",
"value": "{{ msg_text.alert_severity }}"
}, {
},{% endif %}{% if msg_text.alert_description %}{
"name": "Description",
"value": "{{ msg_text.alert_description }}"
}, {
},{% endif %}{
"name": "Status",
"value": "{{ msg_text.alert_status }}"
}],
"markdown": true
}]
}]
}
1 change: 1 addition & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../prom2teams')))

from prom2teams import server, exceptions
from prom2teams.message.parser import parse
27 changes: 27 additions & 0 deletions tests/data/jsons/all_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"receiver": "test_webhook",
"status": "resolved",
"alerts": [
{
"status": "resolved",
"labels": {
"alertname": "DiskSpace",
"device": "rootfs",
"fstype": "rootfs",
"instance": "cs30.evilcorp",
"job": "fsociety",
"mountpoint": "/",
"severity": "severe"
},
"annotations": {
"description": "disk usage 73% on rootfs device",
"summary": "Disk usage alert on CS30.evilcorp"
},
"startsAt": "2017-05-09T07:01:37.803Z",
"endsAt": "2017-05-09T07:08:37.818278068Z",
"generatorURL": "my.prometheusserver.url"
}
],
"externalURL": "my.prometheusalertmanager.url",
"version": "4"
}
25 changes: 25 additions & 0 deletions tests/data/jsons/without_mandatory_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"receiver": "test_webhook",
"alerts": [
{
"labels": {
"alertname": "DiskSpace",
"device": "rootfs",
"fstype": "rootfs",
"instance": "cs30.evilcorp",
"job": "fsociety",
"mountpoint": "/",
"severity": "severe"
},
"annotations": {
"description": "disk usage 73% on rootfs device",
"summary": "Disk usage alert on CS30.evilcorp"
},
"startsAt": "2017-05-09T07:01:37.803Z",
"endsAt": "2017-05-09T07:08:37.818278068Z",
"generatorURL": "my.prometheusserver.url"
}
],
"externalURL": "my.prometheusalertmanager.url",
"version": "4"
}
26 changes: 26 additions & 0 deletions tests/data/jsons/without_optional_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"receiver": "test_webhook",
"status": "resolved",
"alerts": [
{
"status": "resolved",
"labels": {
"alertname": "DiskSpace",
"device": "rootfs",
"fstype": "rootfs",
"instance": "cs30.evilcorp",
"job": "fsociety",
"mountpoint": "/",
"severity": "severe"
},
"annotations": {
"summary": "Disk usage alert on CS30.evilcorp"
},
"startsAt": "2017-05-09T07:01:37.803Z",
"endsAt": "2017-05-09T07:08:37.818278068Z",
"generatorURL": "my.prometheusserver.url"
}
],
"externalURL": "my.prometheusalertmanager.url",
"version": "4"
}
31 changes: 31 additions & 0 deletions tests/test_json_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
import json

from context import parse


class TestJSONFields(unittest.TestCase):

TEST_CONFIG_FILES_PATH = 'tests/data/jsons/'

def test_json_with_all_fields(self):
with open(self.TEST_CONFIG_FILES_PATH + 'all_ok.json') as json_data:
json_received = json.load(json_data)
alert_fields = parse(json.dumps(json_received))
self.assertNotIn('Incorrect',str(alert_fields))

def test_json_without_mandatory_field(self):
with open(self.TEST_CONFIG_FILES_PATH + 'without_mandatory_field.json') as json_data:
json_received = json.load(json_data)
alert_fields = parse(json.dumps(json_received))
self.assertIn('Incorrect',str(alert_fields))

def test_json_without_optional_field(self):
with open(self.TEST_CONFIG_FILES_PATH + 'without_optional_field.json') as json_data:
json_received = json.load(json_data)
alert_fields = parse(json.dumps(json_received))
self.assertNotIn('Incorrect',str(alert_fields))


if __name__ == '__main__':
unittest.main()