From 594471263052498bb23a29b50b01fc3b77070ebc Mon Sep 17 00:00:00 2001 From: Mark van der Velden Date: Thu, 27 Apr 2017 17:24:33 +0200 Subject: [PATCH] Adding a request-logger, mostly to debug stuff in the k8s cluster --- handlers/requestlogger.go | 22 ++++++++++++++++++++++ main.go | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 handlers/requestlogger.go diff --git a/handlers/requestlogger.go b/handlers/requestlogger.go new file mode 100644 index 0000000..273fa0b --- /dev/null +++ b/handlers/requestlogger.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "net/http" + + "time" + + "github.com/go-kit/kit/log" +) + +// NewRequestLogger logs each request with start and ending times +func NewRequestLogger(l log.Logger) func(h http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + l.Log("request", "start", "path", r.URL.Path, "remote_addr", r.RemoteAddr) + defer l.Log("request", "end", "duration", time.Since(start)) + + h.ServeHTTP(w, r) + }) + } +} diff --git a/main.go b/main.go index 53269dd..9b2e2d5 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,6 @@ func main() { logger := log.With( log.NewLogfmtLogger(os.Stderr), "ts", log.DefaultTimestampUTC, - "caller", log.DefaultCaller, ) logger.Log( @@ -137,9 +136,10 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han decorators, // Checking on the route "health", doesn't support path-segment-stripping! - handlers.NewHTTPStatusPaths(l, []string{"health"}, http.StatusOK), + handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK), handlers.NewIgnoreFaviconRequests(), + handlers.NewRequestLogger(l), handlers.NewRateLimitHandler(l, b), )