healthz

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2017 License: MIT Imports: 5 Imported by: 8

README

Healthz

Build Status Go Report Card GoDoc

Tools for providing service health checks.

License

The MIT License (MIT). Please see License File for more information.

Documentation

Overview

Package healthz provides tools for service health checks

The easiest way to setup Kubernetes-like liveness and readiness checks:

liveness  := healthz.NewTCPChecker(":80")
readiness := healthz.NewStatusChecker()

healthService := healthz.HealthService{
	healthz.LivenessCheck: liveness,
	healthz.ReadinessCheck: readiness,
}

mux := http.NewServeMux()
mux.Handle("/healthz", healthService.Handler(healthz.LivenessCheck))
mux.Handle("/readiness", healthService.Handler(healthz.ReadinessCheck))

http.ListenAndServe(":8081", mux)

Setup the health service using a collector:

collector := &healthz.Collector{}

liveness := healthz.NewTCPChecker(":80")
collector.RegisterChecker(healthz.LivenessCheck, liveness)

readiness := healthz.NewStatusChecker()
collector.RegisterChecker(healthz.ReadinessCheck, readiness)

healthService := collector.NewHealthService()
mux := http.NewServeMux()

mux.HandleFunc("/liveness", healthService.HealthStatus)
mux.HandleFunc("/readiness", healthService.ReadinessStatus)
http.ListenAndServe(":8081", mux)

Index

Constants

View Source
const (
	LivenessCheck  = "liveness"
	ReadinessCheck = "readiness"
)

Common check types.

Variables

View Source
var ErrCheckFailed = errors.New("Check failed")

ErrCheckFailed is a generic error which MAY be returned when a check fails.

Functions

func NewHandler added in v0.9.0

func NewHandler(checker Checker) http.Handler

NewHandler returns a new HTTP handler for a checker.

Types

type AlwaysFailureChecker added in v0.4.0

type AlwaysFailureChecker struct{}

AlwaysFailureChecker always returns failure as the check result.

This is useful for testing purposes.

func (*AlwaysFailureChecker) Check added in v0.4.0

func (c *AlwaysFailureChecker) Check() error

Check implements the Checker interface.

type AlwaysSuccessChecker added in v0.4.0

type AlwaysSuccessChecker struct{}

AlwaysSuccessChecker always returns success as the check result.

This is useful for testing purposes.

func (*AlwaysSuccessChecker) Check added in v0.4.0

func (c *AlwaysSuccessChecker) Check() error

Check implements the Checker interface.

type CheckFunc added in v0.4.0

type CheckFunc func() error

CheckFunc is a convenience type to create functions that implement the Checker interface.

func (CheckFunc) Check added in v0.4.0

func (f CheckFunc) Check() error

Check implements the Checker interface and allows any func() error signatured method to be passed as a Checker.

type Checker added in v0.4.0

type Checker interface {
	// Check returns nil if the check passes.
	Check() error
}

Checker is the interface for checking different resources.

type Collector added in v0.7.0

type Collector map[string][]Checker

Collector is a global context structure to accept checkers from all kinds of sources. It aggregates them and returns a HealthService.

func (Collector) Handler added in v0.9.0

func (c Collector) Handler(check string) http.Handler

Handler returns an http.Handler for a check. If a check is not found the returned handler will always return success.

func (Collector) RegisterChecker added in v0.7.0

func (c Collector) RegisterChecker(check string, checker Checker)

RegisterChecker registers a new checker for a specific check.

type CompositeChecker added in v0.8.0

type CompositeChecker struct {
	// contains filtered or unexported fields
}

CompositeChecker is responsible for executing a series of checks and decide if the resource is up or not.

func NewCompositeChecker added in v0.8.0

func NewCompositeChecker(checkers ...Checker) *CompositeChecker

NewCompositeChecker is a shortcut for easily creating a new Checker collection.

func (*CompositeChecker) Check added in v0.8.0

func (c *CompositeChecker) Check() error

Check implements the Checker interface and executes the underlying checks.

Note that since we have no information about what may become a Checker, this cannot be called concurrently.

type HTTPChecker added in v0.4.0

type HTTPChecker struct {
	// contains filtered or unexported fields
}

HTTPChecker checks if an HTTP endpoint is available and successfully responds.

func NewHTTPChecker added in v0.4.0

func NewHTTPChecker(url string, opts ...HTTPCheckerOption) *HTTPChecker

NewHTTPChecker creates a new HTTPChecker with a URL and optional configuration.

Example:

checker := healthz.NewHTTPChecker("http://example.com", healthz.WithHTTPTimeout(3*time.Second))

func (*HTTPChecker) Check added in v0.4.0

func (c *HTTPChecker) Check() error

Check implements the Checker interface and checks the HTTP endpoint status.

type HTTPCheckerOption added in v0.6.0

type HTTPCheckerOption func(*HTTPChecker)

HTTPCheckerOption configures how we check the HTTP endpoint.

func WithHTTPMethod added in v0.6.0

func WithHTTPMethod(method string) HTTPCheckerOption

WithHTTPMethod returns an HTTPCheckerOption that specifies the method for HTTP requests.

The default method is "GET" which works in most of the cases, but another popular choice is "HEAD".

func WithHTTPTimeout added in v0.6.0

func WithHTTPTimeout(timeout time.Duration) HTTPCheckerOption

WithHTTPTimeout returns an HTTPCheckerOption that specifies the timeout for HTTP requests.

Setting a timeout is highly recommended, but it needs to be carefully chosen to avoid false results.

type Handler added in v0.9.0

type Handler struct {
	// contains filtered or unexported fields
}

Handler is responsible for serving the health check over HTTP.

func (*Handler) ServeHTTP added in v0.9.0

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type PingChecker added in v0.5.0

type PingChecker struct {
	// contains filtered or unexported fields
}

PingChecker checks if a Pinger responds without an error.

func NewPingChecker added in v0.5.0

func NewPingChecker(pinger Pinger) *PingChecker

NewPingChecker creates a new PingChecker with a Pinger.

func (*PingChecker) Check added in v0.5.0

func (c *PingChecker) Check() error

Check implements the Checker interface and checks a resource status by pinging it.

type Pinger added in v0.5.0

type Pinger interface {
	Ping() error
}

Pinger is a commonly used interface to check if a connection is alive (used in sql.DB for example).

type Status added in v0.4.0

type Status int

Status is an enum type representing health status.

const (
	Healthy Status = iota
	Unhealthy
)

Possible values are Health and Unhealthy.

type StatusChecker added in v0.4.0

type StatusChecker struct {
	// contains filtered or unexported fields
}

StatusChecker checks the status based on an internal state.

func NewStatusChecker added in v0.4.0

func NewStatusChecker(status Status) *StatusChecker

NewStatusChecker creates a new StatusChecker with an initial state.

func (*StatusChecker) Check added in v0.4.0

func (c *StatusChecker) Check() error

Check implements the Checker interface and checks the internal state. Returns an error if the value of state is false.

func (*StatusChecker) SetStatus added in v0.4.0

func (c *StatusChecker) SetStatus(status Status)

SetStatus sets the internal state of the checker.

type TCPChecker added in v0.6.0

type TCPChecker struct {
	// contains filtered or unexported fields
}

TCPChecker checks if something is listening on a TCP address.

func NewTCPChecker added in v0.6.0

func NewTCPChecker(addr string, opts ...TCPCheckerOption) *TCPChecker

NewTCPChecker creates a new TCPChecker with an address and optional configuration. Example:

checker := healthz.NewTCPChecker(":80", healthz.WithTCPTimeout(3*time.Second))

func (*TCPChecker) Check added in v0.6.0

func (c *TCPChecker) Check() error

Check implements the Checker interface and checks the TCP address status.

type TCPCheckerOption added in v0.6.0

type TCPCheckerOption func(*TCPChecker)

TCPCheckerOption configures how we check the TCP address.

func WithTCPTimeout added in v0.6.0

func WithTCPTimeout(timeout time.Duration) TCPCheckerOption

WithTCPTimeout returns a TCPCheckerOption that specifies the timeout for TCP requests.

Setting a timeout is highly recommended, but it needs to be carefully chosen to avoid false results.

Jump to

Keyboard shortcuts

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