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 Errorf(status int, tmpl string, args ...interface{}) error
- func Errors(w http.ResponseWriter, errors []string, code int)
- func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler
- func Inspect(registry *prometheus.Registry, authToken string, next http.Handler) http.Handler
- func Log(args ...interface{})
- func LogRequests(h http.Handler) http.Handler
- func Logger(req *http.Request) logrus.FieldLogger
- func SetResponseLogFields(ctx context.Context, fields logrus.Fields)
- type ErrorResponse
- type HTTPStatusError
- type Handler
- type IDGenerator
- type RequestLimiter
- type RequestQueue
- 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 HandlerWithDeadline ¶
HandlerWithDeadline cancels the request context if the request takes longer than the specified timeout without having its connection hijacked.
If timeout is 0, there is no deadline: HandlerWithDeadline is a no-op.
func Inspect ¶
Inspect serves a report of current requests at "GET /_inspect/requests", and passes other requests through to the next handler.
If registry is not nil, Inspect registers metrics about current requests.
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 RequestLimiter ¶
type RequestLimiter struct { Handler http.Handler // Queue determines which queue a request is assigned to. Queue func(req *http.Request) *RequestQueue // Priority determines queue ordering. Requests with higher // priority are handled first. Requests with equal priority // are handled FIFO. If Priority is nil, all requests are // handled FIFO. Priority func(req *http.Request, queued time.Time) int64 // "concurrent_requests", "max_concurrent_requests", // "queued_requests", and "max_queued_requests" metrics are // registered with Registry, if it is not nil. Registry *prometheus.Registry // contains filtered or unexported fields }
RequestLimiter wraps http.Handler, limiting the number of concurrent requests being handled by the wrapped Handler. Requests that arrive when the handler is already at the specified concurrency limit are queued and handled in the order indicated by the Priority function.
Caller must not modify any RequestLimiter fields after calling its methods.
func (*RequestLimiter) ServeHTTP ¶
func (rl *RequestLimiter) ServeHTTP(resp http.ResponseWriter, req *http.Request)
type RequestQueue ¶
type RequestQueue struct { // Label for metrics. No two queues should have the same label. Label string // Maximum number of requests being handled at once. Beyond // this limit, requests will be queued. MaxConcurrent int // Maximum number of requests in the queue. Beyond this limit, // the lowest priority requests will return 503. MaxQueue int // Return 503 for any request for which Priority() returns // MinPriority if it spends longer than this in the queue // before starting processing. MaxQueueTimeForMinPriority time.Duration // contains filtered or unexported fields }
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.