{"modulePath":"github.com/felixge/httpsnoop","version":"v1.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpsnoop\n\nPackage 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.\n\nDoing this requires non-trivial wrapping of the http.ResponseWriter interface, which is also exposed for users interested in a more low-level API.\n\n## Functions\n\n```go\nfunc Unwrap(w http.ResponseWriter) http.ResponseWriter\n```\nUnwrap returns the underlying http.ResponseWriter from within zero or more layers of httpsnoop wrappers.\n\n```go\nfunc Wrap(w http.ResponseWriter, hooks Hooks) http.ResponseWriter\n```\nWrap returns a wrapped version of w that provides the exact same interface as w. Specifically if w implements any combination of:\n\n\\- http.Flusher - httpFlushError - http.CloseNotifier - http.Hijacker - io.ReaderFrom - deadliner - fullDuplexEnabler - http.Pusher - io.StringWriter\n\nThe 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.\n\n## Types\n\n```go\ntype CloseNotifyFunc func() \u003c-chan bool\n```\nCloseNotifyFunc is part of the http.CloseNotifier interface.\n\n```go\ntype EnableFullDuplexFunc func() error\n```\nEnableFullDuplexFunc is part of the fullDuplexEnabler interface.\n\n```go\ntype FlushErrorFunc func() error\n```\nFlushErrorFunc is part of the httpFlushError interface.\n\n```go\ntype FlushFunc func()\n```\nFlushFunc is part of the http.Flusher interface.\n\n```go\ntype HeaderFunc func() http.Header\n```\nHeaderFunc is part of the http.ResponseWriter interface.\n\n```go\ntype HijackFunc func() (net.Conn, *bufio.ReadWriter, error)\n```\nHijackFunc is part of the http.Hijacker interface.\n\n```go\ntype Hooks struct {\n\tHeader           func(HeaderFunc) HeaderFunc\n\tWriteHeader      func(WriteHeaderFunc) WriteHeaderFunc\n\tWrite            func(WriteFunc) WriteFunc\n\tFlush            func(FlushFunc) FlushFunc\n\tFlushError       func(FlushErrorFunc) FlushErrorFunc\n\tCloseNotify      func(CloseNotifyFunc) CloseNotifyFunc\n\tHijack           func(HijackFunc) HijackFunc\n\tReadFrom         func(ReadFromFunc) ReadFromFunc\n\tSetReadDeadline  func(SetReadDeadlineFunc) SetReadDeadlineFunc\n\tSetWriteDeadline func(SetWriteDeadlineFunc) SetWriteDeadlineFunc\n\tEnableFullDuplex func(EnableFullDuplexFunc) EnableFullDuplexFunc\n\tPush             func(PushFunc) PushFunc\n\tWriteString      func(WriteStringFunc) WriteStringFunc\n}\n```\nHooks 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.\n\nFor each method, the exact matching hook takes precedence. For example, WriteString calls the WriteString hook when it is configured, even if a Write hook is also configured. If the exact hook is not configured, most methods call through to the underlying ResponseWriter directly.\n\nTwo compatibility fallbacks preserve the behavior users had before Wrap learned about newer optional interfaces:\n\n  - If the underlying ResponseWriter implements io.StringWriter and WriteString is called, but only the Write hook is configured, WriteString is routed through the Write hook with \\[]byte(s). If neither hook is configured, WriteString calls the underlying WriteString method directly.\n  - If the underlying ResponseWriter implements both http.Flusher and FlushError, and FlushError is called, but only the Flush hook is configured, FlushError is routed through the Flush hook while preserving the error returned by the underlying FlushError method. If neither hook is configured, FlushError calls the underlying FlushError method directly.\n\n```go\ntype Metrics struct {\n\t// Code is the first http response code passed to the WriteHeader func of\n\t// the ResponseWriter. If no such call is made, a default code of 200 is\n\t// assumed instead.\n\tCode int\n\t// Duration is the time it took to execute the handler.\n\tDuration time.Duration\n\t// Written is the number of bytes successfully written by the Write or\n\t// ReadFrom function of the ResponseWriter. ResponseWriters may also write\n\t// data to their underlaying connection directly (e.g. headers), but those\n\t// are not tracked. Therefor the number of Written bytes will usually match\n\t// the size of the response body.\n\tWritten int64\n}\n```\nMetrics holds metrics captured from CaptureMetrics.\n\n```go\nfunc CaptureMetrics(hnd http.Handler, w http.ResponseWriter, r *http.Request) Metrics\n```\nCaptureMetrics wraps the given hnd, executes it with the given w and r, and returns the metrics it captured from it.\n\n```go\nfunc CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics\n```\nCaptureMetricsFn 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.\n\n```go\nfunc (m *Metrics) CaptureMetrics(w http.ResponseWriter, fn func(http.ResponseWriter))\n```\nCaptureMetrics 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.\n\n```go\ntype PushFunc func(target string, opts *http.PushOptions) error\n```\nPushFunc is part of the http.Pusher interface.\n\n```go\ntype ReadFromFunc func(src io.Reader) (int64, error)\n```\nReadFromFunc is part of the io.ReaderFrom interface.\n\n```go\ntype SetReadDeadlineFunc func(deadline time.Time) error\n```\nSetReadDeadlineFunc is part of the deadliner interface.\n\n```go\ntype SetWriteDeadlineFunc func(deadline time.Time) error\n```\nSetWriteDeadlineFunc is part of the deadliner interface.\n\n```go\ntype Unwrapper interface {\n\tUnwrap() http.ResponseWriter\n}\n```\n\n```go\ntype WriteFunc func(b []byte) (int, error)\n```\nWriteFunc is part of the http.ResponseWriter interface.\n\n```go\ntype WriteHeaderFunc func(code int)\n```\nWriteHeaderFunc is part of the http.ResponseWriter interface.\n\n```go\ntype WriteStringFunc func(s string) (int, error)\n```\nWriteStringFunc is part of the io.StringWriter interface.\n\n","path":"github.com/felixge/httpsnoop","name":"httpsnoop","synopsis":"Package httpsnoop provides an easy way to capture http related metrics (i.e.","isRedistributable":true}
