Skip to content

Commit

Permalink
Add basic django metrics (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth authored Jan 2, 2024
1 parent 56c7450 commit 63031eb
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 510 deletions.
3 changes: 2 additions & 1 deletion myhpi/core/markdown/extensions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import xml.etree.ElementTree

from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -145,7 +146,7 @@ def decrease(self, match):

class InternalLinkPattern(LinkInlineProcessor):
def handleMatch(self, m, data=None):
el = util.etree.Element("a")
el = xml.etree.ElementTree.Element("a")
try:
el.set("href", self.url(m.group("id")))
el.text = util.AtomicString(m.group("title"))
Expand Down
6 changes: 5 additions & 1 deletion myhpi/core/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
urlpatterns = []
from django.urls import path

from myhpi.core.views import metrics_view

urlpatterns = [path("metrics", metrics_view, name="prometheus-django-metrics")]
12 changes: 12 additions & 0 deletions myhpi/core/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# Create your views here.
import prometheus_client
from django.conf import settings
from django.http import HttpResponse


def metrics_view(request):
provided_key = request.headers.get("X-API-KEY")
if provided_key and provided_key == settings.METRICS_API_KEY:
metrics_page = prometheus_client.generate_latest(prometheus_client.REGISTRY)
return HttpResponse(metrics_page, content_type=prometheus_client.CONTENT_TYPE_LATEST)
else:
return HttpResponse("Unauthorized", status=401)
9 changes: 8 additions & 1 deletion myhpi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"myhpi.polls",
"myhpi.search",
"static_precompiler",
"django_prometheus",
]

AUTHENTICATION_BACKENDS = [
Expand Down Expand Up @@ -93,6 +94,7 @@
LOGIN_URL = "login"

MIDDLEWARE = [
"django_prometheus.middleware.PrometheusBeforeMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand All @@ -104,6 +106,7 @@
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
"myhpi.core.middleware.IPRangeUserMiddleware",
"mozilla_django_oidc.middleware.SessionRefresh",
"django_prometheus.middleware.PrometheusAfterMiddleware",
]

if DJANGO_DEBUG:
Expand Down Expand Up @@ -308,11 +311,15 @@
SERVER_EMAIL = env.str("SERVER_EMAIL")
ADMINS = getaddresses(env.list("ADMINS"))

# Set this to view /metrics with X-API-KEY header
METRICS_API_KEY = env.str("METRICS_API_KEY", default=None)

# logging

LOG_DIR = os.path.join(BASE_DIR, "logs")
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)

# logging
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
Expand Down
13 changes: 13 additions & 0 deletions myhpi/tests/core/test_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from myhpi.tests.core.utils import MyHPIPageTestCase


class MetricsTests(MyHPIPageTestCase):
def test_unauthorized_metrics(self):
with self.settings(METRICS_API_KEY="TEST_KEY"):
response = self.client.get("/metrics")
self.assertEqual(response.status_code, 401)

def test_authorized_metrics(self):
with self.settings(METRICS_API_KEY="TEST_KEY"):
response = self.client.get("/metrics", **{"HTTP_X-API-KEY": "TEST_KEY"})
self.assertEqual(response.status_code, 200)
2 changes: 2 additions & 0 deletions myhpi/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
DEBUG = True
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
WAGTAIL_APPEND_SLASH = False

METRICS_API_KEY = "TEST_KEY"
1 change: 1 addition & 0 deletions myhpi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
".well-known/security.txt",
RedirectView.as_view(url=os.path.join(settings.STATIC_URL, "security.txt")),
),
path("", include("myhpi.core.urls")),
]


Expand Down
1,137 changes: 630 additions & 507 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ html2text = "^2020.1.16"
wagtail-markdown = "^0.11.1"
autoflake = "^2.2.1"
setuptools-scm = "^8.0.4"
django-prometheus = "^2.3.1"

[tool.poetry.group.dev.dependencies]
pylint = "^3.0.3"
Expand Down

0 comments on commit 63031eb

Please sign in to comment.