I am working on a go server application based on go-chi v5
, a http process pipeline is built up with middlewares and some handler functions, and http request flows in this pipeline, from middlewares to handler.
The problem is in middleware some information is unknown, those information can only be known later. For example, I have an instrumentation middleware, which measures the request handling time (egress – ingress), and logs the error when request handling hit any errors, obviously those kind of information is only available when request reaches the business logic module, when instrumentation middleware receives the request, it has no way to know how long does process current request take or whether current request will encounter any errors.
However, in golang the only way to pass information when process http request is using the context
object, the information flows from upstream to downstream, I can add information like requestID
using context.WithValue
, so downstream modules can use the key to retrieve the requestID
out from context
object, but I cannot pass the information from downstream to upstream.
Seems I need a mechanism to setup a callback hook in http process pipeline, when request pops up from the stack/pipeline, but I cannot find such a mechanism, does anyone have a good suggestion on this? Thanks.