health

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2017 License: Apache-2.0 Imports: 5 Imported by: 21

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

View Source
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

View Source
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 Register

func Register(name string, check Checker)

Register adds the Checker and named component to the set of monitored components.

func RegisterFunc

func RegisterFunc(name string, checker func() Status)

RegisterFunc adds the Checker function and named component to the set of monitored components.

func RunChecks

func RunChecks() map[string]Status

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.

func (CheckerFunc) Check

func (fn CheckerFunc) Check() Status

Check 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

func StatusHealthy(message string) Status

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

func StatusHealthyWithProperties(properties map[string]interface{}) Status

StatusHealthyWithProperties creates a new healthy status with given properties.

func StatusUnhealthy

func StatusUnhealthy(message string, cause error) Status

StatusUnhealthy creates a new unhealthy status with the given message and error properties.

func StatusUnhealthyWithProperties

func StatusUnhealthyWithProperties(properties map[string]interface{}) Status

StatusUnhealthyWithProperties creates a new unhealthy status with given properties.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL