README

Health Checks

  • Makes it possible to add a health check for a service (e.g. postgres). The list of checks is checked for the routes /health and /health/check

  • /health all required and registered health checks healthy: Status code 200, Content-Type: text/plain, body: "OK"

    Any required and registered health check is not healthy: status code 503, Content-Type: text/plain, body: "ERR"

  • /health/check returns a table with the results of all registered health checks

Example:

    Required Services: 
    
    postgresdefault        ERR   any error message
    
    redis                  OK    
    
    Optional Services:  
    anotherTestName        WARN  any warning message

  • Each check has to implement the HealthCheck interface and has to be registered

    • health checks are registered as required or optional
    • servicehealthcheck.RegisterHealthCheck(hc HealthCheck, name string) registers a health check with an unique name as required check
    • servicehealthcheck.RegisterOptionalHealthCheck(hc HealthCheck, name string) registers a health check with a unique name as optional check
    • a optional check is not checked when /health is called
    • a required check is always checked
  • To change a registered health check from required to optional or vice versa the health check has to be removed and registered again. Remove a health check with servicehealthcheck.RemoveHealthCheck(name)

Implement a HealthCheck

  • The result of each health check is not NOT cached. The implementation of each health check can use ConnectionState for caching

  • The HealthCheck.HealthCheck() returns OK, WARN or ERR and a detailed message

    • /health => OK and WARN means the service is healthy.
    • /health/check => the complete result of the check is added to the response

Environment Variables

HEALTH_CHECK_INIT_RESULT_ERROR_TTL : Amount of time to cache the errors that occur in the initialisation of the HealthCheck

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthHandler

func HealthHandler() http.Handler

    HealthHandler returns the health endpoint for transactional processing. This Handler only checks the required health checks and returns ERR and 503 or OK and 200.

    func JSONHealthHandler

    func JSONHealthHandler() http.Handler

      JSONHealthHandler return health endpoint with all details about service health. This handler checks all health checks. The response body contains a JSON formatted array with every service (required or optional) and the detailed health checks about them.

      func ReadableHealthHandler

      func ReadableHealthHandler() http.Handler

        ReadableHealthHandler returns the health endpoint with all details about service health. This handler checks all health checks. The response body contains two tables (for required and optional health checks) with the detailed results of the health checks.

        func RegisterHealthCheck

        func RegisterHealthCheck(name string, hc HealthCheck)

          RegisterHealthCheck registers a required HealthCheck. The name must be unique. If the health check satisfies the Initializable interface, it is initialized before it is added. It is not possible to add a health check with the same name twice, even if one is required and one is optional

          func RegisterHealthCheckFunc

          func RegisterHealthCheckFunc(name string, f HealthCheckFunc)

            RegisterHealthCheckFunc registers a required HealthCheck. The name must be unique. It is not possible to add a health check with the same name twice, even if one is required and one is optional

            func RegisterOptionalHealthCheck

            func RegisterOptionalHealthCheck(hc HealthCheck, name string)

              RegisterOptionalHealthCheck registers a HealthCheck like RegisterHealthCheck(hc HealthCheck, name string) but the health check is only checked for /health/check and not for /health/

              Types

              type ConnectionState

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

                ConnectionState caches the result of health checks. It is concurrency-safe.

                func (*ConnectionState) GetState

                func (cs *ConnectionState) GetState() HealthCheckResult

                  GetState returns the current state. That is whether the check is healthy or the error occurred.

                  func (*ConnectionState) LastChecked

                  func (cs *ConnectionState) LastChecked() time.Time

                    LastChecked returns the time that the state was last updated or confirmed.

                    func (*ConnectionState) SetErrorState

                    func (cs *ConnectionState) SetErrorState(err error)

                      SetErrorState sets the state to not healthy.

                      func (*ConnectionState) SetHealthy

                      func (cs *ConnectionState) SetHealthy()

                        SetHealthy sets the state to healthy.

                        type HealthCheck

                        type HealthCheck interface {
                        	HealthCheck(ctx context.Context) HealthCheckResult
                        }

                          HealthCheck is a health check that is registered once and that is performed periodically and/or spontaneously.

                          type HealthCheckFunc

                          type HealthCheckFunc func(ctx context.Context) HealthCheckResult

                          func (HealthCheckFunc) HealthCheck

                          func (hcf HealthCheckFunc) HealthCheck(ctx context.Context) HealthCheckResult

                          type HealthCheckResult

                          type HealthCheckResult struct {
                          	State HealthState
                          	Msg   string
                          }

                            HealthCheckResult describes the result of a health check, contains the state of a service and a message that describes the state. If the state is Ok the description can be empty. The description should contain the error message if any error or warning occurred during the health check.

                            type HealthState

                            type HealthState string

                              HealthState describes if a any error or warning occurred during the health check of a service

                              const (
                              	// Err State of a service, if an error occurred during the health check of the service
                              	Err HealthState = "ERR"
                              	// Warn State of a service, if a warning occurred during the health check of the service
                              	Warn HealthState = "WARN"
                              	// Ok State of a service, if no warning or error occurred during the health check of the service
                              	Ok HealthState = "OK"
                              )

                              type Initializable

                              type Initializable interface {
                              	Init(ctx context.Context) error
                              }

                                Initializable is used to mark that a health check needs to be initialized