diff --git a/maintenance/health/servicehealthcheck/health_handler.go b/maintenance/health/servicehealthcheck/health_handler.go index 214dcaeb2..9bf255fc1 100644 --- a/maintenance/health/servicehealthcheck/health_handler.go +++ b/maintenance/health/servicehealthcheck/health_handler.go @@ -4,6 +4,7 @@ package servicehealthcheck import ( + "fmt" "net/http" "github.com/pace/bricks/maintenance/log" @@ -15,11 +16,20 @@ func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { s := log.Sink{Silent: true} ctx := log.ContextWithSink(r.Context(), &s) + var errors []string + var warnings []string for _, res := range check(ctx, &requiredChecks) { if res.State == Err { - writeResult(w, http.StatusServiceUnavailable, string(Err)) - return + errors = append(errors, res.Msg) + } else if res.State == Warn { + warnings = append(warnings, res.Msg) } } + if len(errors) > 0 { + log.Logger().Info().Strs("errors", errors).Strs("warnings", warnings).Msg("Health check failed") + msg := fmt.Sprintf("ERR: %d errors and %d warnings", len(errors), len(warnings)) + writeResult(w, http.StatusServiceUnavailable, msg) + return + } writeResult(w, http.StatusOK, string(Ok)) } diff --git a/maintenance/health/servicehealthcheck/healthchecker_test.go b/maintenance/health/servicehealthcheck/healthchecker_test.go index 114ed0f4b..a10d8fac2 100644 --- a/maintenance/health/servicehealthcheck/healthchecker_test.go +++ b/maintenance/health/servicehealthcheck/healthchecker_test.go @@ -50,19 +50,19 @@ func TestHandlerHealthCheck(t *testing.T) { title: "Test HealthCheck Error", check: &testHealthChecker{name: "TestHandlerHealthCheckErr", healthCheckErr: true}, expCode: http.StatusServiceUnavailable, - expBody: "ERR", + expBody: "ERR: 1 errors and 0 warnings", }, { title: "Test HealthCheck init Error", check: &testHealthChecker{name: "TestHandlerInitErr", initErr: true}, expCode: http.StatusServiceUnavailable, - expBody: "ERR", + expBody: "ERR: 1 errors and 0 warnings", }, { title: "Test HealthCheck init and check Error", check: &testHealthChecker{name: "TestHandlerInitErrHealthErr", initErr: true, healthCheckErr: true}, expCode: http.StatusServiceUnavailable, - expBody: "ERR", + expBody: "ERR: 1 errors and 0 warnings", }, { title: "Test HealthCheck Ok", @@ -110,7 +110,7 @@ func TestInitErrorRetry(t *testing.T) { } RegisterHealthCheck(checker.name, checker) - testRequest(t, http.StatusServiceUnavailable, "ERR") + testRequest(t, http.StatusServiceUnavailable, "ERR: 1 errors and 0 warnings") // remove initErr checker.initErr = false @@ -147,15 +147,15 @@ func TestInitErrorCaching(t *testing.T) { resetHealthChecks() RegisterHealthCheck(hc.name, hc) - testRequest(t, http.StatusServiceUnavailable, "ERR") + testRequest(t, http.StatusServiceUnavailable, "ERR: 1 errors and 0 warnings") hc.initErr = false - testRequest(t, http.StatusServiceUnavailable, "ERR") + testRequest(t, http.StatusServiceUnavailable, "ERR: 1 errors and 0 warnings") cfg.HealthCheckInitResultErrorTTL = 0 resetHealthChecks() hc.initErr = true RegisterHealthCheck(hc.name, hc) - testRequest(t, http.StatusServiceUnavailable, "ERR") + testRequest(t, http.StatusServiceUnavailable, "ERR: 1 errors and 0 warnings") hc.initErr = false testRequest(t, http.StatusOK, "OK")