...

Package httpsnoop

import "github.com/felixge/httpsnoop"
Overview
Index
Subdirectories

Overview ▾

Package httpsnoop provides an easy way to capture http related metrics (i.e. response time, bytes written, and http status code) from your application's http.Handlers.

Doing this requires non-trivial wrapping of the http.ResponseWriter interface, which is also exposed for users interested in a more low-level API.

func Unwrap

func Unwrap(w http.ResponseWriter) http.ResponseWriter

Unwrap returns the underlying http.ResponseWriter from within zero or more layers of httpsnoop wrappers.

func Wrap

func Wrap(w http.ResponseWriter, hooks Hooks) http.ResponseWriter

Wrap returns a wrapped version of w that provides the exact same interface as w. Specifically if w implements any combination of:

- http.Flusher - http.CloseNotifier - http.Hijacker - io.ReaderFrom - http.Pusher

The wrapped version will implement the exact same combination. If no hooks are set, the wrapped version also behaves exactly as w. Hooks targeting methods not supported by w are ignored. Any other hooks will intercept the method they target and may modify the call's arguments and/or return values. The CaptureMetrics implementation serves as a working example for how the hooks can be used.

type CloseNotifyFunc

CloseNotifyFunc is part of the http.CloseNotifier interface.

type CloseNotifyFunc func() <-chan bool

type FlushFunc

FlushFunc is part of the http.Flusher interface.

type FlushFunc func()

type HeaderFunc

HeaderFunc is part of the http.ResponseWriter interface.

type HeaderFunc func() http.Header

type HijackFunc

HijackFunc is part of the http.Hijacker interface.

type HijackFunc func() (net.Conn, *bufio.ReadWriter, error)

type Hooks

Hooks defines a set of method interceptors for methods included in http.ResponseWriter as well as some others. You can think of them as middleware for the function calls they target. See Wrap for more details.

type Hooks struct {
    Header      func(HeaderFunc) HeaderFunc
    WriteHeader func(WriteHeaderFunc) WriteHeaderFunc
    Write       func(WriteFunc) WriteFunc
    Flush       func(FlushFunc) FlushFunc
    CloseNotify func(CloseNotifyFunc) CloseNotifyFunc
    Hijack      func(HijackFunc) HijackFunc
    ReadFrom    func(ReadFromFunc) ReadFromFunc
    Push        func(PushFunc) PushFunc
}

type Metrics

Metrics holds metrics captured from CaptureMetrics.

type Metrics struct {
    // Code is the first http response code passed to the WriteHeader func of
    // the ResponseWriter. If no such call is made, a default code of 200 is
    // assumed instead.
    Code int
    // Duration is the time it took to execute the handler.
    Duration time.Duration
    // Written is the number of bytes successfully written by the Write or
    // ReadFrom function of the ResponseWriter. ResponseWriters may also write
    // data to their underlaying connection directly (e.g. headers), but those
    // are not tracked. Therefor the number of Written bytes will usually match
    // the size of the response body.
    Written int64
}

func CaptureMetrics

func CaptureMetrics(hnd http.Handler, w http.ResponseWriter, r *http.Request) Metrics

CaptureMetrics wraps the given hnd, executes it with the given w and r, and returns the metrics it captured from it.

func CaptureMetricsFn

func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics

CaptureMetricsFn wraps w and calls fn with the wrapped w and returns the resulting metrics. This is very similar to CaptureMetrics (which is just sugar on top of this func), but is a more usable interface if your application doesn't use the Go http.Handler interface.

func (*Metrics) CaptureMetrics

func (m *Metrics) CaptureMetrics(w http.ResponseWriter, fn func(http.ResponseWriter))

CaptureMetrics wraps w and calls fn with the wrapped w and updates Metrics m with the resulting metrics. This is similar to CaptureMetricsFn, but allows one to customize starting Metrics object.

type PushFunc

PushFunc is part of the http.Pusher interface.

type PushFunc func(target string, opts *http.PushOptions) error

type ReadFromFunc

ReadFromFunc is part of the io.ReaderFrom interface.

type ReadFromFunc func(src io.Reader) (int64, error)

type Unwrapper

type Unwrapper interface {
    Unwrap() http.ResponseWriter
}

type WriteFunc

WriteFunc is part of the http.ResponseWriter interface.

type WriteFunc func(b []byte) (int, error)

type WriteHeaderFunc

WriteHeaderFunc is part of the http.ResponseWriter interface.

type WriteHeaderFunc func(code int)

Subdirectories

Name Synopsis
..
codegen