StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 5 · Middleware

Request Timing Logger

easy0%0 of 1 solved

Problem statement

Complete InvokeAsync in RequestTimingMiddleware so that every request runs through the rest of the pipeline untouched and leaves exactly one log entry behind it, carrying the request method, the request path, the response status code and the elapsed time. The status code and the elapsed time are only knowable once the rest of the pipeline has returned, so the entry belongs on the way out and not on the way in.

Sample Data

The order collection is pre-seeded with three orders, two of them open and one shipped, and a stored order receives the next id after the ones already there. Here is an example of an order record:

{
  "id": 2,
  "customer": "Globex",
  "amount": 80.50,
  "status": "shipped"
}

The endpoints shown under Sample Requests and Responses are the traffic the middleware has to observe, and each one keeps answering exactly as it does there once the middleware is in place.

Service

The Timing Entry

Every request leaves one entry behind it, written through the logger the middleware holds:

  • The category is RequestTimingApi.Middleware.RequestTimingMiddleware.
  • The level is Information.
  • The message template is HTTP {Method} {Path} responded {StatusCode} in {ElapsedMilliseconds} ms.

The four values are passed as the arguments of that template, in that order, so each one is recorded as a named property of the entry and not only as text inside it:

  • Method is the request method, for example GET, POST or DELETE.
  • Path is the request path only, so /orders?status=open is recorded as /orders.
  • StatusCode is the status code the response carries after the rest of the pipeline returned, so a request that ends in 201, 204, 400 or 404 is recorded with that number and not with 200.
  • ElapsedMilliseconds is the time the whole call took, in whole milliseconds, and it is never negative. It is the time this particular call took and not a number chosen from the path, so two calls to the same endpoint do not have to report the same number. It reflects the work the request actually did, so a slow endpoint records at least the time it spends, a fast one records far less, and the same path records a larger value when the same call takes longer.

RequestTimingMiddleware

Implement InvokeAsync. The class holds the delegate that carries the rest of the pipeline and the logger the entry is written through, and every request reaches InvokeAsync first, so the whole rest of the pipeline runs inside it.

  • InvokeAsync:
    • Start measuring the elapsed time before the rest of the pipeline runs.
    • Always invoke the rest of the pipeline, for every request, including a request that matches no endpoint and a request that ends in a 400 or a 404.
    • Never short circuit and never change the status code, the headers or the body that the rest of the pipeline produced, so a response carries only the headers its own endpoint set and nothing added around it.
    • Write exactly one entry, and write it once the rest of the pipeline has returned.
    • Leave one entry per request, so several requests leave their entries in the order the requests were handled.
Sample Requests & Responses

The application already serves an order collection, a daily report and a couple of diagnostics routes. Every call through any of them answers exactly as it did before and leaves one entry behind it.

GET /orders answers 200 with the stored orders in id order, and logs:

HTTP GET /orders responded 200 in 4 ms

The other calls log the same way, each recording its own method, path, status and elapsed time:

HTTP GET /orders responded 200 in 2 ms
HTTP GET /orders/2 responded 200 in 1 ms
HTTP GET /orders/99 responded 404 in 1 ms
HTTP POST /orders responded 201 in 3 ms
HTTP POST /orders responded 400 in 1 ms
HTTP DELETE /orders/1 responded 204 in 2 ms
HTTP DELETE /orders/99 responded 404 in 1 ms
HTTP GET /reports/daily responded 200 in 304 ms
HTTP GET /diagnostics/ping responded 200 in 1 ms
HTTP GET /not-a-route responded 404 in 1 ms

Reading down that list:

  • The second entry came from GET /orders?status=open, and Path records /orders alone, the query string being no part of it.
  • POST /orders stores an order with an amount greater than zero and answers 201, while an amount of 0 or of -5.00 is turned away with 400 and an amount of 0.01 is stored.
  • DELETE /orders/1 answers 204 and the order is gone from GET /orders afterwards, which then reports orders 2 and 3 alone. An unknown id answers 404.
  • GET /reports/daily deliberately takes about 300 milliseconds, so it is the one that shows a real elapsed time. Calling it twice records two measured values rather than the same number twice.
  • GET /diagnostics/ping counts one call and reports the running total, which is how a request is proven to have reached its endpoint. GET /diagnostics/ping-count reports that total without counting a call, so it reads 3 after three calls to /diagnostics/ping.
  • GET /not-a-route matches no endpoint, and is answered 404 by the framework and logged like every other request, recording Path as /not-a-route and StatusCode as 404 in its named properties.

More in Middleware

See the module
Maintenance Mode SwitchThis problem covers putting a switch in front of the whole application, so that while it is on every request is turned away with the same notice and told when to come back, and while it is off nothing about the normal answers changes.—No data yeteasyPipeline Order RepairThis problem covers putting the stages a request passes through into the right order, so failures anywhere are caught and turned into a proper answer, and a protected endpoint is guarded before it ever runs.—No data yeteasyCorrelation ID PipelineThis problem covers giving every request one id that follows it all the way through the application, so each stage and each log line reports the same one, and it comes back on the response whatever happened to the request.—No data yeteasy

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.