change Check to an interface and create implementing type check

This commit is contained in:
Dan Laine 2020-06-30 13:28:56 -04:00
parent 5d7e62f7c7
commit efaf2f147a
2 changed files with 35 additions and 23 deletions

View File

@ -20,36 +20,48 @@ type CheckFn func() (interface{}, error)
// Check defines a single health check that we want to monitor and consider as
// part of our wider healthiness
type Check struct {
type Check interface {
// Name is the identifier for this check and must be unique among all Checks
Name string
Name() string
// CheckFn is the function to call to perform the the health check
CheckFn CheckFn
// Execute performs the health check. It returns nil if the check passes.
// It can also return additional information to marshal and display to the caller
Execute() (interface{}, error)
// ExecutionPeriod is the duration to wait between executions of this Check
ExecutionPeriod time.Duration
ExecutionPeriod() time.Duration
// InitialDelay is the duration to wait before executing the first time
InitialDelay time.Duration
InitialDelay() time.Duration
// InitiallyPassing is whether or not to consider the Check healthy before the
// initial execution
InitiallyPassing bool
InitiallyPassing() bool
}
// gosundheitCheck implements the health.Check interface backed by a CheckFn
type gosundheitCheck struct {
name string
checkFn CheckFn
// check implements the Check interface
type check struct {
name string
checkFn CheckFn
executionPeriod, initialDelay time.Duration
initiallyPassing bool
}
// Name implements the health.Check interface by returning a unique name
func (c gosundheitCheck) Name() string { return c.name }
// Name is the identifier for this check and must be unique among all Checks
func (c check) Name() string { return c.name }
// Execute implements the health.Check interface by executing the checkFn and
// returning the results
func (c gosundheitCheck) Execute() (interface{}, error) { return c.checkFn() }
// Execute performs the health check. It returns nil if the check passes.
// It can also return additional information to marshal and display to the caller
func (c check) Execute() (interface{}, error) { return c.checkFn() }
// ExecutionPeriod is the duration to wait between executions of this Check
func (c check) ExecutionPeriod() time.Duration { return c.executionPeriod }
// InitialDelay is the duration to wait before executing the first time
func (c check) InitialDelay() time.Duration { return c.initialDelay }
// InitiallyPassing is whether or not to consider the Check healthy before the initial execution
func (c check) InitiallyPassing() bool { return c.initiallyPassing }
// Heartbeater provides a getter to the most recently observed heartbeat
type Heartbeater interface {

View File

@ -17,7 +17,7 @@ import (
)
// defaultCheckOpts is a Check whose properties represent a default Check
var defaultCheckOpts = Check{ExecutionPeriod: time.Minute}
var defaultCheckOpts = check{executionPeriod: time.Minute}
// Health observes a set of vital signs and makes them available through an HTTP
// API.
@ -61,18 +61,18 @@ func (h *Health) RegisterHeartbeat(name string, hb Heartbeater, max time.Duratio
// RegisterCheckFunc adds a Check with default options and the given CheckFn
func (h *Health) RegisterCheckFunc(name string, checkFn CheckFn) error {
check := defaultCheckOpts
check.Name = name
check.CheckFn = checkFn
check.name = name
check.checkFn = checkFn
return h.RegisterCheck(check)
}
// RegisterCheck adds the given Check
func (h *Health) RegisterCheck(c Check) error {
return h.health.RegisterCheck(&health.Config{
InitialDelay: c.InitialDelay,
ExecutionPeriod: c.ExecutionPeriod,
InitiallyPassing: c.InitiallyPassing,
Check: gosundheitCheck{c.Name, c.CheckFn},
InitialDelay: c.InitialDelay(),
ExecutionPeriod: c.ExecutionPeriod(),
InitiallyPassing: c.InitiallyPassing(),
Check: c,
})
}