Files
archie/modules/system/health.go
T

182 lines
5.7 KiB
Go

package system
import (
"context"
"net/http"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/internal/buildinfo"
"github.com/samcharles93/archie-core/internal/platform/health"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/runs"
)
// healthTimeout bounds the checks, so a dependency that hangs shows as
// unhealthy instead of stalling the report.
const healthTimeout = 5 * time.Second
// Sources are the facts the health report is assembled from. Each comes from
// the module that owns it, and the composition root supplies them. A source
// that is nil, or that fails, is reported as unhealthy.
type Sources struct {
// Checks runs the readiness checks, one per module or dependency.
Checks func(context.Context) []health.Result
// Connectors is the state of each accepted package.
Connectors func() []connectors.Health
// Runner is the state of the run workers.
Runner func() runs.RunnerHealth
// Waiting counts what waits on a person.
Waiting func(context.Context) (Waiting, error)
// LastFailedRun is the most recent run that failed; nil when none has.
LastFailedRun func(context.Context) (*runs.RunView, error)
}
// UseHealth sets where the health report reads from.
func (m *Module) UseHealth(s Sources) {
m.mu.Lock()
defer m.mu.Unlock()
m.sources = s
}
// Waiting is what waits on a person.
type Waiting struct {
Approvals int `json:"approvals" doc:"Approval requests waiting for an answer"`
// Runs are in the states that need work or a person, by state.
Runs runs.RunCounts `json:"runs"`
}
// Check is one module or dependency's readiness.
type Check struct {
Name string `json:"name"`
Healthy bool `json:"healthy"`
Error string `json:"error,omitzero"`
}
// Runner is the state of the run workers.
type Runner struct {
Healthy bool `json:"healthy"`
runs.RunnerHealth
}
// Failure is the most recent thing that went wrong and why.
type Failure struct {
Source string `json:"source" doc:"A connector name, the runner, or a workflow"`
Cause string `json:"cause"`
At time.Time `json:"at"`
// RunID is set when the failure is a run.
RunID string `json:"run_id,omitzero"`
}
// Health is the binary's state at the time it was read.
type Health struct {
Version string `json:"version"`
Healthy bool `json:"healthy"`
CheckedAt time.Time `json:"checked_at"`
Checks []Check `json:"checks"`
Connectors []connectors.Health `json:"connectors"`
Runner Runner `json:"runner"`
// Waiting is absent when it could not be read; that is a failed check.
Waiting *Waiting `json:"waiting,omitzero"`
LastFailure *Failure `json:"last_failure,omitzero"`
}
func (m *Module) registerHealth(api huma.API) {
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindInstance), huma.Operation{
OperationID: "get-system-health", Method: http.MethodGet, Path: "/api/system/health",
Summary: "Report whether each module, connector and the runner is working, and what waits on a person", Tags: []string{"System"},
}, m.getHealth)
}
// getHealth never fails because a part is down: a failing part is reported as
// unhealthy with its cause.
func (m *Module) getHealth(ctx context.Context, _ *struct{}) (*httpapi.Reply[Health], error) {
m.mu.RLock()
src := m.sources
m.mu.RUnlock()
ctx, cancel := context.WithTimeout(ctx, healthTimeout)
defer cancel()
h := Health{Version: buildinfo.Version, CheckedAt: time.Now().UTC(), Checks: []Check{}, Connectors: []connectors.Health{}}
if src.Checks != nil {
for _, r := range src.Checks(ctx) {
h.Checks = append(h.Checks, Check{Name: r.Name, Healthy: r.Error == "", Error: r.Error})
}
}
if src.Connectors != nil {
h.Connectors = src.Connectors()
}
h.Runner = Runner{Healthy: true}
if src.Runner != nil {
h.Runner.RunnerHealth = src.Runner()
h.Runner.Healthy = h.Runner.Error == ""
}
if src.Waiting != nil {
w, err := src.Waiting(ctx)
if err != nil {
m.log.ErrorContext(ctx, "count what waits on a person", "error", err)
h.Checks = append(h.Checks, Check{Name: "waiting", Error: "couldn't count approvals and runs: " + err.Error()})
} else {
h.Waiting = &w
}
}
var lastRun *runs.RunView
if src.LastFailedRun != nil {
var err error
if lastRun, err = src.LastFailedRun(ctx); err != nil {
m.log.ErrorContext(ctx, "read the last failed run", "error", err)
h.Checks = append(h.Checks, Check{Name: "runs", Error: "couldn't read recent runs: " + err.Error()})
}
}
h.Healthy = healthy(h)
h.LastFailure = lastFailure(h, lastRun)
return httpapi.OK(h), nil
}
func healthy(h Health) bool {
if !h.Runner.Healthy {
return false
}
for _, c := range h.Checks {
if !c.Healthy {
return false
}
}
for _, c := range h.Connectors {
if !c.Healthy {
return false
}
}
return true
}
// lastFailure picks the newest of the failures with a time: a connector that
// failed to start, the runner's last claim error, and the last failed run.
func lastFailure(h Health, run *runs.RunView) *Failure {
var out *Failure
consider := func(f Failure) {
if out == nil || f.At.After(out.At) {
out = &f
}
}
for _, c := range h.Connectors {
if !c.Healthy && c.FailedAt != nil {
consider(Failure{Source: c.Name, Cause: c.Error, At: *c.FailedAt})
}
}
if h.Runner.ErrorAt != nil {
consider(Failure{Source: "runner", Cause: h.Runner.Error, At: *h.Runner.ErrorAt})
}
if run != nil {
at := run.CreatedAt
if run.FinishedAt != nil {
at = *run.FinishedAt
}
consider(Failure{Source: run.Workflow, Cause: run.Error, At: at, RunID: run.ID.String()})
}
return out
}