-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from appuio/add-healthz-endpoint
Add `/healthz` endpoint that checks Alertmanager connection
- Loading branch information
Showing
5 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-openapi/strfmt" | ||
"github.com/prometheus/alertmanager/api/v2/client/general" | ||
"github.com/prometheus/alertmanager/api/v2/models" | ||
) | ||
|
||
// HealthCheck is a health check handler for the Alertmanager API. | ||
type HealthCheck struct { | ||
GeneralService general.ClientService | ||
} | ||
|
||
// HandleHealthz handles a health check request. | ||
// It returns a JSON response with the status of the Alertmanager API or an error if the client returns an error or if receiving a nil response. | ||
func (h HealthCheck) HandleHealthz(res http.ResponseWriter, req *http.Request) { | ||
ams, err := h.GeneralService.GetStatus(general.NewGetStatusParamsWithContext(req.Context())) | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if ams == nil || ams.Payload == nil { | ||
http.Error(res, "Nil response from Alertmanager", http.StatusInternalServerError) | ||
return | ||
} | ||
if err := json.NewEncoder(res).Encode(response{ | ||
Status: "connected", | ||
AlertmanagerCluster: ams.Payload.Cluster, | ||
AlertmanagerVersion: ams.Payload.VersionInfo, | ||
AlertmanagerUptime: ams.Payload.Uptime, | ||
}); err != nil { | ||
http.Error(res, "Encoding error: "+err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
type response struct { | ||
Status string `json:"status"` | ||
AlertmanagerCluster *models.ClusterStatus `json:"alertmanager_cluster"` | ||
AlertmanagerVersion *models.VersionInfo `json:"alertmanager_version"` | ||
AlertmanagerUptime *strfmt.DateTime `json:"alertmanager_uptime"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package healthcheck_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/appuio/alerts_exporter/internal/healthcheck" | ||
"github.com/go-openapi/runtime" | ||
"github.com/prometheus/alertmanager/api/v2/client/general" | ||
"github.com/prometheus/alertmanager/api/v2/models" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOk(t *testing.T) { | ||
t.Parallel() | ||
|
||
hc := &healthcheck.HealthCheck{ | ||
GeneralService: &mockClientService{ | ||
OkResponse: &general.GetStatusOK{ | ||
Payload: &models.AlertmanagerStatus{ | ||
VersionInfo: &models.VersionInfo{ | ||
Version: ptr("v0.22.2"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
req := httptest.NewRecorder() | ||
hc.HandleHealthz(req, httptest.NewRequest("GET", "/healthz", nil)) | ||
res := req.Result() | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
require.Contains(t, req.Body.String(), `"status":"connected"`) | ||
require.Contains(t, req.Body.String(), `v0.22.2`) | ||
} | ||
|
||
func TestErrResponse(t *testing.T) { | ||
t.Parallel() | ||
|
||
hc := &healthcheck.HealthCheck{ | ||
GeneralService: &mockClientService{ | ||
Err: errors.New("some error"), | ||
}, | ||
} | ||
|
||
req := httptest.NewRecorder() | ||
hc.HandleHealthz(req, httptest.NewRequest("GET", "/healthz", nil)) | ||
res := req.Result() | ||
require.Equal(t, http.StatusInternalServerError, res.StatusCode) | ||
require.Contains(t, req.Body.String(), "some error") | ||
} | ||
|
||
func TestNilResponse(t *testing.T) { | ||
t.Parallel() | ||
|
||
hc := &healthcheck.HealthCheck{ | ||
GeneralService: &mockClientService{}, | ||
} | ||
|
||
req := httptest.NewRecorder() | ||
hc.HandleHealthz(req, httptest.NewRequest("GET", "/healthz", nil)) | ||
res := req.Result() | ||
require.Equal(t, http.StatusInternalServerError, res.StatusCode) | ||
require.Contains(t, req.Body.String(), "Nil response") | ||
} | ||
|
||
type mockClientService struct { | ||
OkResponse *general.GetStatusOK | ||
Err error | ||
} | ||
|
||
var _ general.ClientService = (*mockClientService)(nil) | ||
|
||
func (m *mockClientService) GetStatus(*general.GetStatusParams, ...general.ClientOption) (*general.GetStatusOK, error) { | ||
if m.Err != nil { | ||
return nil, m.Err | ||
} | ||
return m.OkResponse, nil | ||
} | ||
|
||
func (m *mockClientService) SetTransport(runtime.ClientTransport) {} | ||
|
||
func ptr[T any](t T) *T { return &t } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters