mmw

package module
v0.0.0-...-225ad5c Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2015 License: Apache-2.0 Imports: 3 Imported by: 1

README

go-metrics-middleware

GoDocBuild Status

Simple http middleware for go-metrics.

See examples and godoc

Documentation

Overview

package mmw provides a generic http.Handler middleware for go-metrics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

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

Middleware is simple wrapper around go-metrics.

func New

func New(sink *metrics.Metrics) *Middleware

New creates a new Middleware

func (*Middleware) Handler

func (mw *Middleware) Handler(handler http.Handler, key ...string) *metricsHandler

Handler creates a new metrics handler that implements http.Handler. This wraps a handler

Example
// create a sink to use
inm := metrics.NewInmemSink(10*time.Millisecond, 50*time.Millisecond)

// create a default metrics config, without hostname
conf := metrics.DefaultConfig("test")
conf.EnableHostname = false

// now use that config and sink with metrics
m, _ := metrics.New(conf, inm)

// simple http handler
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	w.WriteHeader(200)
	fmt.Fprint(w, "Hello World\n")
})

// create a new mw wrapping the metrics
mw := New(m)

// wrap the handler and add to a route
http.Handle("/foo", mw.Handler(handler, "testing"))

// every time "/foo" is access, the middleware will emit two metrics
// it will increment the counter "test.testing.count"
// and add a time sample to "test.testing.time"

http.Handle("/bar", mw.Handler(handler, "another"))

// every time "/bar" is access, the middleware will emit two metrics
// it will increment the counter "test.another.count"
// and add a time sample to "test.another.time"

http.ListenAndServe(":8080", nil)
Output:

func (*Middleware) HandlerFunc

func (mw *Middleware) HandlerFunc(f func(http.ResponseWriter, *http.Request), key ...string) *metricsHandler

HandlerFunc is an adapter that allows "normal" functions to be used as handlers.

func (*Middleware) HandlerWrapper

func (mw *Middleware) HandlerWrapper(key ...string) func(http.Handler) http.Handler

HandlerWrapper wraps Handler and returns an http.Handler. Useful for chains of middleware like https://github.com/justinas/alice

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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