Browse Source

Add last ping body body to Slack notifications (#735)

Co-authored-by: Sebastian Schneider <sebastian.schneider@boxine.de>
Co-authored-by: Pēteris Caune <cuu508@gmail.com>
pull/737/head
Sebastian Schneider 1 year ago
committed by GitHub
parent
commit
6481ed0d19
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      hc/api/tests/test_notify_slack.py
  2. 5
      hc/api/transports.py
  3. 88
      templates/integrations/slack_message.json

35
hc/api/tests/test_notify_slack.py

@ -49,6 +49,7 @@ class NotifySlackTestCase(BaseTestCase):
attachment = payload["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping"], "Success, an hour ago")
self.assertNotIn("Last Ping Body", fields)
# The payload should not contain check's code
serialized = json.dumps(payload)
@ -178,3 +179,37 @@ class NotifySlackTestCase(BaseTestCase):
attachment = kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping"], "Ignored, an hour ago")
@override_settings(SITE_ROOT="http://testserver")
@patch("hc.api.transports.curl.request")
def test_it_shows_last_ping_body(self, mock_post):
self._setup_data("123")
mock_post.return_value.status_code = 200
self.ping.body_raw = b"Hello World"
self.ping.save()
self.channel.notify(self.check)
assert Notification.objects.count() == 1
args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertEqual(fields["Last Ping Body"], "```Hello World```")
@override_settings(SITE_ROOT="http://testserver")
@patch("hc.api.transports.curl.request")
def test_it_shows_truncated_last_ping_body(self, mock_post):
self._setup_data("123")
mock_post.return_value.status_code = 200
self.ping.body_raw = b"Hello World" * 1000
self.ping.save()
self.channel.notify(self.check)
assert Notification.objects.count() == 1
args, kwargs = mock_post.call_args
attachment = kwargs["json"]["attachments"][0]
fields = {f["title"]: f["value"] for f in attachment["fields"]}
self.assertIn("[truncated]", fields["Last Ping Body"])

5
hc/api/transports.py

@ -346,8 +346,9 @@ class Slack(HttpTransport):
def notify(self, check, notification=None) -> None:
if not settings.SLACK_ENABLED:
raise TransportError("Slack notifications are not enabled.")
text = tmpl("slack_message.json", check=check, ping=self.last_ping(check))
ping = self.last_ping(check)
body = get_ping_body(ping)
text = tmpl("slack_message.json", check=check, ping=ping, body=body)
payload = json.loads(text)
self.post(self.channel.slack_webhook_url, json=payload)

88
templates/integrations/slack_message.json

@ -15,58 +15,72 @@
"title_link": "{{ check.cloaked_url }}",
"fields": [
{% if check.desc %}
{"title": "Description",
"value": "{{ check.desc|escapejs }}"
},
{
"title": "Description",
"value": "{{ check.desc|escapejs }}"
},
{% endif %}
{% if check.project.name %}
{"title": "Project",
"value": "{{ check.project.name|escapejs }}",
"short": true
},
{
"title": "Project",
"value": "{{ check.project.name|escapejs }}",
"short": true
},
{% endif %}
{% if check.tags_list %}
{"title": "Tags",
"value": "{% for tag in check.tags_list %}`{{ tag|escapejs }}` {% endfor %}",
"short": true
},
{
"title": "Tags",
"value": "{% for tag in check.tags_list %}`{{ tag|escapejs }}` {% endfor %}",
"short": true
},
{% endif %}
{% if check.kind == "simple" %}
{"title": "Period",
"value": "{{ check.timeout|hc_duration }}",
"short": true
},
{
"title": "Period",
"value": "{{ check.timeout|hc_duration }}",
"short": true
},
{% elif check.kind == "cron" %}
{"title": "Schedule",
"value": "{{ check.schedule|fix_asterisks|escapejs }}",
"short": true
},
{
"title": "Schedule",
"value": "{{ check.schedule|fix_asterisks|escapejs }}",
"short": true
},
{% endif %}
{"title": "Last Ping",
{% if ping is None %}
"value": "Never",
{% elif ping.kind == "ign" %}
"value": "Ignored, {{ ping.created|naturaltime }}",
{% elif ping.kind == "fail" or ping.exitstatus > 0 %}
"value": "Failure, {{ ping.created|naturaltime }}",
{% elif ping.kind == "start" %}
"value": "Start, {{ ping.created|naturaltime }}",
{% elif ping.kind == "log" %}
"value": "Log, {{ ping.created|naturaltime }}",
{% else %}
"value": "Success, {{ ping.created|naturaltime }}",
{% endif %}
"short": true
{
"title": "Total Pings",
"value": "{{ check.n_pings }}",
"short": true
},
{"title": "Total Pings",
"value": "{{ check.n_pings }}",
"short": true
{
"title": "Last Ping",
{% if ping is None %}
"value": "Never",
{% elif ping.kind == "ign" %}
"value": "Ignored, {{ ping.created|naturaltime }}",
{% elif ping.kind == "fail" or ping.exitstatus > 0 %}
"value": "Failure, {{ ping.created|naturaltime }}",
{% elif ping.kind == "start" %}
"value": "Start, {{ ping.created|naturaltime }}",
{% elif ping.kind == "log" %}
"value": "Log, {{ ping.created|naturaltime }}",
{% else %}
"value": "Success, {{ ping.created|naturaltime }}",
{% endif %}
"short": true
}
{% if body %},
{
"title": "Last Ping Body",
"value": "```{{ body|slice:":1000"|escapejs }}{% if body|length > 1000 %}\n[truncated]{% endif %}```"
}
{% endif %}
]
}]
}
Loading…
Cancel
Save