# README
Health Check Module
Health check module compatible with K8s probes.
Installation
go get github.com/ankorstore/yokai/healthcheck
Documentation
This module provides a Checker, that:
- can register any CheckerProbe implementations and organise them for
startup
,liveness
and / orreadiness
checks - and execute them to get an overall CheckerResult
The checker result will be considered as success if ALL registered probes checks are successful.
Probes
This module provides a CheckerProbe
interface to implement to provide your own probes, for example:
package probes
import (
"context"
"github.com/ankorstore/yokai/healthcheck"
)
// success probe
type SuccessProbe struct{}
func NewSuccessProbe() *SuccessProbe {
return &SuccessProbe{}
}
func (p *SuccessProbe) Name() string {
return "successProbe"
}
func (p *SuccessProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
return healthcheck.NewCheckerProbeResult(true, "some success")
}
// failure probe
type FailureProbe struct{}
func NewFailureProbe() *FailureProbe {
return &FailureProbe{}
}
func (p *FailureProbe) Name() string {
return "failureProbe"
}
func (p *FailureProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
return healthcheck.NewCheckerProbeResult(false, "some failure")
}
Notes:
- to perform more complex checks, you can inject dependencies to your probes implementation (ex: database, cache, etc)
- it is recommended to design your probes with a single responsibility (ex: one for database, one for cache, etc)
Checker
You can create a Checker instance, register your CheckerProbe implementations, and launch checks:
package main
import (
"context"
"fmt"
"path/to/probes"
"github.com/ankorstore/yokai/healthcheck"
)
func main() {
ctx := context.Background()
checker, _ := healthcheck.NewDefaultCheckerFactory().Create(
healthcheck.WithProbe(probes.NewSuccessProbe()), // registers for startup, readiness and liveness
healthcheck.WithProbe(probes.NewFailureProbe(), healthcheck.Liveness), // registers for liveness only
)
// startup health check: invoke only successProbe
startupResult := checker.Check(ctx, healthcheck.Startup)
fmt.Printf("startup check success: %v", startupResult.Success) // startup check success: true
for probeName, probeResult := range startupResult.ProbesResults {
fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
// probe name: successProbe, probe success: true, probe message: some success
}
// liveness health check: invoke successProbe and failureProbe
livenessResult := checker.Check(ctx, healthcheck.Liveness)
fmt.Printf("liveness check success: %v", livenessResult.Success) // liveness check success: false
for probeName, probeResult := range livenessResult.ProbesResults {
fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
// probe name: successProbe, probe success: true, probe message: some success
// probe name: failureProbe, probe success: false, probe message: some failure
}
// readiness health check: invoke successProbe and failureProbe
readinessResult := checker.Check(ctx, healthcheck.Readiness)
fmt.Printf("readiness check success: %v", readinessResult.Success) // readiness check success: false
for probeName, probeResult := range readinessResult.ProbesResults {
fmt.Printf("probe name: %s, probe success: %v, probe message: %s", probeName, probeResult.Success, probeResult.Message)
// probe name: successProbe, probe success: true, probe message: some success
// probe name: failureProbe, probe success: false, probe message: some failure
}
}
# Functions
DefaultCheckerOptions are the default options used in the [DefaultCheckerFactory].
NewChecker returns a [Checker] instance.
NewCheckerProbeRegistration returns a [CheckerProbeRegistration], and accepts a [CheckerProbe] and an optional list of [ProbeKind].
NewCheckerProbeResult returns a [CheckerProbeResult], with a probe execution status and feedback message.
NewDefaultCheckerFactory returns a [DefaultCheckerFactory], implementing [CheckerFactory].
WithProbe is used to register a [CheckerProbe] for an optional list of [ProbeKind].
# Structs
Checker provides the possibility to register several [CheckerProbe] and execute them.
CheckerProbeRegistration represents a registration of a [CheckerProbe] in the [Checker].
CheckerProbeResult is the result of a [CheckerProbe] execution.
CheckerResult is the result of a [Checker] check.
DefaultCheckerFactory is the default [CheckerFactory] implementation.
Options are options for the [CheckerFactory] implementations.
# Interfaces
CheckerFactory is the interface for [Checker] factories.
CheckerProbe is the interface for the probes executed by the [Checker].
# Type aliases
CheckerOption are functional options for the [CheckerFactory] implementations.
ProbeKind is an enum for the supported kind of checks.