requestid

package
v0.1.36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

requestid/logger.go

requestid/propagate.go

requestid/requestid.go

Index

Constants

View Source
const DefaultHeader = "X-Request-ID"

DefaultHeader is the default HTTP header for request IDs.

Variables

This section is empty.

Functions

func Client

func Client() *http.Client

Client returns an HTTP client that propagates request IDs.

func ClientWithBase

func ClientWithBase(base http.RoundTripper) *http.Client

ClientWithBase returns an HTTP client with a custom base transport.

func ExtractFromResponse

func ExtractFromResponse(resp *http.Response) string

ExtractFromResponse gets the request ID from a response header.

func ExtractFromResponseWithHeader

func ExtractFromResponseWithHeader(resp *http.Response, header string) string

ExtractFromResponseWithHeader gets the request ID from a custom response header.

func Field

func Field(ctx context.Context) zap.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

func FromRequest(r *http.Request) string

FromRequest retrieves the request ID from an HTTP request.

func GeneratePrefixed

func GeneratePrefixed(prefix string) func() string

GeneratePrefixed returns a generator that adds a prefix to UUIDs.

func GenerateSequential

func GenerateSequential(prefix string) func() string

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 GenerateUUID

func GenerateUUID() string

GenerateUUID generates a UUID v4.

func Get

func Get(ctx context.Context) string

Get retrieves the request ID from the context. Returns an empty string if no request ID is present.

func Logger

func Logger(ctx context.Context, logger *zap.Logger) *zap.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

func LoggerFromRequest(r interface{ Context() context.Context }, logger *zap.Logger) *zap.Logger

LoggerFromRequest returns a logger with the request ID from an HTTP request.

func LoggingMiddleware

func LoggingMiddleware(logger *zap.Logger) func(next func(context.Context)) func(context.Context)

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

func Middleware(cfg Config) func(http.Handler) http.Handler

Middleware returns request ID middleware with the given configuration.

func NewRequest

func NewRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error)

NewRequest creates an HTTP request with the request ID from the context.

func Set

func Set(ctx context.Context, requestID string) context.Context

Set adds a request ID to a context. Useful for propagating request IDs to background jobs or goroutines.

func SetHeader

func SetHeader(ctx context.Context, req *http.Request)

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

func SetHeaderWithName(ctx context.Context, req *http.Request, header string)

SetHeaderWithName adds the request ID with a custom header name.

func Simple

func Simple() func(http.Handler) http.Handler

Simple returns middleware with default configuration.

func ValidateAlphanumeric

func ValidateAlphanumeric(s string) bool

ValidateAlphanumeric checks if a string contains only alphanumeric characters and hyphens.

func ValidateHex

func ValidateHex(s string) bool

ValidateHex checks if a string is valid hexadecimal.

func ValidateUUID

func ValidateUUID(s string) bool

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.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration.

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.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL