Files
httpx/middleware/requestid.go
Aleksey Shakhmatov 49be6f8a7e Add client RequestID middleware for cross-service propagation
Introduces internal/requestid package with shared context key to avoid
circular imports between server and middleware packages. Server's
RequestID middleware now uses the shared key. Client middleware picks up
the ID from context and sets X-Request-Id on outgoing requests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 21:47:58 +03:00

24 lines
723 B
Go

package middleware
import (
"net/http"
"git.codelab.vc/pkg/httpx/internal/requestid"
)
// RequestID returns a middleware that propagates the request ID from the
// request context to the outgoing X-Request-Id header. This pairs with
// the server.RequestID middleware: the server stores the ID in the context,
// and the client middleware forwards it to downstream services.
func RequestID() Middleware {
return func(next http.RoundTripper) http.RoundTripper {
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
if id := requestid.FromContext(req.Context()); id != "" {
req = req.Clone(req.Context())
req.Header.Set("X-Request-Id", id)
}
return next.RoundTrip(req)
})
}
}