Cannot see exemplars in the metrics
Opened this issue · 0 comments
teddy-ambona commented
Hello,
I am trying to add exemplars to the default django-prometheus metrics but calling the metrics endpoint with curl --header "Accept: application/openmetrics-text" http://localhost:8000/metrics does not return the expected trace_id in the response.
Here is what I have done:
in custom_prometheus.py (I simply override process_response and add {'trace_id': 'abc123'} to the .inc() and .observe() methods)
from django_prometheus.middleware import (
PrometheusAfterMiddleware,
)
from django_prometheus.utils import TimeSince
class PrometheusAfterMiddlewareWithExemplar(PrometheusAfterMiddleware):
def process_response(self, request, response):
method = self._method(request)
name = self._get_view_name(request)
status = str(response.status_code)
self.label_metric(
self.metrics.responses_by_status, request, response, status=status
).inc(exemplar={'trace_id': 'abc123'})
self.label_metric(
self.metrics.responses_by_status_view_method,
request,
response,
status=status,
view=name,
method=method,
).inc(exemplar={'trace_id': 'abc123'})
if hasattr(response, "charset"):
self.label_metric(
self.metrics.responses_by_charset,
request,
response,
charset=str(response.charset),
).inc()
if hasattr(response, "streaming") and response.streaming:
self.label_metric(self.metrics.responses_streaming, request, response).inc()
if hasattr(response, "content"):
self.label_metric(
self.metrics.responses_body_bytes, request, response
).observe(len(response.content), {'trace_id': 'abc123'})
if hasattr(request, "prometheus_after_middleware_event"):
self.label_metric(
self.metrics.requests_latency_by_view_method,
request,
response,
view=self._get_view_name(request),
method=request.method,
).observe(TimeSince(request.prometheus_after_middleware_event), {'trace_id': 'abc123'})
else:
self.label_metric(
self.metrics.requests_unknown_latency, request, response
).inc(exemplar={'trace_id': 'abc123'})
return response
in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_prometheus',
]
MIDDLEWARE = [
'django_prometheus.middleware.PrometheusBeforeMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_project.custom_prometheus.PrometheusAfterMiddlewareWithExemplar',
]
What have I done wrong?