ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m3s
ci / lint (pull_request) Successful in 10m9s
ci / build (pull_request) Successful in 9m54s
ci / image (pull_request) Successful in 3m16s
A monitor went down only when every assigned region had failed and reported recently, so one region whose checkers were gone kept a monitor up for good, even while all other regions reported its target failing. Regions without recent results are now left out: a monitor goes down when every region that reported recently has failed. One region failing while another succeeds still does not alert, and with every region silent nothing is judged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
// Package evaluate turns per-region check results into a monitor state.
|
|
//
|
|
// A monitor goes down when every region it is assigned to that reported
|
|
// recently has failed the configured number of times in a row: one region
|
|
// failing while another succeeds is the checker's problem, not the
|
|
// target's. A region without recent results counts as unknown and is left
|
|
// out, so a dead region neither keeps a monitor up nor takes it down; with
|
|
// every region unknown nothing is judged at all. 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
|
|
local reporting = 0
|
|
for i = 7, #ARGV do
|
|
local r = ARGV[i]
|
|
local seen = tonumber(redis.call('HGET', key, 'a:' .. r) or '0')
|
|
if seen >= stale then
|
|
local fails = tonumber(redis.call('HGET', key, 'f:' .. r) or '0')
|
|
if fails < need then return 'none' end
|
|
reporting = reporting + 1
|
|
end
|
|
end
|
|
if reporting == 0 then return 'none' 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()
|
|
}
|