Documentation
¶
Index ¶
- Constants
- func AddRequestIDs(h http.Handler) http.Handler
- func Error(w http.ResponseWriter, error string, code int)
- func ErrorWithStatus(err error, status int) error
- func Errors(w http.ResponseWriter, errors []string, code int)
- func HandlerWithContext(ctx context.Context, next http.Handler) http.Handler
- func Log(args ...interface{})
- func LogRequests(h http.Handler) http.Handler
- func Logger(req *http.Request) logrus.FieldLogger
- type ErrorResponse
- type Handler
- type IDGenerator
- type RequestCounter
- type ResponseWriter
- type Server
Constants ¶
const (
HeaderRequestID = "X-Request-Id"
)
Variables ¶
This section is empty.
Functions ¶
func AddRequestIDs ¶
AddRequestIDs wraps an http.Handler, adding an X-Request-Id header to each request that doesn't already have one.
func Error ¶
func Error(w http.ResponseWriter, error string, code int)
func Errors ¶
func Errors(w http.ResponseWriter, errors []string, code int)
func HandlerWithContext ¶
HandlerWithContext returns an http.Handler that changes the request context to ctx (replacing http.Server's default context.Background()), then calls next.
func Log ¶
func Log(args ...interface{})
Log calls log.Println but first transforms strings so they are safer to write in logs (e.g., 'foo"bar' becomes '"foo\"bar"'). Arguments that aren't strings and don't have a (String() string) method are left alone.
func LogRequests ¶
LogRequests wraps an http.Handler, logging each request and response.
func Logger ¶
func Logger(req *http.Request) logrus.FieldLogger
Types ¶
type Handler ¶
type Handler interface { http.Handler // Returns an http.Handler that serves the Handler's metrics // data at /metrics and /metrics.json, and passes other // requests through to next. ServeAPI(token string, next http.Handler) http.Handler }
func Instrument ¶
Instrument returns a new Handler that passes requests through to the next handler in the stack, and tracks metrics of those requests.
For the metrics to be accurate, the caller must ensure every request passed to the Handler also passes through LogRequests(...), and vice versa.
If registry is nil, a new registry is created.
If logger is nil, logrus.StandardLogger() is used.
type IDGenerator ¶
type IDGenerator struct { // Prefix is prepended to each returned ID. Prefix string // contains filtered or unexported fields }
IDGenerator generates alphanumeric strings suitable for use as unique IDs (a given IDGenerator will never return the same ID twice).
func (*IDGenerator) Next ¶
func (g *IDGenerator) Next() string
Next returns a new ID string. It is safe to call Next from multiple goroutines.
type RequestCounter ¶
type RequestCounter interface { http.Handler // Current() returns the number of requests in progress. Current() int // Max() returns the maximum number of concurrent requests // that will be accepted. Max() int }
RequestCounter is an http.Handler that tracks the number of requests in progress.
func NewRequestLimiter ¶
func NewRequestLimiter(maxRequests int, handler http.Handler, reg *prometheus.Registry) RequestCounter
NewRequestLimiter returns a RequestCounter that delegates up to maxRequests at a time to the given handler, and responds 503 to all incoming requests beyond that limit.
"concurrent_requests" and "max_concurrent_requests" metrics are registered with the given reg, if reg is not nil.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter WroteStatus() int WroteBodyBytes() int Sniffed() []byte }
func WrapResponseWriter ¶
func WrapResponseWriter(orig http.ResponseWriter) ResponseWriter
type Server ¶
type Server struct { http.Server Addr string // host:port where the server is listening. // contains filtered or unexported fields }
func (*Server) Close ¶
Close shuts down the server and returns when it has stopped.
func (*Server) Start ¶
Start is essentially (*http.Server)ListenAndServe() with two more features: (1) by the time Start() returns, Addr is changed to the address:port we ended up listening to -- which makes listening on ":0" useful in test suites -- and (2) the server can be shut down without killing the process -- which is useful in test cases, and makes it possible to shut down gracefully on SIGTERM without killing active connections.