pomerium/internal/telemetry/requestid/requestid.go
Caleb Doxsey 89a105c8e6
authorize: add request id to context (#3497)
* authorize: add request id to context

* fix context keys
2022-07-26 14:34:48 -06:00

33 lines
778 B
Go

// Package requestid has functions for working with x-request-id in http/gRPC requests.
package requestid
import (
"context"
"github.com/google/uuid"
"github.com/pomerium/pomerium/pkg/encoding/base58"
)
const headerName = "x-request-id"
type contextKey struct{}
// WithValue returns a new context from the parent context with a request id value set.
func WithValue(parent context.Context, requestID string) context.Context {
return context.WithValue(parent, contextKey{}, requestID)
}
// FromContext gets the request id from a context.
func FromContext(ctx context.Context) string {
if id, ok := ctx.Value(contextKey{}).(string); ok {
return id
}
return ""
}
// New creates a new request id.
func New() string {
id := uuid.New()
return base58.Encode(id[:])
}