Documentation ¶
Overview ¶
Package health provides functionality to add application-level health checks to individual application components. Health check results can be exposed using an HTTP route and handler. Components are expected to register health checkers, either as Checker interfaces or CheckerFunc using Register() and RegisterFunc() respectively. Registration can be done in package init() function, or explicitly when the component is created. The health check returns a "binary" healthy/unhealthy status and may add additional message. An unhealthy component may optional add a root cause, typically an error value returned by some internal check procedure. The HTTP handler is added by attaching health.Handler() to a route. The returned body is a JSON encoding of a map of components to their corresponding health status.
Index ¶
Constants ¶
const ( // CheckPanicked is reported when a health check panics CheckPanicked = "healthcheck panic" // HTTPStatusCodeHealthChecksPass is the HTTP Status code used to indicate success HTTPStatusCodeHealthChecksPass = http.StatusOK // HTTPStatusCodeHealthChecksFail is the HTTP Status code used to indicate health check failure HTTPStatusCodeHealthChecksFail = http.StatusServiceUnavailable )
Variables ¶
var ( // Healthy is a healthy status with no additional properties. Healthy = Status{Healthy: true} )
Functions ¶
func Components ¶
func Components() []string
Components returns the registered components names (in arbitrary order).
func Handler ¶
func Handler() http.HandlerFunc
Handler returns an http.HandlerFunc that can be used to retrieve the JSON representation of health check statuses. The returned representation is a mapping from component name to its status.
func RegisterFunc ¶
RegisterFunc adds the Checker function and named component to the set of monitored components.
func RunChecks ¶
RunChecks executes all health checks, returning a mapping between registered component names and their health check status.
func Unregister ¶
func Unregister(name string)
Unregister removes the health checker currently registered for the named component.
Types ¶
type Checker ¶
type Checker interface { // Check performs a health check of the component. // Health checks should normally return quickly, and avoid synchronous network calls or long-running computations. // If such operations are needed, they should be performed in the background (e.g., by a separate goroutine). Check() Status }
The Checker type defines an interface with a single Check() function that determines the health of a component and returns its status back to the caller.
type CheckerFunc ¶
type CheckerFunc func() Status
The CheckerFunc is an adapter to allow the use of ordinary functions as health checkers. If fn is a function with the appropriate signature, CheckerFunc(fn) is a Checker object that calls fn.
type Status ¶
type Status struct { Healthy bool `json:"healthy"` Properties map[string]interface{} `json:"properties,omitempty"` }
Status is the result of a health check run.
func StatusHealthy ¶
StatusHealthy creates a new healthy status with given message property. To return a default healthy status, with no properties, just use health.Healthy
func StatusHealthyWithProperties ¶
StatusHealthyWithProperties creates a new healthy status with given properties.
func StatusUnhealthy ¶
StatusUnhealthy creates a new unhealthy status with the given message and error properties.
func StatusUnhealthyWithProperties ¶
StatusUnhealthyWithProperties creates a new unhealthy status with given properties.