23b1b0f54e7995fad2d6bf495e485128f8ae3a32 — Martin Angers a month ago 9375342
use httpsnoop to capture logging metrics
4 files changed, 8 insertions(+), 24 deletions(-)

M README.md
M builder/middleware.go
M go.mod
M go.sum
M README.md => README.md +1 -0
@@ 60,5 60,6 @@
 * `github.com/go-sql-driver/mysql` for mysql DB support
 * `github.com/jmoiron/sqlx` for added SQL and struct support
 * `github.com/kelseyhightower/envconfig` for configuration, overridable via flags
+* `github.com/felixge/httpsnoop` for wrapping `http.ResponseWriter`
 
 [mkcert]: https://github.com/FiloSottile/mkcert

M builder/middleware.go => builder/middleware.go +4 -24
@@ 9,6 9,7 @@
 	"time"
 
 	"git.sr.ht/~mna/kick"
+	"github.com/felixge/httpsnoop"
 	"github.com/gorilla/handlers"
 	"github.com/juju/ratelimit"
 )


@@ 174,19 175,9 @@
 func logging(reqIDHeader string, logFn func(http.ResponseWriter, *http.Request, map[string]interface{})) func(http.Handler) http.Handler {
 	return func(h http.Handler) http.Handler {
 		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-			if _, ok := w.(interface {
-				Status() int
-				Size() int64
-			}); !ok {
-				arw := &responseWriter{
-					ResponseWriter: w,
-				}
-				w = arw
-			}
 			start := time.Now()
-			h.ServeHTTP(w, r)
+			metrics := httpsnoop.CaptureMetrics(h, w, r)
 			end := time.Now()
-
 			m := map[string]interface{}{
 				"start":               start,
 				"end":                 end,


@@ 200,23 191,12 @@
 				"body_bytes_received": r.ContentLength,
 				"user_agent":          r.UserAgent(),
 				"remote_addr":         r.RemoteAddr,
+				"body_bytes_sent":     metrics.Written,
+				"status":              metrics.Code,
 			}
 			if reqIDHeader != "" {
 				m["request_id"] = w.Header().Get(reqIDHeader)
 			}
-			if w, ok := w.(interface {
-				Status() int
-				Size() int64
-			}); ok {
-				m["body_bytes_sent"] = w.Size()
-				status := w.Status()
-				if status == 0 {
-					// can happen if returning without writing a body nor
-					// an explicit status.
-					status = 200
-				}
-				m["status"] = status
-			}
 			logFn(w, r, m)
 		})
 	}

M go.mod => go.mod +1 -0
@@ 4,6 4,7 @@
 
 require (
 	github.com/NYTimes/gziphandler v1.1.1
+	github.com/felixge/httpsnoop v1.0.1
 	github.com/gorilla/csrf v1.5.1
 	github.com/gorilla/handlers v1.4.1-0.20190227193432-ac6d24f88de4
 	github.com/gorilla/securecookie v1.1.1

M go.sum => go.sum +2 -0
@@ 4,6 4,8 @@
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
+github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
 github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
 github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
 github.com/gorilla/csrf v1.5.1 h1:UASc2+EB0T51tvl6/2ls2ciA8/qC7KdTO7DsOEKbttQ=