health
API
health
packageAPI reference for the health
package.
Imports
(6)
T
type
Status
Status represents the health status of a component.
core/health/health.go:12-12
type Status int
S
struct
Report
Report contains the result of a health check.
core/health/health.go:38-42
type Report struct
Fields
| Name | Type | Description |
|---|---|---|
| Status | Status | |
| Duration | time.Duration | |
| Details | map[string]any |
Uses
I
interface
Checker
Checker performs a health check and returns a Report.
core/health/health.go:45-47
type Checker interface
Methods
S
struct
Registry
Registry manages a set of named health Checkers.
core/health/health.go:50-53
type Registry struct
Methods
Register
Method
Register adds a named checker to the registry.
Parameters
name
string
c
Checker
func (*Registry) Register(name string, c Checker)
{
if strings.TrimSpace(name) == "" {
panic("health: checker name cannot be empty")
}
if isNilChecker(c) {
panic("health: checker cannot be nil")
}
r.mu.Lock()
defer r.mu.Unlock()
if _, exists := r.checks[name]; exists {
panic("health: checker is already registered: " + name)
}
r.checks[name] = c
}
CheckAll
Method
Parameters
ctx
context.Context
Returns
map[string]Report
func (*Registry) CheckAll(ctx context.Context) map[string]Report
{
r.mu.RLock()
names := make([]string, 0, len(r.checks))
for n := range r.checks {
names = append(names, n)
}
r.mu.RUnlock()
results := make(map[string]Report, len(names))
for _, name := range names {
r.mu.RLock()
checker := r.checks[name]
r.mu.RUnlock()
start := time.Now()
report := checker.Check(ctx)
report.Duration = time.Since(start)
results[name] = report
}
return results
}
Fields
| Name | Type | Description |
|---|---|---|
| mu | sync.RWMutex | |
| checks | map[string]Checker |
F
function
NewRegistry
NewRegistry creates a new health check Registry.
Returns
core/health/health.go:56-58
func NewRegistry() *Registry
{
return &Registry{checks: make(map[string]Checker)}
}
F
function
isNilChecker
Parameters
checker
Returns
bool
core/health/health.go:76-87
func isNilChecker(checker Checker) bool
{
if checker == nil {
return true
}
value := reflect.ValueOf(checker)
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
return value.IsNil()
default:
return false
}
}
Uses
T
type
checkFunc
core/health/health_test.go:8-8
type checkFunc func(context.Context) Report
F
function
TestStatusString
Parameters
t
core/health/health_test.go:14-26
func TestStatusString(t *testing.T)
{
tests := map[Status]string{
StatusHealthy: "healthy",
StatusDegraded: "degraded",
StatusUnhealthy: "unhealthy",
Status(99): "unknown",
}
for status, want := range tests {
if got := status.String(); got != want {
t.Fatalf("Status.String() = %q, want %q", got, want)
}
}
}
F
function
TestRegistryCheckAll
Parameters
t
core/health/health_test.go:28-48
func TestRegistryCheckAll(t *testing.T)
{
reg := NewRegistry()
reg.Register("db", checkFunc(func(ctx context.Context) Report {
return Report{Status: StatusHealthy, Details: map[string]any{"ok": true}}
}))
results := reg.CheckAll(context.Background())
report, ok := results["db"]
if !ok {
t.Fatal("CheckAll() missing db report")
}
if report.Status != StatusHealthy {
t.Fatalf("Status = %v, want healthy", report.Status)
}
if report.Duration == 0 {
t.Fatal("Duration was not set")
}
if report.Details["ok"] != true {
t.Fatalf("Details[ok] = %v, want true", report.Details["ok"])
}
}
F
function
TestRegistryRejectsInvalidRegistrations
Parameters
t
core/health/health_test.go:50-87
func TestRegistryRejectsInvalidRegistrations(t *testing.T)
{
checker := checkFunc(func(context.Context) Report {
return Report{Status: StatusHealthy}
})
var typedNil *nilChecker
tests := []struct {
name string
checker Checker
setup func(*Registry)
}{
{name: "", checker: checker},
{name: "nil", checker: nil},
{name: "typed-nil", checker: typedNil},
{
name: "duplicate",
checker: checker,
setup: func(registry *Registry) {
registry.Register("duplicate", checker)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registry := NewRegistry()
if test.setup != nil {
test.setup(registry)
}
defer func() {
if recover() == nil {
t.Fatal("Register() accepted an invalid checker")
}
}()
registry.Register(test.name, test.checker)
})
}
}
S
struct
nilChecker
core/health/health_test.go:89-89
type nilChecker struct
Methods
Check
Method
Parameters
Returns
func (*nilChecker) Check(context.Context) Report
{
return Report{}
}