go-chi / chi

lightweight, idiomatic and composable router for building Go HTTP services
https://go-chi.io
MIT License
18.42k stars 985 forks source link

Duplicate Request ID if Use middleware several times #825

Open mason-liu opened 1 year ago

mason-liu commented 1 year ago

If the request id middleware is called several times for one request, each time it will generate new request id. I have this issue because httplog.RequestLogger enables request id middleware by default and no way to remove. The expected result is, request id should only be generated once for each request even it is called several times, 2 ways I think:

  1. https://github.com/go-chi/chi/blob/master/middleware/request_id.go#L70 request ID should get from context instead of request header
  2. Once generate new request ID, should add it to request header
pkieltyka commented 1 year ago

so you're using httplog.RequestLogger in addition adding middleware.RequestID separately..?

dropwhile commented 10 months ago

@mason-liu you can use the httplog.Handler as a middleware instead of httplog.RequestLogger, if you are already using the RequestID and Recoverer middlewares: https://github.com/go-chi/httplog/blob/a414fec0944146d5f63a935c9ad45975f2c09ce7/httplog.go#L57

VojtechVitek commented 1 month ago

Please have a look at https://github.com/go-chi/traceid. It's an alternative to middleware.RequestID, which handles multiple uses well and also adds service-to-service tracing capabilities.