middleware

package
v0.0.0-...-35810fd Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: Apache-2.0 Imports: 35 Imported by: 51

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthenticateUser = Func(func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, ctx, err := user.ExtractOrgIDFromHTTPRequest(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusUnauthorized)
			return
		}
		next.ServeHTTP(w, r.WithContext(ctx))
	})
})

AuthenticateUser propagates the user ID from HTTP headers back to the request's context.

View Source
var BodySizeBuckets = []float64{1 * mb, 2.5 * mb, 5 * mb, 10 * mb, 25 * mb, 50 * mb, 100 * mb, 250 * mb}

BodySizeBuckets defines buckets for request/response body sizes.

Functions

func ClientUserHeaderInterceptor

func ClientUserHeaderInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error

ClientUserHeaderInterceptor propagates the user ID from the context to gRPC metadata, which eventually ends up as a HTTP2 header.

func CountingListener

func CountingListener(l net.Listener, g prometheus.Gauge) net.Listener

CountingListener returns a Listener that increments a Prometheus gauge when a connection is accepted, and decrements the gauge when the connection is closed.

func ExtractRouteName

func ExtractRouteName(ctx context.Context) string

ExtractRouteName returns the route name associated with this request that was previously injected by the RouteInjector middleware or WithRouteName.

This is the same route name used for trace and metric names, and is already suitable for use as a Prometheus label value.

func HTTPGRPCTracingInterceptor

func HTTPGRPCTracingInterceptor(router *mux.Router) grpc.UnaryServerInterceptor

HTTPGRPCTracingInterceptor adds additional information about the encapsulated HTTP request to httpgrpc trace spans.

The httpgrpc client wraps HTTP requests up into a generic httpgrpc.HTTP/Handle gRPC method. The httpgrpc server unwraps httpgrpc.HTTP/Handle gRPC requests into HTTP requests and forwards them to its own internal HTTP router.

By default, the server-side tracing spans for the httpgrpc.HTTP/Handle gRPC method have no data about the wrapped HTTP request being handled.

HTTPGRPCTracer.Wrap starts a child span with span name and tags following the approach in Tracer.Wrap's usage of opentracing-contrib/go-stdlib/nethttp.Middleware and attaches the HTTP server span tags to the parent httpgrpc.HTTP/Handle gRPC span, allowing tracing tooling to differentiate the HTTP requests represented by the httpgrpc.HTTP/Handle spans.

Note that we cannot do this in the httpgrpc Server implementation, as some applications (eg. Mimir's queriers) call Server.Handle() directly, which means we'd attach HTTP-request related span tags to whatever parent span is active in the caller, rather than the /httpgrpc.HTTP/Handle span created by the tracing middleware for requests that arrive over the network.

func IsWSHandshakeRequest

func IsWSHandshakeRequest(req *http.Request) bool

IsWSHandshakeRequest returns true if the given request is a websocket handshake request.

func MakeLabelValue

func MakeLabelValue(path string) string

MakeLabelValue converts a Gorilla mux path to a string suitable for use in a Prometheus label value.

func NewStatsHandler

func NewStatsHandler(receivedPayloadSize, sentPayloadSize *prometheus.HistogramVec, inflightRequests *prometheus.GaugeVec) stats.Handler

NewStatsHandler creates handler that can be added to gRPC server options to track received and sent message sizes.

func NewZeroResponseListener

func NewZeroResponseListener(list net.Listener, log log.Logger) net.Listener

NewZeroResponseListener returns a Listener that logs all connections that encountered io timeout on reads, and were closed before sending any response.

func ServerUserHeaderInterceptor

func ServerUserHeaderInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

ServerUserHeaderInterceptor propagates the user ID from the gRPC metadata back to our context.

func StreamClientInstrumentInterceptor

func StreamClientInstrumentInterceptor(metric *prometheus.HistogramVec, instrumentationOptions ...InstrumentationOption) grpc.StreamClientInterceptor

StreamClientInstrumentInterceptor records duration of streaming gRPC requests client side.

func StreamClientUserHeaderInterceptor

func StreamClientUserHeaderInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error)

StreamClientUserHeaderInterceptor propagates the user ID from the context to gRPC metadata, which eventually ends up as a HTTP2 header. For streaming gRPC requests.

func StreamServerInstrumentInterceptor

func StreamServerInstrumentInterceptor(hist *prometheus.HistogramVec, instrumentationOptions ...InstrumentationOption) grpc.StreamServerInterceptor

StreamServerInstrumentInterceptor instruments gRPC requests for errors and latency.

func StreamServerUserHeaderInterceptor

func StreamServerUserHeaderInterceptor(srv interface{}, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamServerUserHeaderInterceptor propagates the user ID from the gRPC metadata back to our context.

func UnaryClientInstrumentInterceptor

func UnaryClientInstrumentInterceptor(metric *prometheus.HistogramVec, instrumentationOptions ...InstrumentationOption) grpc.UnaryClientInterceptor

UnaryClientInstrumentInterceptor records duration of gRPC requests client side.

func UnaryServerInstrumentInterceptor

func UnaryServerInstrumentInterceptor(hist *prometheus.HistogramVec, instrumentationOptions ...InstrumentationOption) grpc.UnaryServerInterceptor

UnaryServerInstrumentInterceptor instruments gRPC requests for errors and latency.

func WithRouteName

func WithRouteName(r *http.Request, routeName string) *http.Request

WithRouteName annotates r's context with the provided route name.

The provided value must be suitable to use as a Prometheus label value.

This method should generally only be used in tests: in production code, use RouteInjector instead.

Types

type DoNotLogError

type DoNotLogError struct{ Err error }

func (DoNotLogError) Error

func (i DoNotLogError) Error() string

func (DoNotLogError) ShouldLog

func (i DoNotLogError) ShouldLog(_ context.Context) (bool, string)

func (DoNotLogError) Unwrap

func (i DoNotLogError) Unwrap() error

type ErrorHandler

type ErrorHandler struct {
	Code    int
	Handler http.Handler
}

ErrorHandler lets you call an alternate http handler upon a certain response code. Note it will assume a 200 if the wrapped handler does not write anything

func (ErrorHandler) Wrap

func (e ErrorHandler) Wrap(next http.Handler) http.Handler

Wrap implements Middleware

type Func

type Func func(http.Handler) http.Handler

Func is to Interface as http.HandlerFunc is to http.Handler

func NewTimeoutMiddleware

func NewTimeoutMiddleware(dt time.Duration, msg string, log log.Logger) Func

NewTimeoutMiddleware returns a new timeout middleware that returns a 503 Service Unavailable using the http.TimeoutHandler. Note also that the middleware disables the http server write timeout to ensure the two timeouts don't conflict. We disable the server write timeout b/c it's behavior may be unintuitive. See below.

Server.WriteTimeout:

  • does not cancel context and instead allows the request to go until the next write. in practice this means that an http server with a write timeout of 10s may go for significantly longer
  • closes the tcp connection on the next write after the timeout has elapsed instead of sending a meaningful http response
  • allows streaming of http response back to caller

http.TimeoutHandler

  • cancels context allowing downstream code to abandon the request
  • returns a 503 Service Unavailable with the provided message
  • buffers response in memory which may be undesirable for large responses

func (Func) Wrap

func (m Func) Wrap(next http.Handler) http.Handler

Wrap implements Interface

type GRPCServerLog

type GRPCServerLog struct {
	Log log.Logger
	// WithRequest will log the entire request rather than just the error
	WithRequest              bool
	DisableRequestSuccessLog bool
}

GRPCServerLog logs grpc requests, errors, and latency.

func (GRPCServerLog) StreamServerInterceptor

func (s GRPCServerLog) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

StreamServerInterceptor returns an interceptor that logs gRPC requests

func (GRPCServerLog) UnaryServerInterceptor

func (s GRPCServerLog) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

UnaryServerInterceptor returns an interceptor that logs gRPC requests

type HeaderAdder

type HeaderAdder struct {
	http.Header
}

HeaderAdder adds headers to responses

func (HeaderAdder) Wrap

func (h HeaderAdder) Wrap(next http.Handler) http.Handler

Wrap implements Middleware

type Instrument

type Instrument struct {
	Duration          *prometheus.HistogramVec
	PerTenantDuration *prometheus.HistogramVec
	PerTenantCallback PerTenantCallback
	RequestBodySize   *prometheus.HistogramVec
	ResponseBodySize  *prometheus.HistogramVec
	InflightRequests  *prometheus.GaugeVec
}

Instrument is a Middleware which records timings for every HTTP request

func (Instrument) Wrap

func (i Instrument) Wrap(next http.Handler) http.Handler

Wrap implements middleware.Interface

type InstrumentationOption

type InstrumentationOption func(*instrumentationLabel)
var (
	// ReportGRPCStatusOption is an InstrumentationOption that is used for enabling gRPC status codes to be used
	// in instrumentation labels.
	ReportGRPCStatusOption InstrumentationOption = func(instrumentationLabel *instrumentationLabel) {
		instrumentationLabel.reportGRPCStatus = true
	}
)

type Interface

type Interface interface {
	Wrap(http.Handler) http.Handler
}

Interface is the shared contract for all middlesware, and allows middlesware to wrap handlers.

var Identity Interface = Func(func(h http.Handler) http.Handler { return h })

Identity is an Interface which doesn't do anything.

func Merge

func Merge(middlesware ...Interface) Interface

Merge produces a middleware that applies multiple middlesware in turn; ie Merge(f,g,h).Wrap(handler) == f.Wrap(g.Wrap(h.Wrap(handler)))

func PathReplace

func PathReplace(replacement string) Interface

PathReplace replcase Request.RequestURI with the specified string.

func PathRewrite

func PathRewrite(regexp *regexp.Regexp, replacement string) Interface

PathRewrite supports regex matching and replace on Request URIs

type Log

type Log struct {
	Log                      log.Logger
	DisableRequestSuccessLog bool
	LogRequestHeaders        bool // LogRequestHeaders true -> dump http headers at debug log level
	LogRequestAtInfoLevel    bool // LogRequestAtInfoLevel true -> log requests at info log level
	SourceIPs                *SourceIPExtractor
	HTTPHeadersToExclude     map[string]bool
}

Log middleware logs http requests

func NewLogMiddleware

func NewLogMiddleware(log log.Logger, logRequestHeaders bool, logRequestAtInfoLevel bool, sourceIPs *SourceIPExtractor, headersList []string) Log

func (Log) Wrap

func (l Log) Wrap(next http.Handler) http.Handler

Wrap implements Middleware

type OptionalLogging

type OptionalLogging interface {
	// ShouldLog returns whether the error should be logged and the reason. For example, if the error should be sampled
	// return returned reason could be something like "sampled 1/10". The reason, if any, is used to decorate the error
	// both in case the error should be logged or skipped.
	ShouldLog(ctx context.Context) (bool, string)
}

OptionalLogging is the interface that needs be implemented by an error that wants to control whether the log should be logged by GRPCServerLog.

type PerTenantCallback

type PerTenantCallback func(context.Context) string

PerTenantCallback is a function that returns a tenant ID for a given request. When the returned tenant ID is not empty, it is used to label the duration histogram.

type RouteInjector

type RouteInjector struct {
	RouteMatcher RouteMatcher
}

RouteInjector is a middleware that injects the route name for the current request into the request context.

The route name can be retrieved by calling ExtractRouteName.

func (RouteInjector) Wrap

func (i RouteInjector) Wrap(handler http.Handler) http.Handler

type RouteMatcher

type RouteMatcher interface {
	Match(*http.Request, *mux.RouteMatch) bool
}

RouteMatcher matches routes

type SourceIPExtractor

type SourceIPExtractor struct {
	// contains filtered or unexported fields
}

SourceIPExtractor extracts the source IPs from a HTTP request

func NewSourceIPs

func NewSourceIPs(header, regex string, extractAllHosts bool) (*SourceIPExtractor, error)

NewSourceIPs creates a new SourceIPs

func (SourceIPExtractor) Get

func (sips SourceIPExtractor) Get(req *http.Request) string

Get returns any source addresses we can find in the request, comma-separated

type Tracer

type Tracer struct {
	SourceIPs *SourceIPExtractor
}

Tracer is a middleware which traces incoming requests.

func (Tracer) Wrap

func (t Tracer) Wrap(next http.Handler) http.Handler

Wrap implements Interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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