Skip to content

feat: ignore queue time for requests with large bodies - #157

Merged
adamlogic merged 8 commits into
judoscale:mainfrom
kavdev:ignore-large-request-queue-time
Dec 1, 2026
Merged

adamlogic merged 8 commits into
judoscale:mainfrom
kavdev:ignore-large-request-queue-time

Conversation

@kavdev

@kavdev kavdev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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:

@max_request_size_bytes = 100_000 # ignore request payloads over 100k since they skew the queue times

And from request_metrics.rb:

@config.ignore_large_requests? && @size > @config.max_request_size_bytes

This PR does the same in Python:

  • Setting: MAX_REQUEST_SIZE_BYTES, default 100_000. Metric.for_web now also takes the request's Content-Length, and returns no queue time metric for a body over the maximum, as Ruby's RequestMetrics does. Like RequestMetrics, it reads Judoscale's config unless it is given one, so the Django, Flask and ASGI integrations pass only the Content-Length. App time and utilization are still recorded.
  • Turning it off: set MAX_REQUEST_SIZE_BYTES to None, in the JUDOSCALE settings for Django and Flask or in extra_config for ASGI, to count every request. The README documents both.
  • Reported config: max_request_size_bytes is added to for_report, as Ruby includes it in its reported config.
  • Body size: Ruby measures rack.input.size. This reads the Content-Length header, which all three integrations can see before the body is read. A request without a valid Content-Length, such as a chunked upload, is always counted.
  • ASGI: the ASGI middleware gets the same check, to match Ruby, which applies its cutoff on every Rack server. ASGI servers such as uvicorn call the app once the headers arrive, so there the check mostly drops a few unskewed samples. The middleware now looks both headers up in a dict, so with two X-Request-Start headers the last one wins, where it used to take the first.

Not ported: Ruby also subtracts puma.request_body_wait from 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.py covers the default and the reported config, including an override through update().
  • tests/test_metrics.py covers Metric.for_web with 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.
  • The Django, Flask and FastAPI sample apps each send a request with X-Request-Start and a 100,001-byte body, and assert that no qt metric is recorded. These tests fail without the change.
  • pytest and each sample app's bin/test pass, and black is clean.

kavdev and others added 8 commits November 25, 2026 22:19
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]>
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]>

Copy link
Copy Markdown
Member

Thanks for this! I pushed two commits on top:

  • e200203: ASGI is back to how it works on main. 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, and skipping large requests only dropped good samples. The middleware is unchanged, so the duplicate X-Request-Start behavior is also back to first-wins. I removed the FastAPI sample test and the ASGI README paragraph. Django and Flask still skip queue time for large requests.
  • b6fa921: Config.update now coerces MAX_REQUEST_SIZE_BYTES. Metric.for_web compares it to every request's size, so a string, such as one read from an env var, raised a TypeError on every request, and False dropped queue time for any request with a body. A numeric string is now converted to an int, and False turns the cutoff off, as in Ruby. Any other invalid value logs a warning and falls back to 100_000.

CI passes on 3.10–3.14. The ASGI section of the description is now out of date.

@adamlogic
adamlogic merged commit a00afd7 into judoscale:main Dec 1, 2026
5 checks passed
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>
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.

3 participants