Documentation
¶
Overview ¶
requestid/logger.go
requestid/propagate.go
requestid/requestid.go
Index ¶
- Constants
- func Client() *http.Client
- func ClientWithBase(base http.RoundTripper) *http.Client
- func ExtractFromResponse(resp *http.Response) string
- func ExtractFromResponseWithHeader(resp *http.Response, header string) string
- func Field(ctx context.Context) zap.Field
- func FromRequest(r *http.Request) string
- func GeneratePrefixed(prefix string) func() string
- func GenerateSequential(prefix string) func() string
- func GenerateShort() string
- func GenerateTimestamp() string
- func GenerateUUID() string
- func Get(ctx context.Context) string
- func Logger(ctx context.Context, logger *zap.Logger) *zap.Logger
- func LoggerFromRequest(r interface{ ... }, logger *zap.Logger) *zap.Logger
- func LoggingMiddleware(logger *zap.Logger) func(next func(context.Context)) func(context.Context)
- func Middleware(cfg Config) func(http.Handler) http.Handler
- func NewRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error)
- func Set(ctx context.Context, requestID string) context.Context
- func SetHeader(ctx context.Context, req *http.Request)
- func SetHeaderWithName(ctx context.Context, req *http.Request, header string)
- func Simple() func(http.Handler) http.Handler
- func ValidateAlphanumeric(s string) bool
- func ValidateHex(s string) bool
- func ValidateUUID(s string) bool
- type Config
- type Transport
Constants ¶
const DefaultHeader = "X-Request-ID"
DefaultHeader is the default HTTP header for request IDs.
Variables ¶
This section is empty.
Functions ¶
func ClientWithBase ¶
func ClientWithBase(base http.RoundTripper) *http.Client
ClientWithBase returns an HTTP client with a custom base transport.
func ExtractFromResponse ¶
ExtractFromResponse gets the request ID from a response header.
func ExtractFromResponseWithHeader ¶
ExtractFromResponseWithHeader gets the request ID from a custom response header.
func Field ¶
Field returns a zap field with the request ID. Returns a no-op field if no request ID is in the context.
func FromRequest ¶
FromRequest retrieves the request ID from an HTTP request.
func GeneratePrefixed ¶
GeneratePrefixed returns a generator that adds a prefix to UUIDs.
func GenerateSequential ¶
GenerateSequential generates a sequential ID with prefix. Format: prefix-timestamp-counter (e.g., "req-1234567890-00001")
func GenerateShort ¶
func GenerateShort() string
GenerateShort generates a short random ID (16 hex chars).
func GenerateTimestamp ¶
func GenerateTimestamp() string
GenerateTimestamp generates a timestamp-based ID with random suffix.
func Get ¶
Get retrieves the request ID from the context. Returns an empty string if no request ID is present.
func Logger ¶
Logger returns a logger with the request ID added as a field. If no request ID is in the context, returns the original logger.
func LoggerFromRequest ¶
LoggerFromRequest returns a logger with the request ID from an HTTP request.
func LoggingMiddleware ¶
LoggingMiddleware returns middleware that adds the request ID to the logger stored in the context. Use with waffle/logging for automatic request ID logging.
func Middleware ¶
Middleware returns request ID middleware with the given configuration.
func NewRequest ¶
NewRequest creates an HTTP request with the request ID from the context.
func Set ¶
Set adds a request ID to a context. Useful for propagating request IDs to background jobs or goroutines.
func SetHeader ¶
SetHeader adds the request ID from context to a request's headers. Useful when you've already created a request and want to add the ID.
func SetHeaderWithName ¶
SetHeaderWithName adds the request ID with a custom header name.
func ValidateAlphanumeric ¶
ValidateAlphanumeric checks if a string contains only alphanumeric characters and hyphens.
func ValidateHex ¶
ValidateHex checks if a string is valid hexadecimal.
func ValidateUUID ¶
ValidateUUID checks if a string looks like a UUID.
Types ¶
type Config ¶
type Config struct {
// Header is the HTTP header to use for the request ID.
// Default: "X-Request-ID"
Header string
// Generator creates new request IDs.
// Default: generates UUIDs.
Generator func() string
// TrustProxy trusts the incoming request ID header if present.
// When false, always generates a new ID.
// Default: true
TrustProxy bool
// Validator validates incoming request IDs.
// If it returns false, a new ID is generated.
// Default: accepts any non-empty string.
Validator func(string) bool
// SetResponseHeader adds the request ID to the response headers.
// Default: true
SetResponseHeader bool
}
Config configures the request ID middleware.
type Transport ¶
type Transport struct {
// Base is the underlying transport. If nil, http.DefaultTransport is used.
Base http.RoundTripper
// Header is the header name to use. Default: "X-Request-ID"
Header string
}
Transport wraps an http.RoundTripper to propagate request IDs to outgoing requests.