Fixed c3nav api stats prometheus exporter.

we now use a seperate exportet for the c3nav stats cause the
This commit is contained in:
Jenny Danzmayr 2024-09-20 00:59:15 +02:00
parent f03e5f571d
commit 75ff32fc60
3 changed files with 21 additions and 7 deletions

View file

@ -9,14 +9,15 @@ from c3nav.mapdata.models.report import Report
if settings.METRICS:
from prometheus_client import Gauge
from prometheus_client.core import REGISTRY, CounterMetricFamily
from prometheus_client.registry import Collector
from prometheus_client.core import CounterMetricFamily
from prometheus_client.registry import Collector, CollectorRegistry
users_total = Gauge('c3nav_users_total', 'Total number of users')
REGISTRY = CollectorRegistry(auto_describe=True)
users_total = Gauge('c3nav_users_total', 'Total number of users', registry=REGISTRY)
users_total.set_function(lambda: get_user_model().objects.count())
reports_total = Gauge('c3nav_reports_total', 'Total number of reports')
reports_total = Gauge('c3nav_reports_total', 'Total number of reports', registry=REGISTRY)
reports_total.set_function(lambda: Report.objects.count())
reports_open = Gauge('c3nav_reports_open', 'Number of open reports')
reports_open = Gauge('c3nav_reports_open', 'Number of open reports', registry=REGISTRY)
reports_open.set_function(lambda: Report.objects.filter(open=True).count()),
class APIStatsCollector(Collector):

View file

@ -482,3 +482,12 @@ def get_cache_package(request, filetype):
response["Content-Disposition"] = content_disposition
response['X-Processed-Geometry-Update'] = processed_geometry_update
return response
def api_stats_exporter(request):
"""Exports the API metrics for Prometheus"""
import prometheus_client
from c3nav.mapdata.metrics import REGISTRY
metrics_page = prometheus_client.generate_latest(REGISTRY)
return HttpResponse(metrics_page, content_type=prometheus_client.CONTENT_TYPE_LATEST)

View file

@ -50,8 +50,12 @@ if settings.SERVE_ANYTHING:
if settings.METRICS:
with suppress(ImportError):
import django_prometheus # noqu
urlpatterns.insert(0, path('prometheus/', include('django_prometheus.urls')))
import django_prometheus.urls # noqu
from c3nav.mapdata.views import api_stats_exporter
urlpatterns += [
path('prometheus/api_metrics', api_stats_exporter),
path('prometheus/', include(django_prometheus.urls)),
]
if settings.SSO_ENABLED:
urlpatterns += [