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:
2026-03-22 21:47:58 +03:00
parent 3395f70abd
commit 49be6f8a7e
4 changed files with 115 additions and 5 deletions

View 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
}