Skip to content

Commit

Permalink
Allow specifying initial metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitar authored and felixge committed Apr 24, 2022
1 parent 42c30f9 commit 67cc0c4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions capture_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ 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.
func (m *Metrics) CaptureMetrics(w http.ResponseWriter, fn func(http.ResponseWriter)) {
var (
start = time.Now()
m = Metrics{Code: http.StatusOK}
headerWritten bool
hooks = Hooks{
WriteHeader: func(next WriteHeaderFunc) WriteHeaderFunc {
Expand Down Expand Up @@ -74,6 +81,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)
}

0 comments on commit 67cc0c4

Please sign in to comment.