Skip to content

Commit

Permalink
Return 503 on healthz with error
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Jul 28, 2020
1 parent 3a609f4 commit a604ebc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions shared/prometheus/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *Service) healthzHandler(w http.ResponseWriter, r *http.Request) {
Status bool `json:"status"`
Err string `json:"error"`
}
var hasError bool
var statuses []serviceStatus
for k, v := range s.svcRegistry.Statuses() {
s := serviceStatus{
Expand All @@ -70,11 +71,18 @@ func (s *Service) healthzHandler(w http.ResponseWriter, r *http.Request) {
if v != nil {
s.Status = false
s.Err = v.Error()
if s.Err != "" {
hasError = true
}
}
statuses = append(statuses, s)
}
response.Data = statuses

if hasError {
w.WriteHeader(http.StatusServiceUnavailable)
}

// Handle plain text content.
if contentType := negotiateContentType(r); contentType == contentTypePlainText {
var buf bytes.Buffer
Expand Down
3 changes: 3 additions & 0 deletions shared/prometheus/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@ func TestContentNegotiation(t *testing.T) {
if !strings.Contains(body, expectedJSON) {
t.Errorf("Unexpected data, want: %q got %q", expectedJSON, body)
}
if rr.Code < 500 {
t.Errorf("Expected a server error response code, but got %d", rr.Code)
}
})
}

0 comments on commit a604ebc

Please sign in to comment.