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>
This commit is contained in:
19
internal/requestid/requestid.go
Normal file
19
internal/requestid/requestid.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Package requestid provides a shared context key for request IDs,
|
||||
// allowing both client and server packages to access request IDs
|
||||
// without circular imports.
|
||||
package requestid
|
||||
|
||||
import "context"
|
||||
|
||||
type key struct{}
|
||||
|
||||
// NewContext returns a context with the given request ID.
|
||||
func NewContext(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, key{}, id)
|
||||
}
|
||||
|
||||
// FromContext returns the request ID from ctx, or empty string if not set.
|
||||
func FromContext(ctx context.Context) string {
|
||||
id, _ := ctx.Value(key{}).(string)
|
||||
return id
|
||||
}
|
||||
Reference in New Issue
Block a user