From 725be65bddfa944679f41238de7ae563cd30d5ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 29 Jan 2021 15:05:42 +0200 Subject: [PATCH] Add the PROMETHEUS_ENABLED setting --- CHANGELOG.md | 1 + docker/.env | 1 + hc/accounts/tests/test_project.py | 14 +++++++++++++ hc/accounts/views.py | 1 + hc/front/tests/test_add_prometheus.py | 20 +++++++++++++++++++ hc/front/tests/test_metrics.py | 6 ++++++ hc/front/views.py | 4 ++++ hc/settings.py | 5 ++++- templates/accounts/project.html | 2 ++ templates/docs/self_hosted_configuration.html | 3 +++ templates/docs/self_hosted_configuration.md | 6 ++++++ templates/front/channels.html | 2 ++ templates/front/welcome.html | 2 ++ 13 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 hc/front/tests/test_add_prometheus.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 47be1acc..8d32f456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file. - Add the OPSGENIE_ENABLED setting (#471) - Add the PD_ENABLED setting (#471) - Add the PAGERTREE_ENABLED setting (#471) +- Add the PROMETHEUS_ENABLED setting (#471) ## Bug Fixes - Fix unwanted HTML escaping in SMS and WhatsApp notifications diff --git a/docker/.env b/docker/.env index 4756b19e..320e7c72 100644 --- a/docker/.env +++ b/docker/.env @@ -34,6 +34,7 @@ PD_VENDOR_KEY= PING_BODY_LIMIT=10000 PING_EMAIL_DOMAIN=localhost PING_ENDPOINT=http://localhost:8000/ping/ +PROMETHEUS_ENABLED=True PUSHBULLET_CLIENT_ID= PUSHBULLET_CLIENT_SECRET= PUSHOVER_API_TOKEN= diff --git a/hc/accounts/tests/test_project.py b/hc/accounts/tests/test_project.py index b7d47ca3..4b799899 100644 --- a/hc/accounts/tests/test_project.py +++ b/hc/accounts/tests/test_project.py @@ -35,6 +35,7 @@ class ProjectTestCase(BaseTestCase): self.assertContains(r, "X" * 32) self.assertContains(r, "R" * 32) + self.assertContains(r, "Prometheus metrics endpoint") def test_it_creates_api_key(self): self.client.login(username="alice@example.org", password="password") @@ -246,3 +247,16 @@ class ProjectTestCase(BaseTestCase): r = self.client.get(self.url) self.assertNotContains(r, "#set-project-name-modal", status_code=200) self.assertNotContains(r, "Show API Keys") + + @override_settings(PROMETHEUS_ENABLED=False) + def test_it_hides_prometheus_link_if_prometheus_not_enabled(self): + self.project.api_key_readonly = "R" * 32 + self.project.save() + + self.client.login(username="alice@example.org", password="password") + + form = {"show_api_keys": "1"} + r = self.client.post(self.url, form) + self.assertEqual(r.status_code, 200) + + self.assertNotContains(r, "Prometheus metrics endpoint") diff --git a/hc/accounts/views.py b/hc/accounts/views.py index feaf5fe0..fc1a9734 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -291,6 +291,7 @@ def project(request, code): "project": project, "is_owner": is_owner, "show_api_keys": "show_api_keys" in request.GET, + "enable_prometheus": settings.PROMETHEUS_ENABLED is True, } if request.method == "POST": diff --git a/hc/front/tests/test_add_prometheus.py b/hc/front/tests/test_add_prometheus.py new file mode 100644 index 00000000..b9d23958 --- /dev/null +++ b/hc/front/tests/test_add_prometheus.py @@ -0,0 +1,20 @@ +from django.test.utils import override_settings +from hc.test import BaseTestCase + + +class AddPrometheusTestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.url = "/projects/%s/add_prometheus/" % self.project.code + + def test_instructions_work(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertContains(r, "Prometheus") + self.assertContains(r, f"{self.project.code}/metrics/") + + @override_settings(PROMETHEUS_ENABLED=False) + def test_it_handles_disabled_integration(self): + self.client.login(username="alice@example.org", password="password") + r = self.client.get(self.url) + self.assertEqual(r.status_code, 404) diff --git a/hc/front/tests/test_metrics.py b/hc/front/tests/test_metrics.py index 17ee186c..fc513608 100644 --- a/hc/front/tests/test_metrics.py +++ b/hc/front/tests/test_metrics.py @@ -1,3 +1,4 @@ +from django.test.utils import override_settings from hc.api.models import Check from hc.test import BaseTestCase @@ -41,3 +42,8 @@ class MetricsTestCase(BaseTestCase): url = "/projects/%s/checks/metrics/%s" % (self.project.code, "X" * 32) r = self.client.get(url) self.assertEqual(r.status_code, 403) + + @override_settings(PROMETHEUS_ENABLED=False) + def test_it_requires_prometheus_enabled(self): + r = self.client.get(self.url) + self.assertEqual(r.status_code, 404) diff --git a/hc/front/views.py b/hc/front/views.py index 7e503088..0f9bd02f 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -301,6 +301,7 @@ def index(request): "enable_pagertree": settings.PAGERTREE_ENABLED is True, "enable_pd": settings.PD_ENABLED is True, "enable_pdc": settings.PD_VENDOR_KEY is not None, + "enable_prometheus": settings.PROMETHEUS_ENABLED is True, "enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None, "enable_pushover": settings.PUSHOVER_API_TOKEN is not None, "enable_shell": settings.SHELL_ENABLED is True, @@ -774,6 +775,7 @@ def channels(request, code): "enable_pagertree": settings.PAGERTREE_ENABLED is True, "enable_pd": settings.PD_ENABLED is True, "enable_pdc": settings.PD_VENDOR_KEY is not None, + "enable_prometheus": settings.PROMETHEUS_ENABLED is True, "enable_pushbullet": settings.PUSHBULLET_CLIENT_ID is not None, "enable_pushover": settings.PUSHOVER_API_TOKEN is not None, "enable_shell": settings.SHELL_ENABLED is True, @@ -1818,6 +1820,7 @@ def add_msteams(request, code): return render(request, "integrations/add_msteams.html", ctx) +@require_setting("PROMETHEUS_ENABLED") @login_required def add_prometheus(request, code): project, rw = _get_project_for_user(request, code) @@ -1825,6 +1828,7 @@ def add_prometheus(request, code): return render(request, "integrations/add_prometheus.html", ctx) +@require_setting("PROMETHEUS_ENABLED") def metrics(request, code, key): if len(key) != 32: return HttpResponseBadRequest() diff --git a/hc/settings.py b/hc/settings.py index 6b559a75..a99c60c8 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -2,7 +2,7 @@ Django settings for healthchecks project. For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.1/ref/settings +https://docs.djangoproject.com/en/3.1/ref/settings/ """ import os @@ -215,6 +215,9 @@ PAGERTREE_ENABLED = envbool("PAGERTREE_ENABLED", "True") PD_ENABLED = envbool("PD_ENABLED", "True") PD_VENDOR_KEY = os.getenv("PD_VENDOR_KEY") +# Prometheus +PROMETHEUS_ENABLED = envbool("PROMETHEUS_ENABLED", "True") + # Pushover integration PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN") PUSHOVER_SUBSCRIPTION_URL = os.getenv("PUSHOVER_SUBSCRIPTION_URL") diff --git a/templates/accounts/project.html b/templates/accounts/project.html index e10b7092..b4318cbe 100644 --- a/templates/accounts/project.html +++ b/templates/accounts/project.html @@ -92,9 +92,11 @@

Related links: