4
0

requestid.go 530 B

123456789101112131415161718
  1. package client
  2. import "context"
  3. type contextKey struct{}
  4. // NewRequestIDContext returns a new context with the given request ID added to the
  5. // context.
  6. func NewRequestIDContext(ctx context.Context, requestID string) context.Context {
  7. return context.WithValue(ctx, contextKey{}, requestID)
  8. }
  9. // RequestIDFromContext returns the request ID from the context if it exists.
  10. // and is not empty.
  11. func RequestIDFromContext(ctx context.Context) (string, bool) {
  12. v, ok := ctx.Value(contextKey{}).(string)
  13. return v, ok && v != ""
  14. }