health

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StartedAt = time.Now()

StartedAt is the time the service was started.

View Source
var Version string

Version of service, initialized at compiled time.

Functions

func Handler

func Handler(chk Checker) http.HandlerFunc

Handler returns a HTTP handler that serves health check requests. The response body is the health status returned by chk.Check(). By default it's encoded as JSON, but you can specify a different encoding in the HTTP Accept header. The response status is 200 if chk.Check() returns a nil error, 503 otherwise.

Types

type Checker

type Checker interface {
	// Check that all dependencies are healthy. Check returns true
	// if the service is healthy. The returned Health struct
	// contains the health status of each dependency.
	Check(context.Context) (*Health, bool)
}

Checker exposes a health check.

func NewChecker

func NewChecker(deps ...Pinger) Checker

Create a Checker that checks the health of the given dependencies.

type Health

type Health struct {
	// Uptime of service in seconds.
	Uptime int64 `json:"uptime"`
	// Version of service.
	Version string `json:"version"`
	// Status of each dependency indexed by service name.
	// "OK" if dependency is healthy, "NOT OK" otherwise.
	Status map[string]string `json:"status,omitempty"`
}

Health status of a service.

func (Health) MarshalXML

func (h Health) MarshalXML(e *xml.Encoder, start xml.StartElement) error

type Option

type Option func(o *options)

Option configures a Pinger.

func WithPath

func WithPath(path string) Option

WithPath sets the path used to ping the service. Default path is "/livez".

func WithScheme

func WithScheme(scheme string) Option

WithScheme sets the scheme used to ping the service. Default scheme is "http".

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout used to ping the service. Default is no timeout.

func WithTransport

func WithTransport(transport http.RoundTripper) Option

type Pinger

type Pinger interface {
	// Name of remote service.
	Name() string
	// Ping the remote service, return a non nil error if the
	// service is not available.
	Ping(context.Context) error
}

Pinger makes it possible to ping a service.

func NewPinger

func NewPinger(name, addr string, opts ...Option) Pinger

NewPinger returns a new health-check client for the given service. It panics if the given host address is malformed. The default scheme is "http" and the default path is "/livez". Both can be overridden via options.

Jump to

Keyboard shortcuts

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