diff --git a/internal/observability/request-logger.go b/internal/observability/request-logger.go index 4319e13f2..3e7a7e356 100644 --- a/internal/observability/request-logger.go +++ b/internal/observability/request-logger.go @@ -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 { diff --git a/internal/observability/request-logger_test.go b/internal/observability/request-logger_test.go index b15ec797d..7ab244c3f 100644 --- a/internal/observability/request-logger_test.go +++ b/internal/observability/request-logger_test.go @@ -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) +}