Skip to content

Commit 5944712

Browse files
committed
Adding a request-logger, mostly to debug stuff in the k8s cluster
1 parent 580d6b6 commit 5944712

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

handlers/requestlogger.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"time"
7+
8+
"github.com/go-kit/kit/log"
9+
)
10+
11+
// NewRequestLogger logs each request with start and ending times
12+
func NewRequestLogger(l log.Logger) func(h http.Handler) http.Handler {
13+
return func(h http.Handler) http.Handler {
14+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15+
start := time.Now()
16+
l.Log("request", "start", "path", r.URL.Path, "remote_addr", r.RemoteAddr)
17+
defer l.Log("request", "end", "duration", time.Since(start))
18+
19+
h.ServeHTTP(w, r)
20+
})
21+
}
22+
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func main() {
4949
logger := log.With(
5050
log.NewLogfmtLogger(os.Stderr),
5151
"ts", log.DefaultTimestampUTC,
52-
"caller", log.DefaultCaller,
5352
)
5453

5554
logger.Log(
@@ -137,9 +136,10 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han
137136
decorators,
138137

139138
// Checking on the route "health", doesn't support path-segment-stripping!
140-
handlers.NewHTTPStatusPaths(l, []string{"health"}, http.StatusOK),
139+
handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK),
141140

142141
handlers.NewIgnoreFaviconRequests(),
142+
handlers.NewRequestLogger(l),
143143
handlers.NewRateLimitHandler(l, b),
144144
)
145145

0 commit comments

Comments
 (0)