exphttp

package module
v0.0.0-...-13a0eb3 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2015 License: MIT Imports: 15 Imported by: 0

README

exphttp

Expose HTTP request/response timing easily via expvar

Documentation

Overview

Package exphttp implements HTTP request/response timing collection for the standard `net/http` package.

Example usage:

func getThePage(w http.ResponseWriter, r *http.Request) {
   fmt.Fprint(w, "neat page")
}

http.HandleFunc("/thepage/", getThePage)

// same page, but now with stats tracked!
http.Handle("/thepage2/", exphttp.NewExpHandler("thepage2", MakeExpHandlerFunc(getThePage)))

Although it's more efficient with a slightly tweaked http.HandlerFunc that returns the response status code. Here the handler is modified to return its status code work with exphttp:

func getThePage2(w http.ResponseWriter, r *http.Request) int {
   fmt.Fprint(w, "neat page")
   return http.StatusOK
}

http.Handle("/thepage2/", exphttp.NewExpHandler("thepage2", getThePage2))

Index

Constants

View Source
const DefaultGranularity = 32

DefaultGranularity is the default level of granularity for recording counters over time. For example, a RateCounter with an interval of 1 minute and a granulartiy of 30 will be accurate to within 2 seconds.

Thus you want to make sure your polling interval is greater than the measurement interval divided by granularity.

Variables

View Source
var DefaultLogger = log.New(os.Stderr, "", log.LstdFlags)

DefaultLogger is used when creating new ExpHandlers, and used to log requests and timing info to Stderr.

Set to nil to disable before calling NewExpHandler()

Functions

func DefaultRecordFunc

func DefaultRecordFunc(x *ExpPoller, key string, value interface{})

Types

type ExpHandler

type ExpHandler struct {
	// Name of the handler/endpoint.
	Name string

	// Stats contains the request/response stats that are exposed.
	Stats *expvar.Map

	// Durations are the time spans for the rate counters. Only parsed once in
	// the first incoming request to populate counters.
	Durations map[string]time.Duration

	// HandlerFunc is the ExpHandlerFunc that is tracked.
	HandlerFunc ExpHandlerFunc

	// Log requests to this logger if non-nil.
	Log *log.Logger
	// contains filtered or unexported fields
}

ExpHandler is an http.Handler that exposes request/response timing information via the `expvar` stdlib package.

func NewExpHandler

func NewExpHandler(name string, h ExpHandlerFunc) *ExpHandler

NewExpHandler creates a new ExpHandler, publishes a new expvar.Map to track it, sets a default Durations={"min": time.Minute}, sets Log=DefaultLogger, and adds name to the exposed "exphttp" map so that stats polling code can auto-discover.

func (*ExpHandler) ServeHTTP

func (e *ExpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

type ExpHandlerFunc

type ExpHandlerFunc func(w http.ResponseWriter, r *http.Request) int

ExpHandlerFunc is a http.HandlerFunc that returns it's own HTTP StatusCode.

func MakeExpHandlerFunc

func MakeExpHandlerFunc(h http.HandlerFunc) ExpHandlerFunc

MakeExpHandlerFunc wraps a http.HandlerFunc so that the response status code is accessible. It is more efficient to update your code to implement ExpHandlerFunc and return the code directly.

type ExpPoller

type ExpPoller struct {
	PluginName string // plugin
	BaseURL    string
	FetchTime  time.Time
	Vars       map[string]json.RawMessage

	RecordFunc func(key string, val interface{})
}

func (*ExpPoller) Fetch

func (x *ExpPoller) Fetch() error

func (*ExpPoller) HTTPStats

func (x *ExpPoller) HTTPStats() error

func (*ExpPoller) MemStats

func (x *ExpPoller) MemStats() error

func (*ExpPoller) RPCStats

func (x *ExpPoller) RPCStats() error

type ExpRPCServer

type ExpRPCServer struct {

	// IntervalLabel is the suffix to append to rate counters.
	IntervalLabel string

	// Interval is the time span to use for rate counters.
	Interval time.Duration

	// Log requests to this logger if non-nil.
	Log *log.Logger
	// contains filtered or unexported fields
}

ExpRPCServer is a wrapped rpc.Server that exposes timing info and request stats for all the RPC calls going through a rpc.Server.

func NewRPCServer

func NewRPCServer(srv *rpc.Server) *ExpRPCServer

NewRPCServer creates a new ExpRPCServer wrapping a rpc.Server, publishes a new "exprpc" expvar.Map to track it, sets a default IntervalLabel="min" and Interval=time.Minute, and sets Log to DefaultLogger.

To register the wrapped RPC endpoint using the same protocol/endpoint as the default rpc.HandleHTTP() method, use:

expServer := exphttp.NewRPCServer(rpc.DefaultServer)
http.HandleFunc("/_goRPC_", expServer.HandleHTTP)

func (*ExpRPCServer) HandleFunc

func (x *ExpRPCServer) HandleFunc(w http.ResponseWriter, req *http.Request)

HandleHTTP implements an http.HandlerFunc that answers RPC requests, and tracks timing info via expvars. It's basically the same as the default rpc.HandleHTTP methods, only you have to register this one manually.

For example, to replace this code:

rpc.Register(myService)
rpc.HandleHTTP()

Use this sequence instead:

rpc.Register(myService)
expServer := exphttp.NewRPCServer(rpc.DefaultServer)
http.HandleFunc("/_goRPC_", expServer.HandleFunc)

type MovingAverage

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

MovingAverage is a thread-safe moving average tracker that uses minimal memory overhead.

func NewAverage

func NewAverage() *MovingAverage

NewAverage makes a new MovingAverage that never rolls over, effectively a standard average counter.

func NewMovingAverage

func NewMovingAverage(interval time.Duration) *MovingAverage

NewMovingAverage makes a new MovingAverage using the interval provided and DefaultGranularity.

func NewMovingAverageWithGranularity

func NewMovingAverageWithGranularity(interval time.Duration, gran int) *MovingAverage

NewMovingAverageWithGranularity makes a new MovingAverage using the interval and granularity settings provided. Granularity controls how accurate the moving average is within an interval, at the expense of increased memory usage (two int64 per gran number of "buckets").

func (*MovingAverage) Add

func (r *MovingAverage) Add(val int64)

Add an event count into the MovingAverage

func (*MovingAverage) Average

func (r *MovingAverage) Average() int64

Average returns the average number of events in the last interval

func (*MovingAverage) String

func (r *MovingAverage) String() string

String returns Average() as a string (to implement expvar.Var)

type RateCounter

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

RateCounter is a thread-safe counter that allows you to count event rates over time with minimal memory overhead.

func NewCounter

func NewCounter() *RateCounter

NewCounter makes a new RateCounter that never rolls over, effectively a standard counter with a tiny bit of overhead.

func NewRateCounter

func NewRateCounter(interval time.Duration) *RateCounter

NewRateCounter makes a new RateCounter using the interval provided and uses granularity = DefaultGranularity.

func NewRateCounterWithGranularity

func NewRateCounterWithGranularity(interval time.Duration, gran int) *RateCounter

NewRateCounterWithGranularity makes a new RateCounter using the interval and granularity settings provided. Granularity controls how accurate the rate is within an interval, at the expense of increased memory usage (one int64 per gran number of "buckets").

func (*RateCounter) Add

func (r *RateCounter) Add(val int64)

Add an even count into the RateCounter

func (*RateCounter) Rate

func (r *RateCounter) Rate() int64

Rate returns the current number of events in the last interval

func (*RateCounter) String

func (r *RateCounter) String() string

String returns Rate() as a string (to implement expvar.Var)

Directories

Path Synopsis
Command getstats polls a expvar endpoint and dumps values for collectd including memstats, exphttp, exprpcs data entries.
Command getstats polls a expvar endpoint and dumps values for collectd including memstats, exphttp, exprpcs data entries.

Jump to

Keyboard shortcuts

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