health

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
const (
	StatusHealthy   = "healthy"
	StatusDegraded  = "degraded"
	StatusUnhealthy = "unhealthy"
)

Status constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckFunc

type CheckFunc func(ctx context.Context) error

CheckFunc is a health check function. Return nil = healthy, error = unhealthy.

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

func NewChecker(opts ...Option) *Checker

NewChecker creates a new health Checker with the given options.

func (*Checker) AddCheck

func (c *Checker) AddCheck(name string, fn CheckFunc)

AddCheck registers a critical health check. If it fails the overall status is "unhealthy" and the HTTP handler returns 503.

func (*Checker) AddNonCriticalCheck

func (c *Checker) AddNonCriticalCheck(name string, fn CheckFunc)

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

func (c *Checker) Check(ctx context.Context) Response

Check runs all registered checks concurrently and returns the aggregated result.

func (*Checker) Handler

func (c *Checker) Handler() func(http.ResponseWriter, *http.Request) error

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

func (c *Checker) LiveHandler() func(http.ResponseWriter, *http.Request) error

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

func WithTimeout(d time.Duration) Option

WithTimeout sets the per-check timeout. Default is 5 seconds.

type Response

type Response struct {
	Status    string                 `json:"status"`
	Checks    map[string]CheckResult `json:"checks,omitempty"`
	Timestamp int64                  `json:"timestamp"`
}

Response is the full health check response.

Jump to

Keyboard shortcuts

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