Documentation ¶
Index ¶
- Variables
- func GetLogger(r *http.Request) log.Logger
- func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) http.HandlerFunc
- func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc
- func InstrumentLabels(r *http.Request, keys ...string) prometheus.Labels
- func LiveReloader(next http.Handler) http.Handler
- func Logger(next http.Handler) http.Handler
- func LoggerFields(r *http.Request) log.Map
- func LoggerWithOption(options ...LoggerOption) func(http.Handler) http.Handler
- func Metrics(next http.Handler) http.Handler
- func Recoverer(next http.Handler) http.Handler
- type ContextKey
- type LoggerOption
- type LoggerOptionFunc
- type Reloader
Constants ¶
This section is empty.
Variables ¶
var ( // StripSlashes is a middleware that will match request paths with a trailing // slash, strip it from the path and continue routing through the mux, if a route // matches, then it will serve the handler. StripSlashes = middleware.StripSlashes // RequestID is a middleware that injects a request ID into the context of each // request. A request ID is a string of the form "host.example.com/random-0001", // where "random" is a base62 random string that uniquely identifies this go // process, and where the last number is an atomically incremented request // counter. RequestID = middleware.RequestID // RealIP is a middleware that sets a http.Request's RemoteAddr to the results // of parsing either the X-Forwarded-For header or the X-Real-IP header (in that // order). RealIP = middleware.RealIP // NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent // a router (or subrouter) from being cached by an upstream proxy and/or client. NoCache = middleware.NoCache // SetContentType is a middleware that forces response Content-Type. SetContentType = render.SetContentType // GetReqID returns a request ID from the given context if one is present. // Returns the empty string if a request ID cannot be found. GetReqID = middleware.GetReqID // Status sets a HTTP response status code hint into request context at any point // during the request life-cycle. Before the Responder sends its response header // it will check the StatusCtxKey Status = render.Status // Heartbeat endpoint middleware useful to setting up a path like // `/ping` that load balancers or uptime testing external services // can make a request before hitting any routes. It's also convenient // to place this above ACL middlewares as well. Heartbeat = middleware.Heartbeat )
Functions ¶
func InstrumentHandlerCounter ¶
func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) http.HandlerFunc
InstrumentHandlerCounter is a middleware that wraps the provided http.Handler to observe the request result with the provided CounterVec. The CounterVec must have zero, one, or two non-const non-curried labels. For those, the only allowed label names are "code" and "method". The function panics otherwise. Partitioning of the CounterVec happens by HTTP status code and/or HTTP method if the respective instance label names are present in the CounterVec. For unpartitioned counting, use a CounterVec with zero labels.
If the wrapped Handler does not set a status code, a status code of 200 is assumed.
If the wrapped Handler panics, the Counter is not incremented.
See the example for InstrumentHandlerDuration for example usage.
func InstrumentHandlerDuration ¶
func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc
InstrumentHandlerDuration is a middleware that wraps the provided http.Handler to observe the request duration with the provided ObserverVec. The ObserverVec must have zero, one, or two non-const non-curried labels. For those, the only allowed label names are "code" and "method". The function panics otherwise. The Observe method of the Observer in the ObserverVec is called with the request duration in seconds. Partitioning happens by HTTP status code and/or HTTP method if the respective instance label names are present in the ObserverVec. For unpartitioned observations, use an ObserverVec with zero labels. Note that partitioning of Histograms is expensive and should be used judiciously.
If the wrapped Handler does not set a status code, a status code of 200 is assumed.
If the wrapped Handler panics, no values are reported.
Note that this method is only guaranteed to never observe negative durations if used with Go1.9+.
func InstrumentLabels ¶
func InstrumentLabels(r *http.Request, keys ...string) prometheus.Labels
InstrumentLabels returns the instrument labels
func LiveReloader ¶
LiveReloader reloads a webpage
func Logger ¶
Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return.
func LoggerFields ¶
LoggerFields returns the logger's fields
func LoggerWithOption ¶
func LoggerWithOption(options ...LoggerOption) func(http.Handler) http.Handler
LoggerWithOption returns a logger middleware
func Recoverer ¶
Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.
Alternatively, look at https://github.com/pressly/lg middleware pkgs.
Types ¶
type ContextKey ¶
type ContextKey struct {
Name string
}
ContextKey is a value for use with context.WithValue. It's used as a pointer so it fits in an interface{} without allocation. This technique for defining context keys was copied from Go 1.7's new use of context in net/http.
type LoggerOption ¶
LoggerOption represent a logger option
func LoggerOptionWithFields ¶
func LoggerOptionWithFields(kv log.Map) LoggerOption
LoggerOptionWithFields creates a new logger option with fields
type LoggerOptionFunc ¶
LoggerOptionFunc represents a function