Skip to content

Add settings to make it work with django.contrib.auth.middleware.LoginRequiredMiddleware without auth #480

@codot-fr

Description

@codot-fr

Hello,

I've had an issue with django 5.2 and the django.contrib.auth.middleware.LoginRequiredMiddleware middleware on to enable site-wide login requirement.

I wanted to allow /metrics to be unauthenticated because it would be way too complicated to configure prometheus scraping with django auth system, and I don't think it's worth it from a security perspective.

Therefore, I did this bit of workaround:

from django_prometheus.exports import ExportToDjangoView
from django.contrib.auth.decorators import login_not_required

# Hack to allow unauthenticated access to the prometheus metrics view
# with django.contrib.auth.middleware.LoginRequiredMiddleware on
@login_not_required
def UnauthenticatedDjangoMetricsView(request):
    return ExportToDjangoView(request)

urlpatterns = [
    ...
    path('metrics', UnauthenticatedDjangoMetricsView, name="prometheus-django-metrics"),
]

However, it would be quite nice if there was some kind of switch parameter for django-prometheus to enable this decorator. I kinda hope my hack won't break at the next library update.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions