9// Metrics holds metrics captured from CaptureMetrics.
11 // Code is the first http response code passed to the WriteHeader func of
12 // the ResponseWriter. If no such call is made, a default code of 200 is
15 // Duration is the time it took to execute the handler.
16 Duration time.Duration
17 // Written is the number of bytes successfully written by the Write or
18 // ReadFrom function of the ResponseWriter. ResponseWriters may also write
19 // data to their underlaying connection directly (e.g. headers), but those
20 // are not tracked. Therefor the number of Written bytes will usually match
21 // the size of the response body.
25// CaptureMetrics wraps the given hnd, executes it with the given w and r, and
26// returns the metrics it captured from it.
27func CaptureMetrics(hnd http.Handler, w http.ResponseWriter, r *http.Request) Metrics {
28 return CaptureMetricsFn(w, func(ww http.ResponseWriter) {
33// CaptureMetricsFn wraps w and calls fn with the wrapped w and returns the
34// resulting metrics. This is very similar to CaptureMetrics (which is just
35// sugar on top of this func), but is a more usable interface if your
36// application doesn't use the Go http.Handler interface.
37func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics {
38 m := Metrics{Code: http.StatusOK}
39 m.CaptureMetrics(w, fn)
43// CaptureMetrics wraps w and calls fn with the wrapped w and updates
44// Metrics m with the resulting metrics. This is similar to CaptureMetricsFn,
45// but allows one to customize starting Metrics object.
46func (m *Metrics) CaptureMetrics(w http.ResponseWriter, fn func(http.ResponseWriter)) {
51 WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
52 return func(code int) {
55 if !(code >= 100 && code <= 199) && !headerWritten {
62 Write: func(next WriteFunc) WriteFunc {
63 return func(p []byte) (int, error) {
72 ReadFrom: func(next ReadFromFunc) ReadFromFunc {
73 return func(src io.Reader) (int64, error) {
85 m.Duration += time.Since(start)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list