Documentation
¶
Overview ¶
Package health provides a composable health-check mechanism.
Each infrastructure component (postgres, redis, …) supplies a Checker that reports its own name and reachability. Check runs all checkers and returns their combined results, suitable for a /health HTTP endpoint.
Basic usage:
router.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, health.Check(
c.Request.Context(),
postgres.HealthChecker(),
))
})
Index ¶
Examples ¶
Constants ¶
const StatusHealthy = 1
StatusHealthy is the status value returned when a component is reachable.
const StatusUnhealthy = 0
StatusUnhealthy is the status value returned when a component is unreachable.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Checker ¶
Checker is a function that returns the health status of one component. The Checker owns both its name and status; callers compose multiple checkers via Check without knowledge of individual component names.
type Result ¶
Result holds the health status of a single named component.
func Check ¶
Check runs each checker in registration order and returns their combined results. An empty checkers list returns an empty (non-nil) slice.
Example ¶
ExampleCheck shows how to compose multiple health checkers for a /health endpoint. Each checker reports its own component name and status independently.
package main
import (
"context"
"fmt"
"github.com/phcp-tech/common-library-golang/health"
)
func main() {
mockDB := func(ctx context.Context) health.Result {
return health.Result{Name: "postgres", Status: health.StatusHealthy}
}
mockRedis := func(ctx context.Context) health.Result {
return health.Result{Name: "redis", Status: health.StatusHealthy}
}
results := health.Check(context.Background(), mockDB, mockRedis)
for _, r := range results {
fmt.Printf("%s: %d\n", r.Name, r.Status)
}
}
Output: postgres: 1 redis: 1