go-httpwares icon indicating copy to clipboard operation
go-httpwares copied to clipboard

Add http.time_ms to all logs

Open rubensf opened this issue 5 years ago • 0 comments

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

  1. the handler code queries the context for http_ms, but this is checking an internal behaviour from go-httpwares that is error-prone.
  2. the handler code gets its own time measurements, but that'd be redundant with this middleware.
  3. 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.

rubensf avatar Nov 23 '18 15:11 rubensf