root: add error handling for prometheus view

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2022-04-29 10:17:53 +02:00
parent eb6cfd22a7
commit c0a883f76f

View file

@ -1,14 +1,15 @@
"""Metrics view"""
from base64 import b64encode
import prometheus_client
from django.conf import settings
from django.db import connections
from django.db.utils import OperationalError
from django.dispatch import Signal
from django.http import HttpRequest, HttpResponse
from django.views import View
from django_prometheus.exports import ExportToDjangoView
from django_redis import get_redis_connection
from prometheus_client import multiprocess
from redis.exceptions import RedisError
monitoring_set = Signal()
@ -31,7 +32,13 @@ class MetricsView(View):
monitoring_set.send_robust(self)
return ExportToDjangoView(request)
registry = prometheus_client.CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
try:
metrics_page = prometheus_client.generate_latest(registry)
return HttpResponse(metrics_page, content_type=prometheus_client.CONTENT_TYPE_LATEST)
except (UnicodeDecodeError, KeyError):
return HttpResponse(status_code=500)
class LiveView(View):