Skip to content

Commit

Permalink
Merge pull request #5 from Dynom/AddingRequestLogger
Browse files Browse the repository at this point in the history
Adding a request-logger, mostly to debug stuff in the k8s cluster
  • Loading branch information
Dynom authored Apr 27, 2017
2 parents 580d6b6 + 5944712 commit 5289a5b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions handlers/requestlogger.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func main() {
logger := log.With(
log.NewLogfmtLogger(os.Stderr),
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)

logger.Log(
Expand Down Expand Up @@ -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),
)

Expand Down

0 comments on commit 5289a5b

Please sign in to comment.