Skip to content

Commit

Permalink
refactor: exclude healthcheck from access logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cshum committed Mar 7, 2024
1 parent 57628de commit ba4d206
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func handleOk(w http.ResponseWriter, r *http.Request) {
return
}

func isNoopRequest(r *http.Request) bool {
return r.Method == http.MethodGet && (r.URL.Path == "/healthcheck" || r.URL.Path == "/favicon.ico")
}

func (s *Server) panicHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
Expand All @@ -44,11 +48,7 @@ func (s *Server) panicHandler(next http.Handler) http.Handler {

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" {
if isNoopRequest(r) {
handleOk(w, r)
return
}
Expand Down Expand Up @@ -96,8 +96,8 @@ 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
if isNoopRequest(r) {
return // skip logging no-op requests
}
s.Logger.Info("access",
zap.Int("status", wr.Status),
Expand Down

0 comments on commit ba4d206

Please sign in to comment.