aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/felixge/httpsnoop/capture_metrics.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/felixge/httpsnoop/capture_metrics.go')
-rw-r--r--vendor/github.com/felixge/httpsnoop/capture_metrics.go24
1 files changed, 13 insertions, 11 deletions
diff --git a/vendor/github.com/felixge/httpsnoop/capture_metrics.go b/vendor/github.com/felixge/httpsnoop/capture_metrics.go
index 4c45b1a8c..b77cc7c00 100644
--- a/vendor/github.com/felixge/httpsnoop/capture_metrics.go
+++ b/vendor/github.com/felixge/httpsnoop/capture_metrics.go
@@ -3,7 +3,6 @@ package httpsnoop
import (
"io"
"net/http"
- "sync"
"time"
)
@@ -36,17 +35,23 @@ func CaptureMetrics(hnd http.Handler, w http.ResponseWriter, r *http.Request) Me
// sugar on top of this func), but is a more usable interface if your
// application doesn't use the Go http.Handler interface.
func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics {
+ m := Metrics{Code: http.StatusOK}
+ m.CaptureMetrics(w, fn)
+ return m
+}
+
+// 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 (m *Metrics) CaptureMetrics(w http.ResponseWriter, fn func(http.ResponseWriter)) {
var (
start = time.Now()
- m = Metrics{Code: http.StatusOK}
headerWritten bool
- lock sync.Mutex
hooks = Hooks{
WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
return func(code int) {
next(code)
- lock.Lock()
- defer lock.Unlock()
+
if !headerWritten {
m.Code = code
headerWritten = true
@@ -57,8 +62,7 @@ func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metri
Write: func(next WriteFunc) WriteFunc {
return func(p []byte) (int, error) {
n, err := next(p)
- lock.Lock()
- defer lock.Unlock()
+
m.Written += int64(n)
headerWritten = true
return n, err
@@ -68,8 +72,7 @@ func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metri
ReadFrom: func(next ReadFromFunc) ReadFromFunc {
return func(src io.Reader) (int64, error) {
n, err := next(src)
- lock.Lock()
- defer lock.Unlock()
+
headerWritten = true
m.Written += n
return n, err
@@ -79,6 +82,5 @@ func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metri
)
fn(Wrap(w, hooks))
- m.Duration = time.Since(start)
- return m
+ m.Duration += time.Since(start)
}