Skip to content

Commit

Permalink
[FIXED] Healthz with details returns 200 OK even with errors (#6248)
Browse files Browse the repository at this point in the history
Return non-200 status codes if there are errors, also log (just the
count, otherwise the log statement could explode in size).

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
  • Loading branch information
derekcollison authored Dec 12, 2024
2 parents daf3e34 + b6adaea commit de35c8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3241,10 +3241,11 @@ func (s *Server) HandleHealthz(w http.ResponseWriter, r *http.Request) {
Details: includeDetails,
})

code := http.StatusOK
code := hs.StatusCode
if hs.Error != _EMPTY_ {
s.Warnf("Healthcheck failed: %q", hs.Error)
code = hs.StatusCode
} else if len(hs.Errors) != 0 {
s.Warnf("Healthcheck failed: %d errors", len(hs.Errors))
}
// Remove StatusCode from JSON representation when responding via HTTP
// since this is already in the response.
Expand Down
13 changes: 11 additions & 2 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5492,7 +5492,7 @@ func TestHealthzStatusUnavailable(t *testing.T) {
}{
{
"healthz",
fmt.Sprintf("http://%s/healthz", s.MonitorAddr().String()),
fmt.Sprintf("http://%s/healthz?", s.MonitorAddr().String()),
http.StatusServiceUnavailable,
"unavailable",
},
Expand All @@ -5510,7 +5510,16 @@ func TestHealthzStatusUnavailable(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
expectHealthStatus(t, test.url, test.statusCode, test.wantStatus)
t.Run("no-details", func(t *testing.T) {
expectHealthStatus(t, test.url, test.statusCode, test.wantStatus)
})
t.Run("with-details", func(t *testing.T) {
detailsUrl := fmt.Sprintf("%s&details=true", test.url)
if test.wantStatus != "ok" {
test.wantStatus = "error"
}
expectHealthStatus(t, detailsUrl, test.statusCode, test.wantStatus)
})
})
}
}
Expand Down

0 comments on commit de35c8b

Please sign in to comment.