root(major): add prometheus

This commit is contained in:
Langhammer, Jens
2019-11-08 12:23:51 +01:00
parent 44c0eb37cf
commit e4fbcd3735
7 changed files with 178 additions and 131 deletions

View File

@ -0,0 +1,21 @@
"""Metrics view"""
from base64 import b64encode
from django.conf import settings
from django.views import View
from django.http import HttpRequest, HttpResponse, Http404
from django_prometheus.exports import ExportToDjangoView
class MetricsView(View):
"""Wrapper around ExportToDjangoView, using http-basic auth"""
def get(self, request: HttpRequest) -> HttpResponse:
"""Check for HTTP-Basic auth"""
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
token_type, _, credentials = auth_header.partition(' ')
creds = f"monitor:{settings.SECRET_KEY}"
expected = b64encode(str.encode(creds)).decode()
if token_type != 'Basic' or credentials != expected:
raise Http404
return ExportToDjangoView(request)