Documentation
¶
Overview ¶
Package healthcheck probes registered database connections and collects Go runtime diagnostics, returning a structured snapshot suitable for a health-check endpoint.
Creating the service ¶
svc := healthcheck.New([]healthcheck.DBEntry{
{Name: "primary", Driver: "postgres", DB: primaryDB},
{Name: "replica", Driver: "postgres", DB: replicaDB},
})
Full report ¶
Report probes all registered connections concurrently and returns runtime metrics alongside per-connection pool statistics.
report := svc.Report(ctx)
Single connection probe ¶
dbReport, err := svc.CheckDB(ctx, "primary")
Dynamic registration ¶
Connections can be added or removed at any time without restarting the service.
svc.AddDB("cache", "redis", cacheDB)
svc.RemoveDB("replica")
HTTP handler example ¶
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
report := svc.Report(r.Context())
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(report)
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Check ¶
type Check struct {
// contains filtered or unexported fields
}
Check probes registered database connections and collects Go runtime diagnostics.
func (*Check) AddDB ¶
AddDB registers or replaces a named connection. driver is an optional label reported in diagnostics.
func (*Check) CheckDB ¶
CheckDB probes a single connection by name. Returns an error if the name is not registered.
func (*Check) Report ¶
func (s *Check) Report(ctx context.Context) HealthReport
Report returns a full health snapshot: runtime info plus all DB probes executed concurrently.
func (*Check) SetPingTimeout ¶
SetPingTimeout overrides the per-connection ping timeout. A non-positive value resets it to the default.
type DBEntry ¶
DBEntry holds a named database connection to be monitored. Driver is an optional human-readable label (e.g. "postgres", "mysql") reported back in the diagnostics; it is purely informational.
type DBReport ¶
type DBReport struct {
Status string `json:"status"`
Driver string `json:"driver,omitempty"`
Error string `json:"error,omitempty"`
OpenConnections int `json:"open_connections"`
InUse int `json:"in_use"`
Idle int `json:"idle"`
WaitCount int64 `json:"wait_count"`
WaitDuration time.Duration `json:"wait_duration"`
}
DBReport contains non-sensitive diagnostics for a single database connection.
type HealthReport ¶
type HealthReport struct {
Runtime RuntimeReport `json:"runtime"`
Databases map[string]DBReport `json:"databases"`
}
HealthReport is the full snapshot returned by the service.
type Pinger ¶
Pinger is the minimal database surface the health check needs. It is satisfied by *sql.DB out of the box, but any type implementing it (such as a test double) can be registered.
type RuntimeReport ¶
type RuntimeReport struct {
OS string `json:"os"`
Arch string `json:"arch"`
NumCPU int `json:"num_cpu"`
NumGoroutine int `json:"num_goroutine"`
Uptime string `json:"uptime"`
// Memory (all values in bytes)
MemAllocated uint64 `json:"mem_allocated"` // heap currently allocated
MemTotalAlloc uint64 `json:"mem_total_alloc"` // cumulative heap allocated
MemSys uint64 `json:"mem_sys"` // total memory from OS
MemNumGC uint32 `json:"mem_num_gc"` // number of GC cycles completed
MemLastGC string `json:"mem_last_gc"` // time of last GC
}
RuntimeReport contains non-sensitive Go runtime diagnostics.