Replies: 1 comment
-
|
Your understanding is correct — and your instinct to add it as a separate middleware is the right approach. The built-in Adding it to the response is a one-liner you can stack right after r.Use(middleware.RequestID)
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
w.Header().Set("X-Request-Id", reqID)
}
next.ServeHTTP(w, r)
})
})This way:
The client can then log/store the response |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, sorry for a noob question. From what I understand, the x-request-id header is use to correlate request with server log.
Now the RequestID middleware already use the x-request-id header from client if one is sent, so I assume the RequestID here is the same as x-request-id.
My problem is when the client is not send the header to the server,
the server generate one id but the client has no info on the newly generated id, how will I know which request is correlate to which log?
Now what I usually like do is I make another middleware to handle the header setting and use it after the RequestID middleware.
It make more sense to do it this way or, would it be better if the RequestID middleware do this?
Or maybe I completely misunderstand the purpose of this middleware?
Beta Was this translation helpful? Give feedback.
All reactions