check / check (push) Waiting to run
The health check answered ok while every DNS lookup failed, as on a network that intercepts DNS queries. A lookup whose root servers give no usable reply now fails with ErrNoRootServerAnswered. At the end of each round of DNS checks that shutdown did not cut short, the watcher records whether every lookup failed with it or with ErrIntercepted; the health check then gives status error, and /.well-known/healthcheck and /health answer 503. Until the first round finishes it stays ok. The health check now depends on the watcher, so fx starts the watcher before the HTTP server and stops the server first. NewWithRootServers lets tests start a resolver's walks at real servers that are not root servers. Model: opus-5-5
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
// Package healthcheck provides application health status.
|
|
package healthcheck
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"sneak.berlin/go/dnswatcher/internal/config"
|
|
"sneak.berlin/go/dnswatcher/internal/globals"
|
|
"sneak.berlin/go/dnswatcher/internal/logger"
|
|
"sneak.berlin/go/dnswatcher/internal/watcher"
|
|
)
|
|
|
|
// Values of Response.Status.
|
|
const (
|
|
StatusOK = "ok"
|
|
StatusError = "error"
|
|
)
|
|
|
|
// Params contains dependencies for Healthcheck.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Globals *globals.Globals
|
|
Config *config.Config
|
|
Logger *logger.Logger
|
|
Watcher *watcher.Watcher
|
|
}
|
|
|
|
// Healthcheck provides health status information.
|
|
type Healthcheck struct {
|
|
StartupTime time.Time
|
|
log *slog.Logger
|
|
params *Params
|
|
}
|
|
|
|
// Response is the health check response structure.
|
|
type Response struct {
|
|
Status string `json:"status"`
|
|
Now string `json:"now"`
|
|
UptimeSeconds int64 `json:"uptimeSeconds"`
|
|
UptimeHuman string `json:"uptimeHuman"`
|
|
Version string `json:"version"`
|
|
Appname string `json:"appname"`
|
|
Maintenance bool `json:"maintenanceMode"`
|
|
}
|
|
|
|
// New creates a new Healthcheck instance.
|
|
func New(
|
|
lifecycle fx.Lifecycle,
|
|
params Params,
|
|
) (*Healthcheck, error) {
|
|
healthcheck := &Healthcheck{
|
|
log: params.Logger.Get(),
|
|
params: ¶ms,
|
|
}
|
|
|
|
lifecycle.Append(fx.Hook{
|
|
OnStart: func(_ context.Context) error {
|
|
healthcheck.StartupTime = time.Now()
|
|
|
|
return nil
|
|
},
|
|
})
|
|
|
|
return healthcheck, nil
|
|
}
|
|
|
|
// Check returns the current health status. Status is StatusError when
|
|
// the watcher's most recent round of DNS checks could not reach the DNS
|
|
// (see watcher.Watcher.DNSUnreachable), and StatusOK otherwise.
|
|
func (h *Healthcheck) Check() *Response {
|
|
status := StatusOK
|
|
if h.params.Watcher.DNSUnreachable() {
|
|
status = StatusError
|
|
}
|
|
|
|
return &Response{
|
|
Status: status,
|
|
Now: time.Now().UTC().Format(time.RFC3339Nano),
|
|
UptimeSeconds: int64(h.uptime().Seconds()),
|
|
UptimeHuman: h.uptime().String(),
|
|
Appname: h.params.Globals.Appname,
|
|
Version: h.params.Globals.Version,
|
|
Maintenance: h.params.Config.MaintenanceMode,
|
|
}
|
|
}
|
|
|
|
func (h *Healthcheck) uptime() time.Duration {
|
|
return time.Since(h.StartupTime)
|
|
}
|