HTTP health check handler with parallel goroutine-per-check execution, 5 s request-derived timeout, and two-level criticality (LevelCritical → 503, LevelDegraded → 200). What's included: - `Checkable` interface (HealthCheck / Name / Priority) and `Level` type with LevelCritical and LevelDegraded constants - `NewHandler(logger, checks...)` returning http.Handler; runs all checks concurrently via buffered channel, returns JSON with per-component status and latency - `ComponentStatus` and `Response` types for the JSON response body Tested-via: todo-api POC integration Reviewed-against: docs/adr/
57 lines
1.3 KiB
Markdown
57 lines
1.3 KiB
Markdown
# health
|
|
|
|
Stdlib `http.Handler` for service health checks. Runs all checks concurrently and returns a JSON summary.
|
|
|
|
## Install
|
|
|
|
```
|
|
go get code.nochebuena.dev/go/health
|
|
```
|
|
|
|
## Usage
|
|
|
|
```go
|
|
handler := health.NewHandler(logger, db, cache, queue)
|
|
r.Get("/health", handler)
|
|
```
|
|
|
|
### Response
|
|
|
|
```json
|
|
{
|
|
"status": "UP",
|
|
"components": {
|
|
"db": {"status": "UP", "latency": "1.2ms"},
|
|
"cache": {"status": "DOWN", "latency": "5ms", "error": "connection refused"}
|
|
}
|
|
}
|
|
```
|
|
|
|
| Overall status | HTTP code | Condition |
|
|
|---|---|---|
|
|
| `UP` | 200 | All checks pass |
|
|
| `DEGRADED` | 200 | One or more `LevelDegraded` checks fail; no critical failures |
|
|
| `DOWN` | 503 | One or more `LevelCritical` checks fail |
|
|
|
|
## Implementing `Checkable`
|
|
|
|
```go
|
|
func (d *DB) HealthCheck(ctx context.Context) error { return d.pool.PingContext(ctx) }
|
|
func (d *DB) Name() string { return "postgres" }
|
|
func (d *DB) Priority() health.Level { return health.LevelCritical }
|
|
```
|
|
|
|
## Logger
|
|
|
|
`health.Logger` is a duck-typed interface satisfied by `logz.Logger` without importing it.
|
|
|
|
```go
|
|
type Logger interface {
|
|
Debug(msg string, args ...any)
|
|
Info(msg string, args ...any)
|
|
Warn(msg string, args ...any)
|
|
Error(msg string, err error, args ...any)
|
|
WithContext(ctx context.Context) Logger
|
|
}
|
|
```
|