-
Notifications
You must be signed in to change notification settings - Fork 61
/
health_check.go
73 lines (59 loc) · 2.42 KB
/
health_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package grpcutil
import (
"context"
"github.com/gogo/status"
"go.uber.org/atomic"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/grafana/dskit/services"
)
// Check is a function that determines if this gRPC application is healthy.
type Check func(ctx context.Context) bool
// WithManager returns a new Check that tests if the managed services are healthy.
func WithManager(manager *services.Manager) Check {
return func(context.Context) bool {
states := manager.ServicesByState()
// Given this is a health check endpoint for the whole instance, we should consider
// it healthy after all services have been started (running) and until all
// services are terminated. Some services, like ingesters, are still
// fully functioning while stopping.
if len(states[services.New]) > 0 || len(states[services.Starting]) > 0 || len(states[services.Failed]) > 0 {
return false
}
return len(states[services.Running]) > 0 || len(states[services.Stopping]) > 0
}
}
// WithShutdownRequested returns a new Check that returns false when shutting down.
func WithShutdownRequested(requested *atomic.Bool) Check {
return func(context.Context) bool {
return !requested.Load()
}
}
// HealthCheck fulfills the grpc_health_v1.HealthServer interface by ensuring
// each of the provided Checks indicates the application is healthy.
type HealthCheck struct {
checks []Check
}
// NewHealthCheck returns a new HealthCheck for the provided service manager.
func NewHealthCheck(sm *services.Manager) *HealthCheck {
return NewHealthCheckFrom(WithManager(sm))
}
// NewHealthCheckFrom returns a new HealthCheck that uses each of the provided Checks.
func NewHealthCheckFrom(checks ...Check) *HealthCheck {
return &HealthCheck{
checks: checks,
}
}
// Check implements the grpc healthcheck.
func (h *HealthCheck) Check(ctx context.Context, _ *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
for _, check := range h.checks {
if !check(ctx) {
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_NOT_SERVING}, nil
}
}
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
}
// Watch implements the grpc healthcheck.
func (h *HealthCheck) Watch(_ *grpc_health_v1.HealthCheckRequest, _ grpc_health_v1.Health_WatchServer) error {
return status.Error(codes.Unimplemented, "Watching is not supported")
}