Skip to content

Commit

Permalink
Merge pull request #268 from arnold-iakab/healtcheck-json-reformat
Browse files Browse the repository at this point in the history
Reformat healthcheck json output
  • Loading branch information
threez committed May 27, 2021
2 parents 9caa616 + 9542796 commit c5a4fd7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
42 changes: 14 additions & 28 deletions maintenance/health/servicehealthcheck/healthchecker_test.go
Expand Up @@ -258,28 +258,25 @@ func TestHandlerJSONHealthCheck(t *testing.T) {
req []*testHealthChecker
opt []*testHealthChecker
expCode int
expReq []jsonHealthHandler
expReq jsonHealthHandler
}{
{
title: "Test health check json all required",
req: []*testHealthChecker{warn, err, initErr},
opt: []*testHealthChecker{},
expCode: http.StatusServiceUnavailable,
expReq: []jsonHealthHandler{
jsonHealthHandler{
Name: err.name,
expReq: jsonHealthHandler{
err.name: serviceStats{
Status: Err,
Required: true,
Error: "healthCheckErr",
},
jsonHealthHandler{
Name: initErr.name,
initErr.name: serviceStats{
Status: Err,
Required: true,
Error: "initError",
},
jsonHealthHandler{
Name: warn.name,
warn.name: serviceStats{
Status: Ok,
Required: true,
Error: "",
Expand All @@ -291,21 +288,18 @@ func TestHandlerJSONHealthCheck(t *testing.T) {
req: []*testHealthChecker{warn, err},
opt: []*testHealthChecker{initErr},
expCode: http.StatusServiceUnavailable,
expReq: []jsonHealthHandler{
jsonHealthHandler{
Name: err.name,
expReq: jsonHealthHandler{
err.name: serviceStats{
Status: Err,
Required: true,
Error: "healthCheckErr",
},
jsonHealthHandler{
Name: initErr.name,
initErr.name: serviceStats{
Status: Err,
Required: false,
Error: "initError",
},
jsonHealthHandler{
Name: warn.name,
warn.name: serviceStats{
Status: Ok,
Required: true,
Error: "",
Expand All @@ -330,22 +324,14 @@ func TestHandlerJSONHealthCheck(t *testing.T) {

require.Equal(t, tc.expCode, resp.StatusCode)

var data []jsonHealthHandler
var data jsonHealthHandler
err := json.NewDecoder(resp.Body).Decode(&data)
require.NoError(t, err)

sort.Slice(data, func(i, j int) bool {
return data[i].Name < data[j].Name
})
sort.Slice(tc.expReq, func(i, j int) bool {
return tc.expReq[i].Name < tc.expReq[j].Name
})

for i := range tc.expReq {
require.Equal(t, tc.expReq[i].Name, data[i].Name)
require.Equal(t, tc.expReq[i].Status, data[i].Status)
require.Equal(t, tc.expReq[i].Required, data[i].Required)
require.Equal(t, tc.expReq[i].Error, data[i].Error)
for k, v := range tc.expReq {
require.Equal(t, v.Status, data[k].Status)
require.Equal(t, v.Required, data[k].Required)
require.Equal(t, v.Error, data[k].Error)
}
})
}
Expand Down
20 changes: 8 additions & 12 deletions maintenance/health/servicehealthcheck/json_health_handler.go
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/pace/bricks/maintenance/log"
)

type jsonHealthHandler struct {
Name string `json:"name"`
type jsonHealthHandler map[string]serviceStats

type serviceStats struct {
Status HealthState `json:"status"`
Required bool `json:"required"`
Error string `json:"error"`
Expand All @@ -23,15 +24,13 @@ func (h *jsonHealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
reqChecks := check(ctx, &requiredChecks)
optChecks := check(ctx, &optionalChecks)

checkResponse := make([]jsonHealthHandler, len(reqChecks)+len(optChecks))
index := 0
checkResponse := make(jsonHealthHandler)

var errors []string
var warnings []string
status := http.StatusOK
for name, res := range reqChecks {
scr := jsonHealthHandler{
Name: name,
scr := serviceStats{
Status: res.State,
Required: true,
Error: "",
Expand All @@ -43,13 +42,11 @@ func (h *jsonHealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if res.State == Warn {
warnings = append(warnings, fmt.Sprintf("%s: %s", name, res.Msg))
}
checkResponse[index] = scr
index++
checkResponse[name] = scr
}

for name, res := range optChecks {
scr := jsonHealthHandler{
Name: name,
scr := serviceStats{
Status: res.State,
Required: false,
Error: "",
Expand All @@ -58,8 +55,7 @@ func (h *jsonHealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
scr.Error = res.Msg
status = http.StatusServiceUnavailable
}
checkResponse[index] = scr
index++
checkResponse[name] = scr
}

if len(errors) > 0 {
Expand Down

0 comments on commit c5a4fd7

Please sign in to comment.