Repository navigation
Bound method label on latency histogram to known HTTP methods - #541
Open
sarankumar1325 wants to merge 1 commit into
Open
sarankumar1325 wants to merge 1 commit into
sarankumar1325 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PrometheusAfterMiddlewarefolds unknown HTTP methods into"<invalid method>"via_method()for every per-method metric, exceptdjango_http_requests_latency_seconds_by_view_method, which used the rawrequest.methodin bothprocess_responseandprocess_exception.Because the method comes straight from the client, a request like
curl -X FOOBAR /some/viewcreated a new histogram series (all buckets) per distinct method string, so arbitrary methods could grow the latency histogram's label set without bound, whilerequests_total_by_methodandresponses_total_by_status_view_methodstayed bounded.Changes:
self._method(request)for the latency histogram inprocess_responseandprocess_exception.test_latency_histograms_bound_invalid_methods: sends aFOOBARrequest and checks that the latency count is recorded undermethod="<invalid method>"and not undermethod="FOOBAR". It fails on master and passes with the fix.Tests:
testapp/test_middleware.pyandtest_middleware_custom_labels.pypass (Django 6.0, Python 3.12).ruff checkandruff format --checkare 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.