Source File
capture_metrics.go
Belonging Package
github.com/felixge/httpsnoop
package httpsnoopimport ()// 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}// CaptureMetrics wraps the given hnd, executes it with the given w and r, and// returns the metrics it captured from it.func ( http.Handler, http.ResponseWriter, *http.Request) Metrics {return CaptureMetricsFn(, func( http.ResponseWriter) {.ServeHTTP(, )})}// 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 ( http.ResponseWriter, func(http.ResponseWriter)) Metrics {:= Metrics{Code: http.StatusOK}.CaptureMetrics(, )return}// 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.func ( *Metrics) ( http.ResponseWriter, func(http.ResponseWriter)) {var (= time.Now()bool= Hooks{WriteHeader: func( WriteHeaderFunc) WriteHeaderFunc {return func( int) {()if !( >= 100 && <= 199) && ! {.Code == true}}},Write: func( WriteFunc) WriteFunc {return func( []byte) (int, error) {, := ().Written += int64()= truereturn ,}},WriteString: func( WriteStringFunc) WriteStringFunc {return func( string) (int, error) {, := ().Written += int64()= truereturn ,}},ReadFrom: func( ReadFromFunc) ReadFromFunc {return func( io.Reader) (int64, error) {, := ()= true.Written +=return ,}},})// defer to ensure duration is updated even if the handler panicsdefer func() {.Duration += time.Since()}()(Wrap(, ))}// deadliner defines two methods introduced in go 1.20. The standard library// seems not to provide an interface we can import, hence its definition here.type deadliner interface {SetReadDeadline(deadline time.Time) errorSetWriteDeadline(deadline time.Time) error}// fullDuplexEnabler defines a method introduced in go 1.21. The standard// library seems not to provide an interface we can import, hence its definition// here.type fullDuplexEnabler interface {EnableFullDuplex() error}// httpFlushError defines a method introduced in go 1.20. The standard// library seems not to provide an interface we can import, hence its definition// here.// See https://github.com/golang/go/blob/go1.20/src/net/http/responsecontroller.go#L50type httpFlushError interface {FlushError() error}
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64)