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

View File

@ -17,7 +17,7 @@ import (
) )
// defaultCheckOpts is a Check whose properties represent a default Check // 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 // Health observes a set of vital signs and makes them available through an HTTP
// API. // 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 // RegisterCheckFunc adds a Check with default options and the given CheckFn
func (h *Health) RegisterCheckFunc(name string, checkFn CheckFn) error { func (h *Health) RegisterCheckFunc(name string, checkFn CheckFn) error {
check := defaultCheckOpts check := defaultCheckOpts
check.Name = name check.name = name
check.CheckFn = checkFn check.checkFn = checkFn
return h.RegisterCheck(check) return h.RegisterCheck(check)
} }
// RegisterCheck adds the given Check // RegisterCheck adds the given Check
func (h *Health) RegisterCheck(c Check) error { func (h *Health) RegisterCheck(c Check) error {
return h.health.RegisterCheck(&health.Config{ return h.health.RegisterCheck(&health.Config{
InitialDelay: c.InitialDelay, InitialDelay: c.InitialDelay(),
ExecutionPeriod: c.ExecutionPeriod, ExecutionPeriod: c.ExecutionPeriod(),
InitiallyPassing: c.InitiallyPassing, InitiallyPassing: c.InitiallyPassing(),
Check: gosundheitCheck{c.Name, c.CheckFn}, Check: c,
}) })
} }