Add logging of response times

This makes monitoring Chronograf 👍
pull/10616/head
Tim Raymond 2017-05-03 16:43:37 -04:00
parent 1e2e3c1362
commit 43ee0e4baf
1 changed files with 10 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package server
import (
"net/http"
"time"
"github.com/influxdata/chronograf"
)
@ -9,6 +10,7 @@ import (
// Logger is middleware that logs the request
func Logger(logger chronograf.Logger, next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
logger.
WithField("component", "server").
WithField("remote_addr", r.RemoteAddr).
@ -16,6 +18,14 @@ func Logger(logger chronograf.Logger, next http.Handler) http.Handler {
WithField("url", r.URL).
Info("Request")
next.ServeHTTP(w, r)
later := time.Now()
elapsed := later.Sub(now)
logger.
WithField("component", "server").
WithField("remote_addr", r.RemoteAddr).
WithField("response_time", elapsed.String()).
Info("Success")
}
return http.HandlerFunc(fn)
}