go-httpwares
go-httpwares copied to clipboard
Add http.time_ms to all logs
Currently, this http middleware adds a time_ms
field to the log once the request handling is finished. However, we want to be able to get time since the beginning of the request in all logs - including the ones logged in the handler that the middleware is wrapping.
I couldn't find an easy way of doing this. My options were
- the handler code queries the context for
http_ms
, but this is checking an internal behaviour fromgo-httpwares
that is error-prone. - the handler code gets its own time measurements, but that'd be redundant with this middleware.
- add a hook to the logger to add a
http_ms
at every logging. Because you need to provide the logger to the middleware, as long as you use the same logger from your handler you will have all the fields included by the middleware.
The best option here seemed to be 3), which is implemented in this PR.