middleware

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestLogger

func RequestLogger(logger *slog.Logger, logLevel slog.Level, formatter RequestLogFormatter) func(http.Handler) http.Handler

RequestLogger logs incoming HTTP requests.

func WithInflightMetrics

func WithInflightMetrics(m *metrics.InflightMetrics) func(next http.Handler) http.Handler

func WithRequestMetrics

func WithRequestMetrics(m metrics.RequestMetrics) func(next http.Handler) http.Handler

WithRequestMetrics creates an http.Handler middleware that measures request count and duration by method, path and status code. The caller must register the metrics with a Prometheus registry.

Note: for most use cases, promhttp.InstrumentHandler is probably enough.

Types

type RequestLogFormatter

type RequestLogFormatter interface {
	FormatRequest(*http.Request, int, time.Duration) []slog.Attr
}

A RequestLogFormatter takes the HTTP request, the resulting HTTP status code and latency and formats the log entry.

var DefaultRequestLogFormatter RequestLogFormatter = &defaultRequestLogFormatter{}

DefaultRequestLogFormatter is the default RequestLogFormatter. It logs the request's HTTP method and the path, the response's status code and the latency of the request.

type RequestLogFormatterFunc

type RequestLogFormatterFunc func(r *http.Request, statusCode int, latency time.Duration) []slog.Attr

The RequestLogFormatterFunc type is an adapter that allows an ordinary function to be used as a RequestLogFormatter.

func (RequestLogFormatterFunc) FormatRequest

func (f RequestLogFormatterFunc) FormatRequest(r *http.Request, statusCode int, latency time.Duration) []slog.Attr

FormatRequest calls f(r, statusCode, latency)

Jump to

Keyboard shortcuts

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