StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 13 · Middleware

Request Timing Middleware

mediumDjango MiddlewareLogging—No data yet

Problem statement

In this Django project, implement the per-request log operations keeps asking for. RequestTimingMiddleware in app/middleware.py is registered and wired, and it hands every request straight down the stack without recording anything, so when a page is slow nobody can say which path it was, what came back, or how long it took. Fill the slot in, and do it without changing a single response on the way out.

Requirements

Implement:

  • the body of __call__ on RequestTimingMiddleware in app/middleware.py

The following files are already implemented and provided: app/views.py, app/urls.py, app/probes.py and project/settings.py. MIDDLEWARE already lists app.middleware.RequestTimingMiddleware first, so this middleware wraps the whole stack and whatever it measures is everything that happens below it; project/settings.py is read-only, so keep the class in that module and keep its name. LOGGING already points a logger named app.requests at the console, so one line prints per entry while the server runs. app/probes.py holds StampMiddleware, a given middleware the tests mount below yours to watch whether the request really reaches it. The routes are given too: / names the port, /reports/quick/ answers 200 with the body quick report ready and the header X-Report-Id: r-1001 to any method, /reports/slow/ answers 200 after sleeping 50ms inside the view, and /reports/missing/ answers 404 with the body report not found and the header X-Report-Id: r-404.

1. One entry per request

  • Every request leaves exactly one record on the logger app.requests at level INFO: not one going in and another coming out.
  • The status belongs to the response, so the entry cannot be written until the rest of the stack has answered.
  • Nothing is filtered. A 404, and a path that matched no route at all, gets an entry like anything else.

2. The four fields of the entry

  • The field names are method, path, status and elapsed, exactly those four and no others.
  • method is the method of the request, so a GET reads GET and a POST to the same route reads POST.
  • path is the path that was asked for.
  • status is the status the response carries, a 404 included.
  • Fields attached with extra={...} land on the record as attributes, and fields written as key=value pairs in the message are parsed back out of it; either shape is read the same way. Separators, field order, quoting and any wording around the fields are not graded.

3. elapsed is a measurement, not a constant

  • elapsed is how long the request took, as a number, and it is never negative.
  • It covers the handling below: the clock is read before the rest of the stack runs and again after it returns, so the work the view did is inside the measurement.
  • /reports/slow/ sleeps 50ms inside the view and reports at least 0.04, while /reports/quick/ answers at once and reports less than half of what /reports/slow/ reports. The two routes are compared against each other, so the unit is yours: seconds, milliseconds, whatever you log it in.

4. The response goes out exactly as it came back

  • The response returned from below is the one returned to the client, not a copy and not a replacement: its status, body and headers all survive, for the 200 and for the 404 alike.

5. The stack below still runs

  • get_response is called on every request, whatever the path and whatever comes back. A middleware that answers on its own has cut off everything beneath it, view included, and the probe's X-Stamped-By: probe header is then missing from the response.

Notes

  • A duration read after the view has already finished is 0, which is the one answer that is never true, and a single clock reading is not a duration at all.
  • Prefer a monotonic clock for a duration (time.perf_counter or time.monotonic) over time.time, whose wall clock can be moved backwards underneath you and hand you a negative length. A clock change is not something a test run can arrange, so this is advice rather than a requirement.
  • Only app/middleware.py changes; there is no wiring left to do.
  • No caching, no sessions, no messages. This is one middleware and one log entry.

More in Middleware

See the module
Maintenance Mode GateDuring releases the site should return a maintenance response, except for staff who need to verify the deploy. Write the middleware that short-circuits with the defined 503 before the view runs, exempting staff users and the allowlisted paths.—No data yetmediumException Capture OrderingUnhandled view errors answer with a raw 500 that carries no trace id, and a request has nothing tying its log lines together. Write a middleware that stamps every request with an id and turns an unhandled view exception into the defined JSON error response carrying it, then register it in the right place in the stack.—No data yetmediumObservable Request PipelineThe app needs one coherent request pipeline - identity, timing, maintenance gating, and error conversion working together in the right order - and currently has none of it. Write the layers that assign a request id, log one timed entry carrying it, gate general traffic with the defined 503 while staff pass, and convert an unhandled view exception into the defined error response, then register them so every concern sees what it must.0%0 of 1 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.