diff --git a/api/health/checks.go b/api/health/checks.go index d11544b..262f0f6 100644 --- a/api/health/checks.go +++ b/api/health/checks.go @@ -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 { diff --git a/api/health/service.go b/api/health/service.go index b59cf7e..9c2e479 100644 --- a/api/health/service.go +++ b/api/health/service.go @@ -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, }) }