healthcheck

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 11 Imported by: 296

Documentation

Index

Constants

View Source
const (
	StatusOK       = "OK"
	StatusWarning  = "WARNING"
	StatusCritical = "CRITICAL"
)

A list of possible check statuses

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check struct {
	// contains filtered or unexported fields
}

Check represents a check performed by the health check

func NewCheck

func NewCheck(name string, checker Checker) (*Check, error)

NewCheck returns a pointer to a new instantiated Check with the provided checker function

func (*Check) MarshalJSON

func (c *Check) MarshalJSON() ([]byte, error)

MarshalJSON returns the json representation of the check as a byte array

func (*Check) UnmarshalJSON

func (c *Check) UnmarshalJSON(b []byte) error

UnmarshalJSON takes the json representation of a check as a byte array and populates the Check object

type CheckState

type CheckState struct {
	// contains filtered or unexported fields
}

CheckState represents the health status returned by a checker

func NewCheckState

func NewCheckState(name string) *CheckState

NewCheckState returns a pointer to a new instantiated CheckState

func (*CheckState) LastChecked

func (s *CheckState) LastChecked() *time.Time

LastChecked gets the last checked time of the check

func (*CheckState) LastFailure

func (s *CheckState) LastFailure() *time.Time

LastFailure gets the time of the last failed check

func (*CheckState) LastSuccess

func (s *CheckState) LastSuccess() *time.Time

LastSuccess gets the time of the last successful check

func (*CheckState) MarshalJSON

func (s *CheckState) MarshalJSON() ([]byte, error)

MarshalJSON returns the json representation of the check state as a byte array

func (*CheckState) Message

func (s *CheckState) Message() string

Message gets the check message

func (*CheckState) Name

func (s *CheckState) Name() string

Name gets the check name

func (*CheckState) Status

func (s *CheckState) Status() string

Status gets the check status

func (*CheckState) StatusCode

func (s *CheckState) StatusCode() int

StatusCode gets the check status code

func (*CheckState) UnmarshalJSON

func (s *CheckState) UnmarshalJSON(b []byte) error

UnmarshalJSON takes the json representation of a check state as a byte array and populates the CheckState object

func (*CheckState) Update

func (s *CheckState) Update(status, message string, statusCode int) error

Update updates the relevant state fields based on the status provided status of the check, must be one of healthcheck.StatusOK, healthcheck.StatusWarning or healthcheck.StatusCritical message briefly describing the check state statusCode returned if the check was an HTTP check (optional, provide 0 if not relevant) If any Subscriber is registered, the callback will be triggered if the state changed since last interation

type Checker

type Checker func(context.Context, *CheckState) error

Checker represents the interface all checker functions abide to

type HealthCheck

type HealthCheck struct {
	Status    string        `json:"status"`
	Version   VersionInfo   `json:"version"`
	Uptime    time.Duration `json:"uptime"`
	StartTime time.Time     `json:"start_time"`
	Checks    []*Check      `json:"checks"`
	// contains filtered or unexported fields
}

HealthCheck represents the app's health check, including its component checks

func New

func New(version VersionInfo, criticalTimeout, interval time.Duration) HealthCheck

New returns a new instantiated HealthCheck object. Caller to provide: version information of the app, criticalTimeout for how long to wait until an unhealthy dependent propagates its state to make this app unhealthy interval in which to check health of dependencies

func (*HealthCheck) AddAndGetCheck added in v1.2.0

func (hc *HealthCheck) AddAndGetCheck(name string, checker Checker) (check *Check, err error)

AddAndGetCheck adds a provided checker to the health check and returns the corresponding Check pointer, which maybe used for subscription

func (*HealthCheck) AddCheck

func (hc *HealthCheck) AddCheck(name string, checker Checker) (err error)

AddCheck adds a provided checker to the health check

func (*HealthCheck) GetStatus added in v1.1.1

func (hc *HealthCheck) GetStatus() string

GetStatus returns the current status in a thread-safe way

func (*HealthCheck) Handler

func (hc *HealthCheck) Handler(w http.ResponseWriter, req *http.Request)

Handler responds to an http request for the current health status

func (*HealthCheck) SetStatus added in v1.1.1

func (hc *HealthCheck) SetStatus(newStatus string) string

SetStatus sets the current status in a thread-safe way, returns the new status

func (*HealthCheck) Start

func (hc *HealthCheck) Start(ctx context.Context)

Start begins each ticker, this is used to run the health checks on dependent apps It also starts a go-routine to check the state after the criticalErrorTimeout has expired until the app is fully started, to make sure the state is updated accordingly without relying on the http Handle being called takes argument context and should utilise contextWithCancel Passing a nil context will cause errors during stop/app shutdown

func (*HealthCheck) Stop

func (hc *HealthCheck) Stop()

Stop will cancel all tickers and thus stop all health checks It also stops the go-routine that was created in start, if it is still alive.

func (*HealthCheck) Subscribe added in v1.2.0

func (hc *HealthCheck) Subscribe(s Subscriber, checks ...*Check)

Subscribe will subscribe the subscriber to the provided checks. This method may be called multiple times to subscribe to more checks and it is idempotent. The subscriber will be notified of the accumulated state of the subscribed Checks every time that a check changes its state. WARNING: A subscriber can be subscribed to multiple '*Check' structures, but a subscriber must be subscribed to only one instance of a '*HealthCheck'.

func (*HealthCheck) SubscribeAll added in v1.2.0

func (hc *HealthCheck) SubscribeAll(s Subscriber)

SubscribeAll will subscribe the subscriber to all the Checks that have been added. The subscriber will be notified of the global state every time that a check changes its state.

func (*HealthCheck) Unsubscribe added in v1.2.0

func (hc *HealthCheck) Unsubscribe(s Subscriber, checks ...*Check)

Unsubscribe removes the provided checks that will be used in order to determine the accumulated state for the provided subscriber

func (*HealthCheck) UnsubscribeAll added in v1.2.0

func (hc *HealthCheck) UnsubscribeAll(s Subscriber)

UnsubscribeAll stops further notifications of health updates to the provided subscriber

type Subscriber added in v1.2.0

type Subscriber interface {
	OnHealthUpdate(status string)
}

type VersionInfo

type VersionInfo struct {
	BuildTime       time.Time `json:"build_time"`
	GitCommit       string    `json:"git_commit"`
	Language        string    `json:"language"`
	LanguageVersion string    `json:"language_version"`
	Version         string    `json:"version"`
}

VersionInfo represents the version information of an app

func NewVersionInfo

func NewVersionInfo(buildTime, gitCommit, version string) (VersionInfo, error)

NewVersionInfo returns a health check version info object. Caller to provide: buildTime for when the app was built as a unix time stamp in string form gitCommit the SHA-1 commit hash of the built app version the semantic version of the built app

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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