Documentation
¶
Overview ¶
Package http contains middleware for monitoring net/http handlers.
The provided middleware hooks into the request/response cycle in order to provide data to a provided set of hooks. These hooks are responsible for persisting metrics on behalf of the middleware.
The middleware also resolves requests to endpoint names. Endpoint names are intended to be low cardinality and should contain no dynamic data. This allows metrics to be gathered on each endpoint exposed by a handler.
Example usage:
// Get hooks and resolver for the middleware. hooks := prom.NewHooks(nil) resolver := NewContextResolver() // Create the middleware. mw := NewMiddleware(hooks, resolver) // Wrap an existing handler with the middleware. handler = mw(handler)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMiddleware ¶
NewMiddleware creates a standard net/http middleware which calls the provided hooks during the request cycle.
Example ¶
package main
import (
"net/http"
mhttp "github.com/zenreach/compass/http"
)
func main() {
// Create a handler to respond with "Hello, world!"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("Hello, world!"))
})
// To keep this simply our hooks won't do anything.
hooks := &NilHooks{}
// Create a resolver to name our endpoint "hello".
resolver := mhttp.ResolverFunc(func(*http.Request) string {
return "hello"
})
// Start the "Hello, world!" HTTP server with our middleware.
mw := mhttp.NewMiddleware(hooks, resolver)
http.ListenAndServe(":8080", mw(handler))
}
// NilHooks implements http.Hooks as a set of noops.
type NilHooks struct{}
func (*NilHooks) RequestHook(endpoint string, req *http.Request) {}
func (*NilHooks) ResponseHook(endpoint string, req *http.Request, res *mhttp.Response) {}
Types ¶
type Hooks ¶
type Hooks interface {
// RequestHook is called when the request is received and prior to calling
// the underlying handler's ServeHTTP function.
RequestHook(endpoint string, req *http.Request)
// ResponseHook is called after the underlying handler's ServeHTTP
// function.
ResponseHook(endpoint string, req *http.Request, res *Response)
}
Hooks are called by the HTTP handler to report metrics.
type Resolver ¶
Resolver generates an endpoint name from a request.
func NewContextResolver ¶
func NewContextResolver() Resolver
NewContextResolver returns a resolver which extracts the endpoint name from the request's context. The endpoint is set using the WithEndpoint function.
If the value is not set in the context or contains an empty string then "other" is returned.
type ResolverFunc ¶
ResolverFunc is a helper type for creating a Resolver from a function.
type Response ¶
type Response struct {
// Response time. This is the time it took to interpret the request and
// generate a response.
Duration time.Duration
// HTTP status code sent in the response.
StatusCode int
// Size of the response in bytes.
ContentLength int64
// Header sent in the response.
Header http.Header
}
Response contains details pertaining to the HTTP reponse.