Documentation
¶
Overview ¶
Package middleware provides common HTTP middleware for the Tinkerbell HTTP server.
Index ¶
- Constants
- Variables
- func Logging(logger logr.Logger) func(http.Handler) http.Handler
- func OTel(operationName string) func(http.Handler) http.Handler
- func Recovery(log logr.Logger) func(http.Handler) http.Handler
- func RequestMetrics() func(http.Handler) http.Handler
- func SourceIP() func(http.Handler) http.Handler
- func WithLogLevel(level int, handler http.Handler) http.Handler
- func XFF(trustedProxies []string) (func(http.Handler) http.Handler, error)
Constants ¶
const ( // LogLevelNever suppresses all logging for a handler (except 5xx responses). LogLevelNever = -1 // LogLevelAlways logs every request at V(0), useful for low-noise // endpoints where visibility matters (e.g. cloud-init / Tootles). LogLevelAlways = 0 // LogLevelDebug logs requests at V(1), the default for handlers that // don't specify a level. Suitable for high-noise routes like the web UI. LogLevelDebug = 1 )
Log-level constants for use with WithLogLevel.
Variables ¶
var Registry = prometheus.NewRegistry()
Registry is the Prometheus registry for HTTP middleware metrics. It is separate from the default registry so that HTTP metrics can be served on a dedicated /http/metrics endpoint.
Functions ¶
func Logging ¶
Logging returns middleware that logs HTTP requests using the provided logger. It logs method, URI, client IP, duration, and response status code.
The log verbosity for each request is determined by the context value set via WithLogLevel. If no level is set the default is LogLevelDebug (V(1)). Responses with status >= 500 are always logged at V(0) regardless of the configured level. A level of LogLevelNever (-1) suppresses all output.
func Recovery ¶
Recovery returns middleware that recovers from panics, logs the panic value and stack trace, and returns a 500 status code.
func RequestMetrics ¶
RequestMetrics returns middleware that instruments HTTP requests with Prometheus metrics. Metrics are registered on Registry so they can be served on a dedicated endpoint as well as aggregated on the combined /metrics endpoint. It is safe to call more than once; metrics are registered exactly once.
func SourceIP ¶
SourceIP returns middleware that captures the original r.RemoteAddr (the TCP connection IP) into the request context. This must be applied as the outermost middleware — before XFF — so that the value is recorded before any header- based rewriting of RemoteAddr. The Logging middleware reads this value to log it as "sourceIP".
func WithLogLevel ¶
WithLogLevel wraps handler so that the Logging middleware uses the given logr V-level for non-error responses. Use the LogLevel* constants.
LogLevelNever (-1): suppress all logging, except 5xx responses LogLevelAlways ( 0): log every request at V(0) LogLevelDebug ( 1): log at V(1), the default
5xx responses are always logged at V(0) regardless of the configured level.
Types ¶
This section is empty.