http

package
v0.0.0-...-ab88fb7 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2017 License: BSD-3-Clause Imports: 4 Imported by: 0

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

func NewMiddleware(hooks Hooks, resolver Resolver) func(http.Handler) http.Handler

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) {}

func WithEndpoint

func WithEndpoint(r *http.Request, endpoint string) *http.Request

WithEndpoint returns a new shallow copied request with the endpoint set in the request's context.

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

type Resolver interface {
	Resolve(*http.Request) string
}

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

type ResolverFunc func(*http.Request) string

ResolverFunc is a helper type for creating a Resolver from a function.

func (ResolverFunc) Resolve

func (fn ResolverFunc) Resolve(r *http.Request) string

Resolve calls fn on ther request and returns the result.

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.

Jump to

Keyboard shortcuts

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