middleware

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package middleware provides common HTTP middleware for the Tinkerbell HTTP server.

Index

Constants

View Source
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

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

func Logging(logger logr.Logger) func(http.Handler) http.Handler

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 OTel

func OTel(operationName string) func(http.Handler) http.Handler

OTel returns middleware that wraps an http.Handler with OpenTelemetry instrumentation.

func Recovery

func Recovery(log logr.Logger) func(http.Handler) http.Handler

Recovery returns middleware that recovers from panics, logs the panic value and stack trace, and returns a 500 status code.

func RequestMetrics

func RequestMetrics() func(http.Handler) http.Handler

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

func SourceIP() func(http.Handler) http.Handler

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

func WithLogLevel(level int, handler http.Handler) http.Handler

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.

func XFF

func XFF(trustedProxies []string) (func(http.Handler) http.Handler, error)

XFF returns middleware that processes X-Forwarded-For headers. If trustedProxies is empty, the middleware is a no-op.

Types

This section is empty.

Jump to

Keyboard shortcuts

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