feat(evaluate): alert only when every region of a monitor fails

Regions without recent results count as unknown and keep the monitor up.
State changes are decided by one Lua script so concurrent results on
several API replicas yield a single transition.

Refs #14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 01:23:37 +01:00
1 parent ffef63e586
commit de7c6fc842
2 files changed
+258

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
package evaluate
import (
"context"
"os"
"testing"
"time"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
func newTest(t *testing.T) (*Evaluator, *time.Time) {
t.Helper()
url := os.Getenv("UPTIME_TEST_REDIS_URL")
if url == "" {
t.Skip("UPTIME_TEST_REDIS_URL not set")
}
opts, err := redis.ParseURL(url)
if err != nil {
t.Fatal(err)
}
rdb := redis.NewClient(opts)
t.Cleanup(func() { _ = rdb.Close() })
now := time.UnixMilli(1_800_000_000_000)
e := New(rdb)
e.now = func() time.Time { return now }
return e, &now
}
type step struct {
region string
ok bool
want Transition
}
func play(t *testing.T, e *Evaluator, now *time.Time, regions []string, confirmations int, steps []step) {
t.Helper()
id := uuid.New()
for i, s := range steps {
*now = now.Add(time.Second)
got, err := e.Observe(context.Background(), Observation{
MonitorID: id, Region: s.region, OK: s.ok, At: *now,
Interval: time.Minute, Confirmations: confirmations, Regions: regions,
})
if err != nil {
t.Fatal(err)
}
if got != s.want {
t.Errorf("step %d (%s ok=%t) = %v, want %v", i, s.region, s.ok, got, s.want)
}
}
}
func TestDownOnlyWhenAllRegionsFail(t *testing.T) {
e, now := newTest(t)
play(t, e, now, []string{"eu", "us"}, 2, []step{
{"eu", false, None},
{"eu", false, None}, // eu confirmed down, us has not reported
{"us", true, None},
{"us", false, None},
{"eu", false, None},
{"us", false, Down}, // both regions failed twice in a row
{"us", false, None}, // no repeated transition
{"eu", true, Up}, // one healthy region is enough to recover
{"eu", true, None},
})
}
func TestSingleRegionWithConfirmations(t *testing.T) {
e, now := newTest(t)
play(t, e, now, []string{"eu"}, 3, []step{
{"eu", false, None},
{"eu", false, None},
{"eu", true, None}, // a success resets the count
{"eu", false, None},
{"eu", false, None},
{"eu", false, Down},
})
}
func TestSilentRegionBlocksAlert(t *testing.T) {
e, now := newTest(t)
id := uuid.New()
obs := func(region string, ok bool) Transition {
got, err := e.Observe(context.Background(), Observation{
MonitorID: id, Region: region, OK: ok, At: *now,
Interval: time.Minute, Confirmations: 1, Regions: []string{"eu", "us"},
})
if err != nil {
t.Fatal(err)
}
return got
}
obs("us", false)
// us stops reporting for longer than three intervals.
*now = now.Add(5 * time.Minute)
if got := obs("eu", false); got != None {
t.Errorf("eu failure with us silent = %v, want none", got)
}
}
func TestOutOfOrderResultIgnored(t *testing.T) {
e, now := newTest(t)
id := uuid.New()
obs := func(at time.Time, ok bool) Transition {
got, err := e.Observe(context.Background(), Observation{
MonitorID: id, Region: "eu", OK: ok, At: at, Interval: time.Minute, Confirmations: 1, Regions: []string{"eu"},
})
if err != nil {
t.Fatal(err)
}
return got
}
if got := obs(*now, false); got != Down {
t.Fatalf("first failure = %v, want down", got)
}
if got := obs(now.Add(-time.Minute), true); got != None {
t.Errorf("older success = %v, want ignored", got)
}
}