Skip to content

Bound method label on latency histogram to known HTTP methods - #541

Open
sarankumar1325 wants to merge 1 commit into
django-commons:masterfrom
sarankumar1325:fix/latency-invalid-method-label
Open

sarankumar1325 wants to merge 1 commit into
django-commons:masterfrom
sarankumar1325:fix/latency-invalid-method-label

Conversation

@sarankumar1325

Copy link
Copy Markdown

PrometheusAfterMiddleware folds unknown HTTP methods into "<invalid method>" via _method() for every per-method metric, except django_http_requests_latency_seconds_by_view_method, which used the raw request.method in both process_response and process_exception.

Because the method comes straight from the client, a request like curl -X FOOBAR /some/view created a new histogram series (all buckets) per distinct method string, so arbitrary methods could grow the latency histogram's label set without bound, while requests_total_by_method and responses_total_by_status_view_method stayed bounded.

Changes:

  • Use self._method(request) for the latency histogram in process_response and process_exception.
  • New end2end test test_latency_histograms_bound_invalid_methods: sends a FOOBAR request and checks that the latency count is recorded under method="<invalid method>" and not under method="FOOBAR". It fails on master and passes with the fix.
  • CHANGELOG entry under v2.6.0.

Tests: testapp/test_middleware.py and test_middleware_custom_labels.py pass (Django 6.0, Python 3.12). ruff check and ruff format --check are clean. The remaining end2end/unit failures in my local run are the DB and cache backends (postgres/mysql/redis/memcached not available locally) and fail the same way on master.

This change was prepared with AI assistance and reviewed by me.

requests_latency_seconds_by_view_method used the raw request.method,
while every other per-method metric goes through _method(), which folds
unknown methods into "<invalid method>". A client sending arbitrary
methods could create unbounded label values on the histogram.

Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

1 participant