A simple and flexible Go library that provides customizable health check endpoints to monitor application and dependency availability.
A simple and flexible health check library for Go.
This tool is used by Go developers to implement health check endpoints in their applications, enabling infrastructure and cloud platforms like Kubernetes to monitor service availability and readiness. It helps DevOps and SecOps teams automate health monitoring by aggregating the status of multiple dependencies and providing detailed health information.
Best practice is to configure appropriate timeouts and caching durations to balance responsiveness and resource usage. Periodic checks reduce overhead by avoiding execution on every HTTP request. The library integrates well with Kubernetes probes and other cloud infrastructure monitoring tools.
Ensure Go is installed on your system
Import the library in your Go project using: github.com/alexliesenfeld/health
Use `go get github.com/alexliesenfeld/health` to add the library to your module dependencies
health.NewChecker(health.WithCacheDuration(1*time.Second), health.WithTimeout(10*time.Second), health.WithCheck(health.Check{Name: "database", Timeout: 2*time.Second, Check: db.PingContext}), health.WithPeriodicCheck(15*time.Second, 3*time.Second, health.Check{Name: "search", Check: func(ctx context.Context) error { return fmt.Errorf("this makes the check fail") }}), health.WithStatusListener(func(ctx context.Context, state health.CheckerState) { log.Println(...) }))
Creates a new health checker with caching, global timeout, synchronous and periodic checks, and a status listener.
http.Handle("/health", checker)
Registers the health checker as an HTTP handler to serve health check responses.