Files
health/README.md

57 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

# 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
}
```