middlewares

package module
v0.0.0-...-3961898 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2018 License: MIT Imports: 11 Imported by: 6

README

Middlewares

Some common middlewares I use in multiple projects

Contains:

  • Gzip: gzips responses
  • Audit: go-metric capturing with yammer metrics compatible output
  • Healthchecks: go-metrics healthchecks with yammer metrics compatible output
  • Profiling: use pprof
  • Recovery: handle panics

Documentation

Index

Constants

View Source
const (
	BestCompression    = gzip.BestCompression
	BestSpeed          = gzip.BestSpeed
	DefaultCompression = gzip.DefaultCompression
	NoCompression      = gzip.NoCompression
)

These compression constants are copied from the compress/gzip package.

Variables

This section is empty.

Functions

func DefaultStack

func DefaultStack(appInfo AppInfo, lgr Logger, orig http.Handler) http.Handler

DefaultStack sets up the default middlewares

func Gzip

func Gzip(level int, next http.Handler) http.Handler

Gzip returns a handler which will handle the Gzip compression in ServeHTTP. Valid values for level are identical to those in the compress/gzip package.

func GzipMW

func GzipMW(level int) alice.Constructor

GzipMW returns a middleware which will handle the Gzip compression in ServeHTTP. Valid values for level are identical to those in the compress/gzip package.

func NewAuditMW

func NewAuditMW(info AppInfo, logger Logger) alice.Constructor

NewAuditMW returns a new Audit middleware

func NewHealthChecksMW

func NewHealthChecksMW(basePath string) alice.Constructor

NewHealthChecksMW creates a new health check middleware at the specified path

func NewProfiler

func NewProfiler(next http.Handler) http.Handler

NewProfiler creates a middleware for profiling

func NewRecoveryMW

func NewRecoveryMW(appName string, lgr Logger) alice.Constructor

NewRecoveryMW returns a new instance of Recovery middleware which traps panics

Types

type AppInfo

type AppInfo struct {
	Name     string `json:"name"`
	Version  string `json:"version"`
	Commit   string `json:"commit"`
	BasePath string `json:"basePath"`
	Pid      int    `json:"pid"`
}

AppInfo the information describing the component for this API

type Audit

type Audit struct {
	// logger is the Logger instance used to log messages with the Audit middleware
	Logger Logger
	// contains filtered or unexported fields
}

Audit is a middleware handler that logs the request as it goes in and the response as it goes out.

func NewAudit

func NewAudit(info AppInfo, logger Logger, next http.Handler) *Audit

NewAudit returns a new Audit instance

func (*Audit) ServeHTTP

func (l *Audit) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type CounterMetric

type CounterMetric struct {
	Count int64 `json:"count"`
}

CounterMetric represents a value that increases or decreases

type GaugeFloat64Metric

type GaugeFloat64Metric struct {
	Value float64 `json:"value"`
}

GaugeFloat64Metric represents a float64 measurement

type GaugeMetric

type GaugeMetric struct {
	Value int64 `json:"value"`
}

GaugeMetric represents measurements.

type HealtCheckData

type HealtCheckData struct {
	Error string `json:"error"`
}

HealtCheckData shows error status if any for a health check

type HealthChecks

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

HealthChecks is a middleware that serves healthcheck information

func NewHealthChecks

func NewHealthChecks(basePath string, next http.Handler) *HealthChecks

NewHealthChecks creates a new health check middleware at the specified path

func (*HealthChecks) ServeHTTP

func (h *HealthChecks) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP is the middleware interface implementation

type HistogramMetric

type HistogramMetric struct {
	Count  int64   `json:"count"`
	Min    int64   `json:"min"`
	Max    int64   `json:"max"`
	Mean   float64 `json:"mean"`
	StdDev float64 `json:"stdDev"`
	Median float64 `json:"median"`
	P75    float64 `json:"p75"`
	P95    float64 `json:"p95"`
	P99    float64 `json:"p99"`
	P999   float64 `json:"p999"`
	Unit   string  `json:"unit,omitempty"`
}

HistogramMetric shows a histogram metric

type Logger

type Logger interface {
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Errorf(string, ...interface{})
}

Logger interface to allow different logging libaries to be used with this middleware

type MeterMetric

type MeterMetric struct {
	Count int64   `json:"count"`
	M1    float64 `json:"m1"`
	M5    float64 `json:"m5"`
	M15   float64 `json:"m15"`
	Mean  float64 `json:"mean"`
}

MeterMetric represents a metered value with 1-minute, 5-minute and 15 minute averages as well as a mean

type Profiler

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

Profiler exposes net/http/pprof as a middleware

func (*Profiler) ServeHTTP

func (p *Profiler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type Recovery

type Recovery struct {
	Logger     Logger
	PrintStack bool
	// contains filtered or unexported fields
}

Recovery is a middleware that recovers from any panics and writes a 500 if there was one.

func NewRecovery

func NewRecovery(appName string, lgr Logger, next http.Handler) *Recovery

NewRecovery returns a new instance of Recovery

func (*Recovery) ServeHTTP

func (rec *Recovery) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type TimerMetric

type TimerMetric struct {
	Rate     MeterMetric     `json:"rate"`
	Duration HistogramMetric `json:"duration"`
}

TimerMetric is a combination of a meter metric and a histogram metric, typically used for tracing time spent and call counts for example

Jump to

Keyboard shortcuts

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