Files
uptimesebi/internal/evaluate/evaluate.go
T
Sebastian UnterschützandClaude Opus 5.5 dc2e21f964
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m17s
ci / test (pull_request) Successful in 12m7s
ci / build (pull_request) Successful in 19m34s
ci / image (pull_request) Successful in 3m15s
feat: show unchecked monitors and tell about regions without checkers
A region whose checkers had all gone left its monitors looking up: the
console and status pages took the last result, however old, and nobody
was told.

- A region's last result counts only while it is younger than the
  evaluator's staleness (3 intervals, at least 2 minutes); older ones
  show as "no data since …" in the console and leave status page
  components unknown instead of operational.
- A monitor no region has reported for recently is marked "not being
  checked", counted on the dashboard with an explanation, and noted on
  its page.
- A region watch on every API replica emails the platform's admins
  (platform regions) or the organization's owners (own regions) once a
  region with checkers and enabled monitors has had no live checker for
  UPTIME_REGION_ALERT_AFTER (default 5m), and again when one is back.
  The dashboard names such regions at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-24 23:35:43 +01:00

138 lines
3.6 KiB
Go

// Package evaluate turns per-region check results into a monitor state.
//
// A monitor goes down only when every region it is assigned to has failed
// the configured number of times in a row and reported recently. A region
// without recent results counts as unknown and keeps the monitor up: one
// checker losing its network must not page anyone. Any successful region
// brings the monitor back up.
//
// The state lives in Redis and each observation is applied by one Lua
// script, so concurrent results on several API replicas produce exactly one
// transition.
package evaluate
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
// Transition is the change an observation caused.
type Transition int
// Transitions.
const (
None Transition = iota
Down
Up
)
func (t Transition) String() string {
switch t {
case Down:
return "down"
case Up:
return "up"
}
return "none"
}
// Observation is one check result with the monitor settings needed to judge
// it.
type Observation struct {
MonitorID uuid.UUID
Region string
OK bool
At time.Time
Interval time.Duration
Confirmations int
// Regions are all regions the monitor is assigned to.
Regions []string
}
// StaleAfter is how old a region's last result may be before the region
// counts as unknown.
func StaleAfter(interval time.Duration) time.Duration {
return max(3*interval, 2*time.Minute)
}
const stateTTL = 7 * 24 * time.Hour
// observeScript applies one result.
//
// KEYS: monitor state hash.
// ARGV: region, ok (1/0), at ms, confirmations, stale-before ms, ttl s,
// then every assigned region.
var observeScript = redis.NewScript(`
local key, region = KEYS[1], ARGV[1]
local ok, at, need, stale = ARGV[2] == '1', tonumber(ARGV[3]), tonumber(ARGV[4]), tonumber(ARGV[5])
local last = tonumber(redis.call('HGET', key, 'a:' .. region) or '0')
if at < last then return 'none' end
redis.call('HSET', key, 'a:' .. region, string.format('%.0f', at))
if ok then
redis.call('HSET', key, 'f:' .. region, 0)
else
redis.call('HINCRBY', key, 'f:' .. region, 1)
end
redis.call('EXPIRE', key, ARGV[6])
local state = redis.call('HGET', key, 'state') or 'up'
if state == 'down' then
if ok then
redis.call('HSET', key, 'state', 'up')
return 'up'
end
return 'none'
end
for i = 7, #ARGV do
local r = ARGV[i]
local fails = tonumber(redis.call('HGET', key, 'f:' .. r) or '0')
local seen = tonumber(redis.call('HGET', key, 'a:' .. r) or '0')
if fails < need or seen < stale then return 'none' end
end
redis.call('HSET', key, 'state', 'down')
return 'down'
`)
// Evaluator is safe for concurrent use.
type Evaluator struct {
rdb redis.UniversalClient
now func() time.Time
}
// New returns an Evaluator on rdb.
func New(rdb redis.UniversalClient) *Evaluator {
return &Evaluator{rdb: rdb, now: time.Now}
}
// Observe applies o and reports the resulting transition.
func (e *Evaluator) Observe(ctx context.Context, o Observation) (Transition, error) {
ok := "0"
if o.OK {
ok = "1"
}
stale := e.now().Add(-StaleAfter(o.Interval)).UnixMilli()
args := []any{o.Region, ok, o.At.UnixMilli(), max(o.Confirmations, 1), stale, int(stateTTL.Seconds())}
for _, r := range o.Regions {
args = append(args, r)
}
res, err := observeScript.Run(ctx, e.rdb, []string{"eval:" + o.MonitorID.String()}, args...).Text()
if err != nil {
return None, fmt.Errorf("evaluate: %w", err)
}
switch res {
case "down":
return Down, nil
case "up":
return Up, nil
}
return None, nil
}
// Forget drops the state of a deleted monitor.
func (e *Evaluator) Forget(ctx context.Context, id uuid.UUID) error {
return e.rdb.Del(ctx, "eval:"+id.String()).Err()
}