Documentation ¶
Overview ¶
Package check standardizes /health and /ready endpoints. This allows you to easily know when your server is ready and healthy.
Index ¶
- type Check
- func (c *Check) AddHealthCheck(check Checker)
- func (c *Check) AddReadyCheck(check Checker)
- func (c *Check) CheckHealth(ctx context.Context) Response
- func (c *Check) CheckReady(ctx context.Context) Response
- func (c *Check) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (c *Check) SetPassthrough(h http.Handler)
- type Checker
- type CheckerFunc
- type NamedChecker
- type Response
- type Responses
- type Status
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Check ¶
type Check struct {
// contains filtered or unexported fields
}
Check wraps a map of service names to status checkers.
func NewCheck ¶
func NewCheck() *Check
NewCheck returns a Health with a default checker.
Example ¶
// Run the default healthcheck. it always return 200. It is good if you // have a service without any dependency h := NewCheck() h.CheckHealth(context.Background())
Output:
func (*Check) AddHealthCheck ¶
AddHealthCheck adds the check to the list of ready checks. If c is a NamedChecker, the name will be added.
func (*Check) AddReadyCheck ¶
AddReadyCheck adds the check to the list of ready checks. If c is a NamedChecker, the name will be added.
func (*Check) CheckHealth ¶
CheckHealth evaluates c's set of health checks and returns a populated Response.
Example ¶
h := NewCheck() h.AddHealthCheck(Named("google", CheckerFunc(func(ctx context.Context) Response { var r net.Resolver _, err := r.LookupHost(ctx, "google.com") if err != nil { return Error(err) } return Pass() }))) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() h.CheckHealth(ctx)
Output:
func (*Check) CheckReady ¶
CheckReady evaluates c's set of ready checks and returns a populated Response.
func (*Check) ServeHTTP ¶
func (c *Check) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves /ready and /health requests with the respective checks.
Example ¶
c := NewCheck() http.ListenAndServe(":6060", c)
Output:
func (*Check) SetPassthrough ¶
SetPassthrough allows you to set a handler to use if the request is not a ready or health check. This can be useful if you intend to use this as a middleware.
Example ¶
c := NewCheck() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello friends!")) }) c.SetPassthrough(http.DefaultServeMux) http.ListenAndServe(":6060", c)
Output:
type Checker ¶
Checker indicates a service whose health can be checked.
func ErrCheck ¶
ErrCheck will create a health checker that executes a function. If the function returns an error, it will return an unhealthy response. Otherwise, it will be as if the Ok function was called. Note: it is better to use CheckFunc, because with Check, the context is ignored.
func Named ¶
Named returns a Checker that will attach a name to the Response from the check. This way, it is possible to augment a Response with a human-readable name, but not have to encode that logic in the actual check itself.
func NamedFunc ¶
func NamedFunc(name string, fn CheckerFunc) Checker
NamedFunc is the same as Named except it takes a CheckerFunc.
type CheckerFunc ¶
CheckerFunc is an adapter of a plain func() error to the Checker interface.
type NamedChecker ¶
NamedChecker is a superset of Checker that also indicates the name of the service. Prefer to implement NamedChecker if your service has a fixed name, as opposed to calling *Health.AddNamed.
type Response ¶
type Response struct { Name string `json:"name"` Status Status `json:"status"` Message string `json:"message,omitempty"` Checks Responses `json:"checks,omitempty"` }
Response is a result of a collection of health checks.
type Responses ¶
type Responses []Response
Responses is a sortable collection of Response objects.