Skip to content

Commit

Permalink
Merge pull request #246 from pace/log-failed-healthchecks
Browse files Browse the repository at this point in the history
Log failed health checks
  • Loading branch information
threez authored Jan 4, 2021
2 parents 2dc7744 + 44be3be commit 91e17a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions maintenance/health/servicehealthcheck/health_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package servicehealthcheck

import (
"fmt"
"net/http"

"github.com/pace/bricks/maintenance/log"
Expand All @@ -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))
}
14 changes: 7 additions & 7 deletions maintenance/health/servicehealthcheck/healthchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 91e17a6

Please sign in to comment.