|
@@ -35,9 +35,17 @@ 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
|
|
// sugar on top of this func), but is a more usable interface if your
|
|
// application doesn't use the Go http.Handler interface.
|
|
// application doesn't use the Go http.Handler interface.
|
|
func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics {
|
|
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 (
|
|
var (
|
|
start = time.Now()
|
|
start = time.Now()
|
|
- m = Metrics{Code: http.StatusOK}
|
|
|
|
headerWritten bool
|
|
headerWritten bool
|
|
hooks = Hooks{
|
|
hooks = Hooks{
|
|
WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
|
|
WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
|
|
@@ -74,6 +82,5 @@ func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metri
|
|
)
|
|
)
|
|
|
|
|
|
fn(Wrap(w, hooks))
|
|
fn(Wrap(w, hooks))
|
|
- m.Duration = time.Since(start)
|
|
|
|
- return m
|
|
|
|
|
|
+ m.Duration += time.Since(start)
|
|
}
|
|
}
|