Skip to content

Commit

Permalink
fix: remove /health from logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed May 16, 2024
1 parent de45b3f commit f26ab6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/observability/request-logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ func AddRequestID(globalConfig *conf.GlobalConfiguration) func(next http.Handler
}

func NewStructuredLogger(logger *logrus.Logger, config *conf.GlobalConfiguration) func(next http.Handler) http.Handler {
return chimiddleware.RequestLogger(&structuredLogger{logger, config})
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health" {
next.ServeHTTP(w, r)
} else {
chimiddleware.RequestLogger(&structuredLogger{logger, config})(next).ServeHTTP(w, r)
}
})
}
}

type structuredLogger struct {
Expand Down
23 changes: 23 additions & 0 deletions internal/observability/request-logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ func TestLogger(t *testing.T) {
require.Equal(t, "test-request-id", logs["request_id"])
require.NotNil(t, logs["time"])
}

func TestExcludeHealthFromLogs(t *testing.T) {
var logBuffer bytes.Buffer
config, err := conf.LoadGlobal(apiTestConfig)
require.NoError(t, err)

config.Logging.Level = "info"
require.NoError(t, ConfigureLogging(&config.Logging))

// logrus should write to the buffer so we can check if the logs are output correctly
logrus.SetOutput(&logBuffer)

logHandler := NewStructuredLogger(logrus.StandardLogger(), config)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
w := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "http://example.com/health", nil)
require.NoError(t, err)
logHandler.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)

require.Empty(t, logBuffer)
}

0 comments on commit f26ab6e

Please sign in to comment.