Add basic prometheus webhook support

with type=prometheus and secret url arguments.
Also update project files.
This commit is contained in:
Ben 2020-04-03 16:32:17 +02:00
parent 847c0cb45c
commit 9b41ab82a0
Signed by: ben
GPG key ID: 0F54A7ED232D3319
4 changed files with 126 additions and 10 deletions

View file

@ -0,0 +1,31 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="ERROR" enabled_by_default="true">
<option name="ourVersions">
<value>
<list size="3">
<item index="0" class="java.lang.String" itemvalue="3.6" />
<item index="1" class="java.lang.String" itemvalue="3.7" />
<item index="2" class="java.lang.String" itemvalue="3.8" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="E501" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N812" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyRedundantParenthesesInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
</profile>
</component>

View file

@ -3,5 +3,5 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (webhook-matrix-notifier)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (webhook-matrix-notifier)" project-jdk-type="Python SDK" />
</project>

View file

@ -4,10 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (webhook-matrix-notifier)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.8 (webhook-matrix-notifier)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

98
wmn.py
View file

@ -1,5 +1,8 @@
import json
import re
import sys
import traceback
import typing
from datetime import datetime
import yaml
@ -136,9 +139,9 @@ def process_jenkins_request():
if len(change_message) > 0:
htimestamp = datetime.fromtimestamp(change['timestamp'] / 1000).strftime("%d. %b %y %H:%M")
return f"{shorten(change_message)} " \
f"({shorten(change['commitId'], 7, appendix='')}) " \
f"by {change['author']} " \
f"at {htimestamp}"
f"({shorten(change['commitId'], 7, appendix='')}) " \
f"by {change['author']} " \
f"at {htimestamp}"
else:
return shorten(json.dumps(change), appendix="...}")
@ -155,8 +158,8 @@ def process_jenkins_request():
f"{len(change_messages)} commits</p>\n"
"" + (f"<ul>\n{html_changes}\n</ul>\n" if len(change_messages) > 0 else ""),
body=f"**Build {build_name} on project {project_name} complete: {result_type}**, "
f"{len(change_messages)} commits\n"
"" + (f"{text_changes}\n" if len(change_messages) > 0 else ""),
f"{len(change_messages)} commits\n"
"" + (f"{text_changes}\n" if len(change_messages) > 0 else ""),
msgtype=msgtype)
except MatrixRequestError as e:
return matrix_error(e)
@ -165,11 +168,96 @@ def process_jenkins_request():
return "", 204
def process_prometheus_request():
secret = request.args.get('secret')
if secret != cfg['secret']:
abort(401)
msgtype = get_msg_type()
room = get_a_room()
# written for version 4 of the alertmanager webhook JSON
# https://prometheus.io/docs/alerting/configuration/#webhook_config
def color_status_html(status: str, text: typing.Optional[str] = None):
_status_colors = {"resolved": "34A91D", "firing": "EF2929"}
if text is None:
text = status
if status in _status_colors:
return f'<font color="#{_status_colors[status]}">{text}</font>'
else:
return text
def extract_alert_message(alert: typing.Dict[str, typing.Any]) -> typing.Tuple[str, str]:
"""Takes the alert object and returns (text, html) as a string tuple."""
alert_status = alert.get("status", "None")
alert_labels = str(alert.get("labels", None))
alert_annotations = str(alert.get("annotations", None))
alert_start = alert.get("startsAt", None)
alert_end = alert.get("endsAt", None)
alert_daterange = []
if alert_start is not None:
alert_start = datetime.fromisoformat(alert_start).strftime("%d. %b %y %H:%M %Z").rstrip()
alert_daterange.append(f'Started at {alert_start}')
if alert_end is not None:
alert_end = datetime.fromisoformat(alert_end).strftime("%d. %b %y %H:%M %Z").rstrip()
alert_daterange.append(f'Ended at {alert_end}')
alert_daterange = "" if len(alert_daterange) == 0 else f'({", ".join(alert_daterange)})'
alert_generator_url = alert.get("generatorURL", "None")
return (
f'[{alert_status}] Labels: {alert_labels}, Annotations: {alert_annotations} - {alert_daterange} | Generator: {alert_generator_url}',
f'<strong>{color_status_html(alert_status)}</strong> Labels: {alert_labels}, Annotations: {alert_annotations} - {alert_daterange} | Generator: {alert_generator_url}',
)
def extract_prometheus_message() -> typing.Tuple[str, str]:
"""Dissects the request's JSON and returns (text, html) as a string tuple."""
group_key = request.json.get("groupKey", "None")
status = request.json.get("status", "None")
receiver = request.json.get("receiver", "None")
group_labels = str(request.json.get("groupLabels", None))
common_labels = str(request.json.get("commonLabels", None))
common_annotations = str(request.json.get("commonAnnotations", None))
ext_url = request.json.get("externalURL", "None")
alerts = request.json.get("alerts", []) # type: typing.List[typing.Dict[str, typing.Any]]
text_alerts, html_alerts = zip(*map(extract_alert_message, alerts))
text_alerts = "\n" + "\n".join((f"- {msg}" for msg in text_alerts))
html_alerts = "<br>\n<ul>\n" + "\n".join((f" <li>{msg}</li>" for msg in html_alerts)) + "\n</ul>"
return (
f'*{status.title()} alert for group {group_key}*\n Receiver: {receiver}\n Labels: {group_labels} | {common_labels}\n Annotations: {common_annotations}\n External URL: {ext_url}\nAlerts:{text_alerts}',
f'<strong>{color_status_html(status, f"{status.title()} alert for group {group_key}")}</strong><br>\n <em>Receiver:</em> {receiver}<br>\n <em>Labels:</em> {group_labels} | {common_labels}<br>\n <em>Annotations:</em> {common_annotations}<br>\n <em>External URL:</em> {ext_url}<br>\n<em>Alerts:</em>{html_alerts}',
)
try:
html, body = extract_prometheus_message()
except (LookupError, ValueError, TypeError):
print("Error parsing JSON and forming message:", file=sys.stderr)
traceback.print_exc()
return "Error parsing JSON and forming message", 500
try:
client = MatrixClient(cfg["matrix"]["server"])
client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"])
room = client.join_room(room_id_or_alias=room)
room.send_html(html=html, body=body, msgtype=msgtype)
except MatrixRequestError as e:
return matrix_error(e)
# see Flask.make_response, this is interpreted as (body, status)
return "", 204
@application.route('/matrix', methods=("POST",))
def notify():
if 'X-Gitlab-Token' in request.headers:
return process_gitlab_request()
elif 'X-Jenkins-Token' in request.headers:
return process_jenkins_request()
elif 'type' in request.args and request.args.get('type') == "prometheus":
return process_prometheus_request()
else:
return "Cannot determine the request's webhook cause", 400