Documentation
¶
Index ¶
- func NewHandler(checker Checker) http.Handler
- func NewHandlerFunc(checker Checker) http.HandlerFunc
- func NewHandlerFuncWithConfig(checker Checker, cfg HandlerConfig) http.HandlerFunc
- func NewHandlerWithConfig(checker Checker, cfg HandlerConfig) http.Handler
- func WithAfterCheckListener(listener AfterCheckListener) option
- func WithBeforeCheckListener(listener BeforeCheckListener) option
- func WithCacheDuration(duration time.Duration) option
- func WithCheck(check Check) option
- func WithDisabledCache() option
- func WithDisabledDetails() option
- func WithMaxErrorMessageLength(length uint) option
- func WithPeriodicCheck(refreshPeriod time.Duration, check Check) option
- func WithStatusListener(listener StatusListener) option
- func WithTimeout(timeout time.Duration) option
- type AfterCheckListener
- type AfterComponentCheckListener
- type AvailabilityStatus
- type BeforeCheckListener
- type BeforeComponentCheckListener
- type Check
- type CheckState
- type CheckStatus
- type Checker
- type ComponentStatusListener
- type HandlerConfig
- type StatusListener
- type SystemStatus
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶ added in v0.2.0
NewHandler creates a new health check http.Handler. The Checker will be started automatically (see Checker.Start).
func NewHandlerFunc ¶ added in v0.3.0
func NewHandlerFunc(checker Checker) http.HandlerFunc
NewHandlerFunc creates a new health check http.Handler. The Checker will be started automatically (see Checker.Start).
func NewHandlerFuncWithConfig ¶ added in v0.3.0
func NewHandlerFuncWithConfig(checker Checker, cfg HandlerConfig) http.HandlerFunc
NewHandlerFuncWithConfig creates a new health check http.Handler. If HandlerConfig.DisableCheckerAutostart is not true, the Checker will be started automatically (see Checker.Start).
func NewHandlerWithConfig ¶ added in v0.3.0
func NewHandlerWithConfig(checker Checker, cfg HandlerConfig) http.Handler
NewHandlerWithConfig creates a new health check http.Handler. If HandlerConfig.DisableCheckerAutostart is not true, the Checker will be started automatically (see Checker.Start).
func WithAfterCheckListener ¶ added in v0.3.0
func WithAfterCheckListener(listener AfterCheckListener) option
WithAfterCheckListener registers a handler function that will be called whenever the overall system health AvailabilityStatus changes.
func WithBeforeCheckListener ¶ added in v0.3.0
func WithBeforeCheckListener(listener BeforeCheckListener) option
WithBeforeCheckListener registers a handler function that will be called whenever the overall system health AvailabilityStatus changes.
func WithCacheDuration ¶
WithCacheDuration sets the duration for how long the aggregated health check result will be cached. By default, the cache TTL (i.e, the duration for how long responses will be cached) is set to 1 second. Caching will prevent that each incoming HTTP request triggers a new health check. A duration of 0 will effectively disable the cache and has the same effect as WithDisabledCache.
func WithCheck ¶
func WithCheck(check Check) option
WithCheck adds a new health check that contributes to the overall service availability status. This check will be triggered each time Checker.Check is called (i.e., for each HTTP request). If health checks are expensive or you expect a bigger amount of requests on your the health endpoint, consider using WithPeriodicCheck instead.
func WithDisabledCache ¶
func WithDisabledCache() option
WithDisabledCache disabled the check cache. This is not recommended in most cases. This will effectively lead to a health endpoint that initiates a new health check for each incoming HTTP request. This may have an impact on the systems that are being checked (especially if health checks are expensive). Caching also mitigates "denial of service" attacks. Caching is enabled by default.
func WithDisabledDetails ¶ added in v0.3.0
func WithDisabledDetails() option
WithDisabledDetails disables all data in the JSON response body. The AvailabilityStatus will be the only content. Example: { "status":"down" }. Enabled by default.
func WithMaxErrorMessageLength ¶
func WithMaxErrorMessageLength(length uint) option
WithMaxErrorMessageLength limits maximum number of characters in error messages. Default is 500.
func WithPeriodicCheck ¶
WithPeriodicCheck adds a new health check that contributes to the overall service availability status. The health check will be performed on a fixed schedule and will not be executed for each HTTP request (as in contrast to WithCheck). This allows to process a much higher number of HTTP requests without actually calling the checked services too often or to execute long running checks. This way Checker.Check (and the health endpoint) always returns the last result of the periodic check.
func WithStatusListener ¶ added in v0.3.0
func WithStatusListener(listener StatusListener) option
WithStatusListener registers a handler function that will be called whenever the overall system health AvailabilityStatus changes.
func WithTimeout ¶
WithTimeout defines a timeout duration for all checks. You can override this timeout by using the timeout value in the Check configuration. Default value is 30 seconds.
Types ¶
type AfterCheckListener ¶ added in v0.3.0
AfterCheckListener is a callback function that will be called right after a the availability status of the system was checked. The listener is allowed to add or remove values to/from the context in parameter ctx. The new context is expected in the return value of the function. If you do not want to extend the context, just return the passed ctx parameter. Attention: This listener will only be invoked when the Checker.Check function is executed (i.e., for every HTTP request). It will not be invoked before a periodic check function is executed!
type AfterComponentCheckListener ¶ added in v0.3.1
type AfterComponentCheckListener func(ctx context.Context, state CheckState) context.Context
AfterComponentCheckListener is a callback function that will be called right after a components availability status will be checked. The listener is allowed to add or remove values to/from the context in parameter ctx. The new context is expected in the return value of the function. If you do not want to extend the context, just return the passed ctx parameter.
type AvailabilityStatus ¶ added in v0.3.0
type AvailabilityStatus string
AvailabilityStatus expresses the availability of either a component or the whole system.
const ( // StatusUnknown holds the information that the availability // status is not known, because not all checks were executed yet. StatusUnknown AvailabilityStatus = "unknown" // StatusUp holds the information that the system or a component // is up and running. StatusUp AvailabilityStatus = "up" // StatusDown holds the information that the system or a component // down and not available. StatusDown AvailabilityStatus = "down" )
type BeforeCheckListener ¶ added in v0.3.0
BeforeCheckListener is a callback function that will be called right before a the availability status of the system will be checked. The listener is allowed to add/remove values to the context in parameter ctx. The new context is expected in the return value of the function. If you do not want to extend the context, just return the passed ctx parameter. Attention: This listener will only be invoked when the Checker.Check function is executed (i.e., for every HTTP request). It will not be invoked before a periodic check function is executed!
type BeforeComponentCheckListener ¶ added in v0.3.1
type BeforeComponentCheckListener func(ctx context.Context, state CheckState) context.Context
BeforeComponentCheckListener is a callback function that will be called right before a components availability status will be checked. The listener is allowed to add/remove values to the context in parameter ctx. The new context is expected in the return value of the function. If you do not want to extend the context, just return the passed ctx parameter.
type Check ¶
type Check struct {
// The Name must be unique among all checks. Name is a required attribute.
Name string // Required
// Check is the check function that will be executed to check availability.
// This function must return an error if the checked service is considered
// not available. Check is a required attribute.
Check func(ctx context.Context) error // Required
// Timeout will override the global timeout value, if it is smaller than
// the global timeout (see WithTimeout).
Timeout time.Duration // Optional
// MaxTimeInError will set a duration for how long a service must be
// in an error state until it is considered down/unavailable.
MaxTimeInError time.Duration // Optional
// MaxConsecutiveFails will set a maximum number of consecutive
// check fails until the service is considered down/unavailable.
MaxConsecutiveFails uint // Optional
// StatusListener allows to set a listener that will be called
// whenever the AvailabilityStatus of the check changes.
StatusListener ComponentStatusListener // Optional
// BeforeCheckListener is a callback function that will be called
// right before a components availability status will be checked.
BeforeCheckListener BeforeComponentCheckListener // Optional
// AfterCheckListener is a callback function that will be called
// right after a components availability status was checked.
AfterCheckListener AfterComponentCheckListener // Optional
// contains filtered or unexported fields
}
Check allows to configure health checks.
type CheckState ¶ added in v0.3.0
type CheckState struct {
// LastCheckedAt holds the time of when the check was last executed.
LastCheckedAt *time.Time
// LastCheckedAt holds the last time of when the check did not return an error.
LastSuccessAt *time.Time
// LastFailureAt holds the last time of when the check did return an error.
LastFailureAt *time.Time
// FirstCheckStartedAt holds the time of when the first check was started.
FirstCheckStartedAt time.Time
// LastResult holds the error of the last check (nil if successful).
LastResult error
// ConsecutiveFails holds the number of how often the check failed in a row.
ConsecutiveFails uint
// The current availability status of the check.
Status AvailabilityStatus
}
CheckState represents the current state of a component check.
type CheckStatus ¶ added in v0.3.0
type CheckStatus struct {
// Status is the availability status of a component.
Status AvailabilityStatus `json:"status"`
// Timestamp holds the time when the check was executed.
Timestamp *time.Time `json:"timestamp,omitempty"`
// Error contains the check error message, if the check failed.
Error *string `json:"error,omitempty"`
}
CheckStatus holds a components health information.
type Checker ¶ added in v0.3.0
type Checker interface {
// Start will start all necessary background workers and prepare
// the checker for further usage.
Start()
// Stop stops will stop the checker.
Stop()
// Check performs a health check. It expects a context, that
// may contain deadlines to which will be adhered to. The context
// will be passed to downstream calls.
Check(ctx context.Context) SystemStatus
// GetRunningPeriodicCheckCount returns the number of currently
// running periodic checks.
GetRunningPeriodicCheckCount() int
}
Checker is the main checker interface. It provides all health checking logic.
func NewChecker ¶ added in v0.3.0
func NewChecker(options ...option) Checker
NewChecker creates a new Checker. The provided options will be used to modify its configuration.
type ComponentStatusListener ¶ added in v0.3.1
type ComponentStatusListener func(ctx context.Context, state CheckState)
ComponentStatusListener is a callback function that will be called when a components availability status changes (e.g. from "up" to "down").
type HandlerConfig ¶ added in v0.3.0
type StatusListener ¶ added in v0.3.1
type StatusListener func(ctx context.Context, status AvailabilityStatus, state map[string]CheckState)
StatusListener is a callback function that will be called when the system availability status changes (e.g. from "up" to "down").
type SystemStatus ¶ added in v0.3.0
type SystemStatus struct {
// Status is the aggregated system availability status.
Status AvailabilityStatus `json:"status"`
// Details contains health information for all checked components.
Details *map[string]CheckStatus `json:"details,omitempty"`
}
SystemStatus holds the aggregated system availability status and detailed information about the individual checks.