Skip to content

Commit

Permalink
refactor: exclude healthcheck from access logs (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshum authored Mar 7, 2024
1 parent bb1ed45 commit 2b011f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
29 changes: 14 additions & 15 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,19 @@ func (s *Server) panicHandler(next http.Handler) http.Handler {
})
}

func pathHandler(
method string, handleFuncs map[string]http.HandlerFunc,
) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
next.ServeHTTP(w, r)
return
}
if handle, ok := handleFuncs[r.URL.Path]; ok {
handle(w, r)
return
}
func noopHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
next.ServeHTTP(w, r)
return
})
}
}
if r.URL.Path == "/healthcheck" || r.URL.Path == "/favicon.ico" {
handleOk(w, r)
return
}
next.ServeHTTP(w, r)
return
})
}

func stripQueryStringHandler(next http.Handler) http.Handler {
Expand Down Expand Up @@ -100,6 +96,9 @@ func (s *Server) accessLogHandler(next http.Handler) http.Handler {
Status: 200,
}
next.ServeHTTP(wr, r)
if r.URL.Path == "/healthcheck" || r.URL.Path == "/favicon.ico" {
return // skip healthcheck routes
}
s.Logger.Info("access",
zap.Int("status", wr.Status),
zap.String("method", r.Method),
Expand Down
7 changes: 2 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ func New(app Service, options ...Option) *Server {
// Handler: application
s.Handler = s.App

// Handler: utility routes
s.Handler = pathHandler(http.MethodGet, map[string]http.HandlerFunc{
"/favicon.ico": handleOk,
"/healthcheck": handleOk,
})(s.Handler)
// handle no-op routes /healthcheck, /favicon.ico
s.Handler = noopHandler(s.Handler)

for _, option := range options {
option(s)
Expand Down

0 comments on commit 2b011f4

Please sign in to comment.