Skip to content

Commit

Permalink
Fix health check for older Prometheus versions
Browse files Browse the repository at this point in the history
 - The output string changed in newer versions, so the fix is
   to check only for the substring
  • Loading branch information
martialblog committed Aug 25, 2023
1 parent be72a5d commit 94aefe6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions cmd/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func TestHealthCmd(t *testing.T) {
args: []string{"run", "../main.go", "health"},
expected: "[OK] - Prometheus Server is Healthy. | statuscode=200\n",
},
{
name: "health-ok-older-versions",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`Prometheus is Healthy.`))
})),
args: []string{"run", "../main.go", "health"},
expected: "[OK] - Prometheus is Healthy. | statuscode=200\n",
},
{
name: "ready-ok",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
6 changes: 3 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (c *Client) GetStatus(ctx context.Context, endpoint string) (returncode int
respBody := strings.TrimSpace(string(b))

// What we expect from the Prometheus Server
statusOk := "Prometheus Server is Healthy."
statusOk := "is Healthy."
if endpoint == "ready" {
statusOk = "Prometheus Server is Ready."
statusOk = "is Ready."
}

if resp.StatusCode == http.StatusOK && respBody == statusOk {
if resp.StatusCode == http.StatusOK && strings.Contains(respBody, statusOk) {
return check.OK, resp.StatusCode, respBody, err
}

Expand Down

0 comments on commit 94aefe6

Please sign in to comment.