pomerium/internal/telemetry/requestid/requestid.go
Caleb Doxsey 500405512f
dependencies: vendor base58, remove shortuuid (#2739)
* vendor base58

* remove shortuuid
2021-11-02 09:23:15 -06:00

33 lines
773 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"
var 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[:])
}