Files
archie/internal/app/statestore/alerts.go
T
sam caf560bdfa Feat/operator alerts (#1635)
* feat(state): durable operator alerts owned by the State Store

Alerts dedup on org+kind+subject, resolve themselves when a condition
clears, and are raised, resolved, dismissed and listed through the state
contract. Task transitions raise needs-human and failed alerts in the same
transaction; the store checks service health and prunes closed alerts.
Each raise and resolve queues a webhook delivery whose attempts are kept.

* feat(notify): deliver operator alerts to the webhook; raise config and release alerts

notify.webhook now receives raised and resolved alerts above a severity
threshold with HMAC-signed payloads, bounded retry and every attempt recorded
on the alert. archied raises model alias, provider and credential-binding
alerts from the effective configuration, an update alert from the release
channel, and a credential-rejected alert from the egress resolver. The
dashboard API lists and dismisses the caller's org's alerts.

* feat(ui): alert bell in the top bar and webhook alert settings

The bell lists the org's open alerts by severity with dismiss and a link per
alert, and refreshes from the event stream. The task settings page gains the
webhook severity threshold and signing secret. ui/dist rebuilt.
2026-10-12 03:30:46 +11:00

128 lines
3.9 KiB
Go

package statestore
import (
"context"
"fmt"
"slices"
"time"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
)
const (
// alertSweepInterval is how often the store checks service health and
// prunes closed alerts.
alertSweepInterval = time.Minute
// alertRetention is how long a resolved or dismissed alert is kept.
alertRetention = 30 * 24 * time.Hour
// serviceGrace is how long a service may be unhealthy before it is worth
// an operator's attention: restarts and updates pass through it.
serviceGrace = 5 * time.Minute
)
// serviceWatch remembers since when each service has been unhealthy.
type serviceWatch struct {
since map[string]time.Time
}
// unhealthy returns the services that have been down or degraded for longer
// than serviceGrace, and forgets those that recovered. A service that never
// reported is absent rather than unhealthy and is left out.
func (w *serviceWatch) unhealthy(mesh []presence.Service, now time.Time) []string {
if w.since == nil {
w.since = map[string]time.Time{}
}
var out []string
for _, service := range mesh {
if !slices.Contains(presence.Services(), service.Service) || service.ReportedAt.IsZero() {
continue
}
if service.State == presence.StateUp {
delete(w.since, service.Service)
continue
}
since, ok := w.since[service.Service]
if !ok {
since = now
w.since[service.Service] = since
}
if now.Sub(since) >= serviceGrace {
out = append(out, service.Service)
}
}
return out
}
func serviceAlert(name string) alert.Input {
return alert.Input{
Org: org.DefaultOrgID, Kind: alert.KindServiceUnhealthy, Subject: "service:" + name,
Severity: alert.SeverityWarning, Condition: true,
Title: name + " is unhealthy",
Body: fmt.Sprintf("%s has not been healthy for over %d minutes.", name, int(serviceGrace.Minutes())),
Link: "/settings/status",
}
}
// startSweeps starts the store's periodic work: auto-updating packages, and
// checking service health and pruning alerts.
func (b *server) startSweeps(ctx context.Context, deps staterpc.Deps) {
go b.autoUpdatePackages(ctx, deps.Packages)
if deps.Alerts != nil {
go b.sweepAlerts(ctx, deps.Alerts)
}
}
// sweepAlerts raises and clears service health alerts and prunes closed
// alerts, until ctx ends. Service health has no transition to hang an alert
// on, so it is checked here, next to the presence records it reads.
func (b *server) sweepAlerts(ctx context.Context, store alert.Store) {
pruner, _ := b.st.(alert.Pruner)
records, _ := b.st.(interface {
ListPresence(context.Context) ([]storecontract.Presence, error)
})
reporter := alert.Reporter{Raiser: store, Log: b.log, By: "state-store"}
var watch serviceWatch
ticker := time.NewTicker(alertSweepInterval)
defer ticker.Stop()
for {
if records != nil {
b.checkServices(ctx, reporter, records, &watch)
}
if pruner != nil {
if _, err := pruner.PruneAlerts(ctx, time.Now().Add(-alertRetention)); err != nil && ctx.Err() == nil {
b.log.Warn("prune alerts", "err", err)
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
func (b *server) checkServices(ctx context.Context, reporter alert.Reporter, records interface {
ListPresence(context.Context) ([]storecontract.Presence, error)
}, watch *serviceWatch,
) {
list, err := records.ListPresence(ctx)
if err != nil {
if ctx.Err() == nil {
b.log.Warn("service health not checked", "err", err)
}
return
}
now := time.Now()
mesh := presence.Mesh(list, now)
down := watch.unhealthy(mesh, now)
for _, service := range mesh {
if slices.Contains(presence.Services(), service.Service) {
reporter.Sync(ctx, slices.Contains(down, service.Service), serviceAlert(service.Service))
}
}
}