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 (*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 ¶
HandlerWrapper wraps Handler and returns an http.Handler. Useful for chains of middleware like https://github.com/justinas/alice
Click to show internal directories.
Click to hide internal directories.