Repository navigation
feat: ignore queue time for requests with large bodies - #157
Merged
Merged
Conversation
Ruby's RequestMetrics takes the config it reads, defaulting to Config.instance. Metric.for_web now does the same, defaulting to Judoscale's config. Co-Authored-By: Claude <[email protected]>
Some web servers, such as waitress, read the whole request body before they call the app. A slow upload of a large body then shows up as queue time that says nothing about capacity. Metric.for_web now also takes the request's Content-Length, and returns no queue time metric for a body over the config's MAX_REQUEST_SIZE_BYTES, as Ruby's RequestMetrics does. A missing or invalid Content-Length counts as no body. App time and utilization are still recorded. The setting defaults to 100_000 as in Ruby, and is reported with the rest of the config. Set it to None to count every request. Co-Authored-By: Claude <[email protected]>
CONTENT_LENGTH comes from the WSGI environ as a string, which may be empty or absent. Co-Authored-By: Claude <[email protected]>
Werkzeug parses the header into request.content_length, an int, or None when the request has no Content-Length. The sample app's index route only accepts GET, so its test sends the large body with a GET. Co-Authored-By: Claude <[email protected]>
The middleware now looks both headers up in a dict of the request's headers. The loop it replaces stopped at X-Request-Start, so it would have missed a Content-Length that came after it. With two X-Request-Start headers, the last one now wins instead of the first. ASGI servers such as uvicorn call the app as soon as the headers arrive, so they don't add upload time to queue time themselves. The check is here to match Ruby, which applies its cutoff on every Rack server. Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
ASGI servers such as uvicorn call the app as soon as the headers arrive, so a slow upload never shows up as queue time there. Skipping large requests on ASGI only dropped good samples. Amp-Thread-ID: https://ampcode.com/threads/T-386281c6-cbb1-4bf2-b363-f53338fdcd89 Co-authored-by: Adam McCrea <[email protected]>
Metric.for_web compares the setting to every request's size, so a string such as one read from an environment variable raised a TypeError on every request, and False dropped queue time for any request with a body. Config.update now converts a numeric string to an int and False to None, which turns the cutoff off as in Ruby. Any other invalid value logs a warning and falls back to the default. Amp-Thread-ID: https://ampcode.com/threads/T-386281c6-cbb1-4bf2-b363-f53338fdcd89 Co-authored-by: Adam McCrea <[email protected]>
Member
|
Thanks for this! I pushed two commits on top:
CI passes on 3.10–3.14. The ASGI section of the description is now out of date. |
adamlogic
pushed a commit
that referenced
this pull request
Dec 1, 2026
🤖 I have created a release *beep* *boop* --- ## [1.18.0](v1.17.0...v1.18.0) (2026-12-01) ### Features * allow RQ 3 ([#155](#155)) ([2ef9d09](2ef9d09)) * ignore queue time for requests with large bodies ([#157](#157)) ([a00afd7](a00afd7)) ### Documentation * correct the default log level in the README ([#156](#156)) ([77bc5bd](77bc5bd)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@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.
Some WSGI servers, such as waitress, read the whole request body before they call the app. A slow upload of a large body then shows up as request queue time, which says nothing about capacity, and the autoscaler adds web processes that don't help.
The Ruby adapter already leaves these requests out. From
config.rb:And from
request_metrics.rb:This PR does the same in Python:
MAX_REQUEST_SIZE_BYTES, default100_000.Metric.for_webnow also takes the request'sContent-Length, and returns no queue time metric for a body over the maximum, as Ruby'sRequestMetricsdoes. LikeRequestMetrics, it reads Judoscale's config unless it is given one, so the Django, Flask and ASGI integrations pass only theContent-Length. App time and utilization are still recorded.MAX_REQUEST_SIZE_BYTEStoNone, in theJUDOSCALEsettings for Django and Flask or inextra_configfor ASGI, to count every request. The README documents both.max_request_size_bytesis added tofor_report, as Ruby includes it in its reported config.rack.input.size. This reads theContent-Lengthheader, which all three integrations can see before the body is read. A request without a validContent-Length, such as a chunked upload, is always counted.X-Request-Startheaders the last one wins, where it used to take the first.Not ported: Ruby also subtracts
puma.request_body_waitfrom queue time. WSGI has no equivalent, so the size cutoff is the only correction here.Open question
Should this be on by default, as in Ruby, or opt-in? This PR turns it on by default to match Ruby. Making it opt-in would only mean changing the default to
None.Tests
tests/test_config.pycovers the default and the reported config, including an override throughupdate().tests/test_metrics.pycoversMetric.for_webwith no length, a small body, exactly 100000, a larger body, a non-numeric value, and no maximum, and checks that it reads Judoscale's config when it isn't given one.X-Request-Startand a 100,001-byte body, and assert that noqtmetric is recorded. These tests fail without the change.pytestand each sample app'sbin/testpass, andblackis clean.