From 95dce5cdfce74a16efcad9b17ff28417580443e9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 23 Apr 2015 13:35:56 -0600 Subject: [PATCH] Latency now available with recorder and replacer --- middleware/recorder.go | 7 ++++++- middleware/replacer.go | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/middleware/recorder.go b/middleware/recorder.go index ef5b69bf..4884026c 100644 --- a/middleware/recorder.go +++ b/middleware/recorder.go @@ -1,6 +1,9 @@ package middleware -import "net/http" +import ( + "net/http" + "time" +) // responseRecorder is a type of ResponseWriter that captures // the status code written to it and also the size of the body @@ -12,6 +15,7 @@ type responseRecorder struct { http.ResponseWriter status int size int + start time.Time } // NewResponseRecorder makes and returns a new responseRecorder, @@ -24,6 +28,7 @@ func NewResponseRecorder(w http.ResponseWriter) *responseRecorder { return &responseRecorder{ ResponseWriter: w, status: http.StatusOK, + start: time.Now(), } } diff --git a/middleware/replacer.go b/middleware/replacer.go index b6194ad7..6e47d5f7 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -50,8 +50,9 @@ func NewReplacer(r *http.Request, rr *responseRecorder) replacer { "{when}": func() string { return time.Now().Format(timeFormat) }(), - "{status}": strconv.Itoa(rr.status), - "{size}": strconv.Itoa(rr.size), + "{status}": strconv.Itoa(rr.status), + "{size}": strconv.Itoa(rr.size), + "{latency}": time.Since(rr.start).String(), } // Header placeholders