Documentation
¶
Overview ¶
Package healthz provides an HTTP handler that returns information about the health status of the application.
Index ¶
- Constants
- Variables
- func AddBuildInfo()
- func DeleteMeta(name string)
- func Deregister(name string)
- func Handler() http.Handler
- func IsScopedMultiError(err error) bool
- func IsWarning(err error) bool
- func Register(name string, period time.Duration, fn CheckFunc)
- func RegisterRemote(name string, period time.Duration, url string, opt *RemoteOptions) error
- func Set(name string, err error, timeout time.Duration)
- func SetMeta(name string, value interface{})
- func Warn(msg string) error
- func Warnf(format string, args ...interface{}) error
- type CheckFunc
- type Checker
- func (c *Checker) AddBuildInfo()
- func (c *Checker) Close()
- func (c *Checker) DeleteMeta(name string)
- func (c *Checker) Deregister(name string)
- func (c *Checker) Handler() http.Handler
- func (c *Checker) Register(name string, period time.Duration, fn CheckFunc)
- func (c *Checker) RegisterRemote(name string, period time.Duration, url string, opt *RemoteOptions) error
- func (c *Checker) Set(name string, err error, expiry time.Duration)
- func (c *Checker) SetMeta(name string, value interface{})
- func (c *Checker) Status() Status
- type Config
- type Expired
- type RemoteOptions
- type Runtime
- type ScopedMultiError
- type Status
- type Warning
Constants ¶
const ( // DefaultRuntimeTTL is the default TTL of the collected runtime stats. DefaultRuntimeTTL = 15 * time.Second // DefaultCheckPeriod is the default check period if 0 is passed to Register DefaultCheckPeriod = time.Second )
const ( // StatusOK is returned when all the registered checks pass. StatusOK = "OK" StatusUnavailable = "Unavailable" // StatusWarning is returned when one or more registered check returns // a warning and none returns a fatal error. StatusWarning = "Warning" )
const ( // RemoteDefaultTimeout is the default timeout for fetching a remote // healthz in RegisterRemote. RemoteDefaultTimeout = 10 * time.Second )
Variables ¶
var ( // DefaultChecker is the default global checker referenced by the shortcut // functions in this package. Change this variable with caution, because // you will lose any checkers that have already been registered to the // old one. DefaultChecker = NewChecker(&Config{DefaultRuntimeTTL}) )
Functions ¶
func AddBuildInfo ¶
func AddBuildInfo()
AddBuildInfo adds some build info like VCS from debug.ReadBuildInfo to the metadata.
func DeleteMeta ¶
func DeleteMeta(name string)
DeleteMeta is a shortcut for DefaultChecker.DeleteMeta. See there for more information.
func Deregister ¶
func Deregister(name string)
Deregister is a shortcut for DefaultChecker.Deregister. See there for more information.
func IsScopedMultiError ¶
IsScopedMultiError returns true if the error is a ScopedMultiError. It will NOT attempt to unwrap the error.
func IsWarning ¶
IsWarning returns true if the error is a Warning instead of a failure. It will recursively unwrap the error to look for a Warning.
func RegisterRemote ¶
RegisterRemote registers a remote /healthz endpoint that needs to be monitored. See Checker.RegisterRemote for details.
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker is a health status checker responsible for evaluating the registered checks as well as of collecting useful runtime information about the Go Process. It provides an HTTP handler that returns the current health status.
func NewChecker ¶
NewChecker creates a new Checker. Using a custom Checker instead of the global DefaultChecker is not recommended.
func (*Checker) AddBuildInfo ¶
func (c *Checker) AddBuildInfo()
AddBuildInfo will add build information like the Go version and VCS to the exposed metadata.
func (*Checker) DeleteMeta ¶
DeleteMeta deletes a named entry from the configured metadata.
func (*Checker) Deregister ¶
Deregister deregisters a check.
func (*Checker) Handler ¶
Handler returns an HTTP handler to be used as a health check endpoint. If the application is healthy and all the registered check pass, it returns a `200 OK` HTTP status code, otherwise, it fails with a `503 Service Unavailable` code. All responses contain a JSON encoded payload with information about the runtime system, current checks statuses and some configurable metadata.
func (*Checker) RegisterRemote ¶
func (c *Checker) RegisterRemote(name string, period time.Duration, url string, opt *RemoteOptions) error
RegisterRemote registers a remote /healthz endpoint that needs to be monitored. The period parameter determines the poll interval. The url must contain the full url to the healthz endpoint.
If the remote endpoint uses the same JSON structure as this instance does, the name is used to prefix all remote failures and warnings, separated by a slash ('/'). E.g. if the name is "foo", a remote "bar" error will end up as "foo/bar" in the status reported by this instance. It the remote endpoint is not served by this library and no compatible failures and warnings keys could be found, this check returns a single error for this endpoint with the requested name.
func (*Checker) Set ¶
Set sets a static status value without a periodic checker function. This can be useful if your application has an event loop that can directly update the status for real-time information, instead of relying on a checker function to run periodically. If the expiry duration is not 0, the status will be reset to Expired after this duration, if no new value is set in the meantime.
type Config ¶
type Config struct { // RuntimeTTL is the time between checking runtime stats like memory usage. // It defaults to DefaultRuntimeTTL. RuntimeTTL time.Duration }
Config parameterizes a Checker.
type Expired ¶
type Expired struct {
// contains filtered or unexported fields
}
Expired is the error status set after a status set by SetMeta has expired.
type RemoteOptions ¶
type RemoteOptions struct { // Client allows you to override the default http.Client Client *http.Client // optional client override // Timeout allows you to override the default timeout of RemoteDefaultTimeout // used by RegisterRemote. If the Client is overridden, this does nothing. Timeout time.Duration // optional timeout, if the default is not OK // AsWarnings instructs RegisterRemote to downgrade any remote errors to // warnings for this check. AsWarnings bool // Warn404 make a remote 404 a warning instead of an error. This is useful // if you are not sure if the target url has a healthz endpoint. Warn404 bool }
RemoteOptions are options passed to RegisterRemote
type Runtime ¶
type Runtime struct { CollectedAt time.Time `json:"-"` Arch string `json:"arch"` OS string `json:"os"` Version string `json:"version"` GoroutinesCount int `json:"goroutines_count"` HeapObjectsCount int `json:"heap_objects_count"` AllocBytes int `json:"alloc_bytes"` TotalAllocBytes int `json:"total_alloc_bytes"` }
Runtime contains statistics about the Go's process.
type ScopedMultiError ¶
ScopedMultiError contains multiple errors keyed by a unique name
func (ScopedMultiError) Error ¶
func (e ScopedMultiError) Error() string
type Status ¶
type Status struct { OK bool `json:"ok"` // May have warnings HasWarnings bool `json:"has_warnings"` Status string `json:"status"` Time time.Time `json:"time"` Since time.Time `json:"since"` Runtime Runtime `json:"runtime"` Failures map[string]string `json:"failures"` Warnings map[string]string `json:"warnings"` Metadata map[string]interface{} `json:"metadata,omitempty"` }
Status represents the service health status.