Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partitions in health checks #129

Merged
merged 8 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions rest/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type HealthCheck interface {
// ServiceName returns the name of the service that is health checked.
ServiceName() string
// Check is a function returning a service status and an error.
Check(ctx context.Context) (HealthStatus, error)
Check(ctx context.Context) (HealthResult, error)
}

// HealthResponse is returned by the API when executing a health check.
Expand All @@ -41,17 +41,12 @@ type HealthResponse struct {
Status HealthStatus `json:"status"`
// Message gives additional information on the overall health state.
Message string `json:"message"`
// Services is map of services by name with their individual health results.
Services map[string]HealthResult `json:"services"`
// provides further information on the result e.g. services or partitions
Information map[string]HealthResult `json:"information"`
Gerrit91 marked this conversation as resolved.
Show resolved Hide resolved
}

// HealthResult holds the health state of a service.
type HealthResult struct {
// Status indicates the health of the service.
Status HealthStatus `json:"status"`
// Message gives additional information on the health of a service.
Message string `json:"message"`
}
type HealthResult HealthResponse

type healthResource struct {
log *zap.SugaredLogger
Expand Down Expand Up @@ -111,9 +106,9 @@ func (h *healthResource) check(request *restful.Request, response *restful.Respo
var (
service = request.QueryParameter("service")
result = HealthResponse{
Status: HealthStatusHealthy,
Message: "",
Services: map[string]HealthResult{},
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
}

resultChan = make(chan chanResult)
Expand All @@ -139,16 +134,17 @@ func (h *healthResource) check(request *restful.Request, response *restful.Respo
result := chanResult{
name: name,
HealthResult: HealthResult{
Status: HealthStatusHealthy,
Message: "",
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
},
}
defer func() {
resultChan <- result
}()

var err error
result.Status, err = healthCheck.Check(ctx)
result.HealthResult, err = healthCheck.Check(ctx)
if err != nil {
result.Message = err.Error()
h.log.Errorw("unhealthy service", "name", name, "status", result.Status, "error", err)
Expand All @@ -162,8 +158,7 @@ func (h *healthResource) check(request *restful.Request, response *restful.Respo
go func() {
for r := range resultChan {
r := r
result.Services[r.name] = r.HealthResult

result.Information[r.name] = r.HealthResult
}
finished <- true
}()
Expand All @@ -179,23 +174,26 @@ func (h *healthResource) check(request *restful.Request, response *restful.Respo

<-finished

result.Status = DeriveOverallHealthStatus(result.Services)
result.Status = DeriveOverallHealthStatus(result.Information)

err := response.WriteHeaderAndEntity(rc, result)
if err != nil {
h.log.Error("error writing response", zap.Error(err))
}
}

func DeriveOverallHealthStatus(services map[string]HealthResult) HealthStatus {
func DeriveOverallHealthStatus(information map[string]HealthResult) HealthStatus {
var (
result = HealthStatusHealthy
degraded int
unhealthy int
)

for _, service := range services {
switch service.Status {
for _, i := range information {
if i.Status == "" {
i.Status = DeriveOverallHealthStatus(i.Information)
}
switch i.Status {
case HealthStatusHealthy:
case HealthStatusDegraded:
degraded++
Expand All @@ -206,14 +204,14 @@ func DeriveOverallHealthStatus(services map[string]HealthResult) HealthStatus {
}
}

if len(services) > 0 {
if len(information) > 0 {
if degraded > 0 {
result = HealthStatusDegraded
}
if unhealthy > 0 {
result = HealthStatusPartiallyUnhealthy
}
if unhealthy == len(services) {
if unhealthy == len(information) {
result = HealthStatusUnhealthy
}
}
Expand Down
59 changes: 50 additions & 9 deletions rest/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ func (e *succeedingCheck) ServiceName() string {
return "success"
}

func (e *succeedingCheck) Check(ctx context.Context) (HealthStatus, error) {
return HealthStatusHealthy, nil
func (e *succeedingCheck) Check(ctx context.Context) (HealthResult, error) {
return HealthResult{
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{
"successPartition": {
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
}, nil
}

type failingCheck struct{}
Expand All @@ -29,8 +39,18 @@ func (e *failingCheck) ServiceName() string {
return "fail"
}

func (e *failingCheck) Check(ctx context.Context) (HealthStatus, error) {
return HealthStatusUnhealthy, fmt.Errorf("facing an issue")
func (e *failingCheck) Check(ctx context.Context) (HealthResult, error) {
return HealthResult{
Status: HealthStatusUnhealthy,
Message: "",
Information: map[string]HealthResult{
"failPartition": {
Status: HealthStatusUnhealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
}, fmt.Errorf("facing an issue")
}

func TestNewHealth(t *testing.T) {
Expand All @@ -56,9 +76,9 @@ func TestNewHealth(t *testing.T) {
h: nil,
},
want: &HealthResponse{
Status: HealthStatusHealthy,
Message: "",
Services: map[string]HealthResult{},
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
{
Expand All @@ -71,14 +91,28 @@ func TestNewHealth(t *testing.T) {
want: &HealthResponse{
Status: HealthStatusPartiallyUnhealthy,
Message: "facing an issue",
Services: map[string]HealthResult{
Information: map[string]HealthResult{
"success": {
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{
"successPartition": {
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
},
"fail": {
Status: HealthStatusUnhealthy,
Message: "facing an issue",
Information: map[string]HealthResult{
"failPartition": {
Status: HealthStatusUnhealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
},
},
},
Expand All @@ -94,10 +128,17 @@ func TestNewHealth(t *testing.T) {
want: &HealthResponse{
Status: HealthStatusHealthy,
Message: "",
Services: map[string]HealthResult{
Information: map[string]HealthResult{
"success": {
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{
"successPartition": {
Status: HealthStatusHealthy,
Message: "",
Information: map[string]HealthResult{},
},
},
},
},
},
Expand Down
Loading