Documentation
¶
Overview ¶
Package health provides health check endpoint builders with dependency checking, timeouts, and standard response formats. Designed for Kubernetes liveness/readiness probes and load balancer health checks.
Usage:
h := health.NewChecker(health.WithTimeout(3 * time.Second))
h.AddCheck("postgres", func(ctx context.Context) error {
return db.PingContext(ctx)
})
h.AddNonCriticalCheck("redis", func(ctx context.Context) error {
return rdb.Ping(ctx).Err()
})
r.Get("/health", h.Handler())
r.Get("/health/live", h.LiveHandler())
Index ¶
- Constants
- type CheckFunc
- type CheckResult
- type Checker
- func (c *Checker) AddCheck(name string, fn CheckFunc)
- func (c *Checker) AddNonCriticalCheck(name string, fn CheckFunc)
- func (c *Checker) Check(ctx context.Context) Response
- func (c *Checker) Handler() func(http.ResponseWriter, *http.Request) error
- func (c *Checker) LiveHandler() func(http.ResponseWriter, *http.Request) error
- type Option
- type Response
Constants ¶
const ( StatusHealthy = "healthy" StatusDegraded = "degraded" StatusUnhealthy = "unhealthy" )
Status constants.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckResult ¶
type CheckResult struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
Duration int64 `json:"duration_ms"`
}
CheckResult is the result of a single named check.
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker runs health checks and exposes HTTP handlers.
func NewChecker ¶
NewChecker creates a new health Checker with the given options.
func (*Checker) AddCheck ¶
AddCheck registers a critical health check. If it fails the overall status is "unhealthy" and the HTTP handler returns 503.
func (*Checker) AddNonCriticalCheck ¶
AddNonCriticalCheck registers a non-critical health check. If it fails the overall status is "degraded" but the HTTP handler still returns 200.
func (*Checker) Check ¶
Check runs all registered checks concurrently and returns the aggregated result.
func (*Checker) Handler ¶
Handler returns an HTTP handler that runs all health checks and responds with 200 (healthy/degraded) or 503 (unhealthy). Compatible with router.HandlerFunc (returns error).
func (*Checker) LiveHandler ¶
LiveHandler returns an HTTP handler that always responds with 200. Use this for Kubernetes liveness probes to confirm the process is running.
type Option ¶
type Option func(*Checker)
Option configures a Checker.
func WithTimeout ¶
WithTimeout sets the per-check timeout. Default is 5 seconds.