Documentation
¶
Index ¶
- func WithCacheDuration(duration time.Duration) option
- func WithCheck(check Check) option
- func WithDisabledCache() option
- func WithDisabledDetails() option
- func WithManualStart() option
- func WithMaxErrorMessageLength(length uint) option
- func WithPeriodicCheck(refreshPeriod time.Duration, check Check) option
- func WithStatusListener(listener SystemStatusListener) option
- func WithTimeout(timeout time.Duration) option
- type AvailabilityStatus
- type Check
- type CheckState
- type CheckStatus
- type CheckStatusListener
- type Checker
- type Handler
- type SystemStatus
- type SystemStatusListener
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithCacheDuration ¶
WithCacheDuration sets the duration for how long the aggregated health check result will be cached. This is set to 1 second by default. 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 AvailabilityStatus. This check will be triggered each time the health check HTTP endpoint is called (and the cache has expired, see WithCacheDuration). If health checks are expensive or you expect a lot of calls to 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.
func WithDisabledDetails ¶ added in v0.3.0
func WithDisabledDetails() option
WithDisabledDetails disables hides all data in the JSON response body but the the AvailabilityStatus itself. Example: { "AvailabilityStatus":"down" }
func WithManualStart ¶
func WithManualStart() option
WithManualStart prevents an automatic start of periodic checks (see NewHandler). If this configuration option is used and you want to start periodic checks yourself, you need to start them by using Start.
func WithMaxErrorMessageLength ¶
func WithMaxErrorMessageLength(length uint) option
WithMaxErrorMessageLength limits maximum number of characters in error messages.
func WithPeriodicCheck ¶
WithPeriodicCheck adds a new health check that contributes to the overall service availability AvailabilityStatus. 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. The health endpoint always returns the last result of the periodic check. When periodic checks are started (happens automatically if WithManualStart is not used) they are also executed for the first time. Until all periodic checks have not been executed at least once, the overall availability AvailabilityStatus will be "unknown" with HTTP AvailabilityStatus code 503 (Service Unavailable).
func WithStatusListener ¶ added in v0.3.0
func WithStatusListener(listener SystemStatusListener) option
WithStatusListener registers a handler function that will be called whenever the overall system health AvailabilityStatus changes. Attention: Ideally, this method should be quick and not block for too long.
func WithTimeout ¶
WithTimeout globally defines a timeout duration for all checks. You can still override this timeout by using the timeout value in the Check configuration. Default value is 30 seconds.
Types ¶
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 yet, because no check was yet. StatusUnknown AvailabilityStatus = "unknown" // StatusUp holds the information that the system or component // is available. StatusUp AvailabilityStatus = "up" // StatusDown holds the information that the system or component // is not available. StatusDown AvailabilityStatus = "down" )
type Check ¶
type Check struct {
// The Name must be unique among all checks. Name is a required attribute.
Name string
// 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
// Timeout will override the global timeout value, if it is smaller than
// the global timeout (see WithTimeout).
Timeout time.Duration
// MaxTimeInError will set a duration for how long a service must be
// in an error state until it is considered unavailable.
MaxTimeInError time.Duration
// MaxConsecutiveFails will set a maximum number of consecutive
// check fails until the service is considered unavailable.
MaxConsecutiveFails uint
// StatusListener allows to set a listener that will be called
// whenever the AvailabilityStatus of the check changes.
StatusListener CheckStatusListener
// 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 was "up".
LastSuccessAt time.Time
// LastResult holds the error of the last check (is 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
// contains filtered or unexported fields
}
CheckState contains all state attributes of a components 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 happened.
Timestamp time.Time `json:"timestamp,omitempty"`
// Error contains the error message, if a check was not successful.
Error *string `json:"error,omitempty"`
}
CheckStatus holds the a components health information.
type CheckStatusListener ¶ added in v0.3.0
type CheckStatusListener func(name string, state CheckState)
CheckStatusListener is a callback function that will be called when a components availability status changes (e.g. from "up" to "down").
type Checker ¶ added in v0.3.0
type Checker interface {
// Start starts all periodic checks and prepares the
// checker for accepting check requests.
Start()
// Stop stops all periodic checks.
Stop()
// Check performs a health check. The context may contain
// deadlines to which will be adhered to and will be
// passed to downstream calls.
Check(ctx context.Context) SystemStatus
}
Checker is the main checker interface.
func NewChecker ¶ added in v0.3.0
func NewChecker(options ...option) Checker
NewChecker creates a standalone health checker. If periodic checks have been configured (see WithPeriodicCheck) or if automatic start is explicitly turned off using WithManualStart). It operates in the same way as NewHandler but returning the Checker directly instead of the handler.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶ added in v0.2.0
NewHandler creates a new health check http.Handler. If periodic checks have been configured (see WithPeriodicCheck), they will be started as well (if not explicitly turned off using WithManualStart).
func NewHandlerWithStatusCodes ¶
NewHandlerWithStatusCodes creates a new health check http.Handler. If periodic checks have been configured (see WithPeriodicCheck), they will be started as well (if not explicitly turned off using WithManualStart).
type SystemStatus ¶ added in v0.3.0
type SystemStatus struct {
// Status is the aggregated availability status of the system.
Status AvailabilityStatus `json:"status"`
// Details contains health information about all checked components.
Details *map[string]CheckStatus `json:"details,omitempty"`
}
SystemStatus holds the aggregated system health information.
type SystemStatusListener ¶ added in v0.3.0
type SystemStatusListener func(status AvailabilityStatus, state map[string]CheckState)
SystemStatusListener is a callback function that will be called when the system availability status changes (e.g. from "up" to "down").