Documentation ¶
Overview ¶
Package healthcheck provides types and functions to implement liveness and readyness checks based on HTTP probes. The package provides a http.Handler that can be mounted using to a running server. Client code can register checks to be executed when the readyness endpoint is invoked. The package also provides ready to use checks for HTTP endpoints and SQL databases.
The handler also reports version information of the running application. This is an opt-in feature disabled by default. The version info will be gathered using the runtime/debug and can be enhanced with custom fields.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Configures the final path element of the URL serving the liveness check. // Changes to this variable will only take effect when done before calling New. LivePath = "/livez" // Configures the final path element of the URL serving the readyness check. // Changes to this variable will only take effect when done before calling New. ReadyPath = "/readyz" // Configures the final path element of the URL serving the info endpoint. // Changes to this variable will only take effect when done before calling New. InfoPath = "/infoz" // Default timeout to apply to readyness checks DefaultReadynessCheckTimeout = 10 * time.Second )
var ErrPingCheckFailed = errors.New("ping check failed")
var ErrURLCheckFailed = errors.New("URL check failed")
Functions ¶
This section is empty.
Types ¶
type Check ¶
type Check interface { // Check is called to execute the check. Any non-nil return value // is considered a check failure incl. context deadlines. Check(context.Context) error }
Check defines the interface for custom readyness checks.
func CheckHTTPResponse ¶
CheckHTTPResponse creates a Check that issues a HTTP request with method to url using client. The check reports an error if either the request fails or the received status code is >= 400 (bad request). If client is nil http.DefaultClient is used.
type ErrorLogger ¶
type ErrorLogger func(err error)
ErrorLogger defines a type for a function to log errors that occured during ready check execution. err is the error returned by the check function.
type Handler ¶
type Handler struct { ErrorLogger ErrorLogger ReadynessTimeout time.Duration // contains filtered or unexported fields }
Handler implements liveness and readyness checking.
func New ¶
New creates a new Handler ready to use. The Handler must be mounted on some HTTP path (i.e. on a http.ServeMux) to receive requests.
func (*Handler) AddCheckFunc ¶
AddCheckFunc registers c as another readyness check.
func (*Handler) EnableInfo ¶
EnableInfo enables an info endpoint that outputs version information and additional details.
func (*Handler) ExecuteReadyChecks ¶
ExecuteReadyChecks executes all readyness checks in parallel. It reports the first error hit or nil if all checks pass. Every check is executed with a timeout configured for the handler (if any).
type Option ¶
type Option func(*Handler)
Option defines a function type used to customize the provided Handler.
func WithErrorLogger ¶
func WithErrorLogger(l ErrorLogger) Option
WithErrorLogger creates an Option that sets Handler.ErrorLogger to l.
func WithReadynessTimeout ¶
WithReadynessTimeout creates an Option that sets Handler.ReadynessTimeout to t.
type Pinger ¶
type Pinger interface { // PingContext pings the remote endpoint. It returns nil if the endpoint is // healthy, a non-nil error otherwise. PingContext(context.Context) error }
Pinger defines the interface for connection types that support pinging the remote endpoint to learn about its liveness. The method name is chosen to make a value of type *sql.DB satisfy this interface without any adaption.