Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot see exemplars in the metrics #345

Open
teddy-ambona opened this issue Jan 24, 2023 · 0 comments
Open

Cannot see exemplars in the metrics #345

teddy-ambona opened this issue Jan 24, 2023 · 0 comments

Comments

@teddy-ambona
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant