feat(checker): send evidence with failures, changes and every 10 minutes

Refs #79

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 12:09:42 +01:00
1 parent 48312fc59c
commit 78cd911008
3 files changed
+76 -2

No files matched your search

+3
View File
@@ -78,6 +78,9 @@ type Result struct {
LatencyMS int `json:"latency_ms"` LatencyMS int `json:"latency_ms"`
StatusCode int `json:"status_code,omitempty"` StatusCode int `json:"status_code,omitempty"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
// Evidence is what the check saw (check.Evidence), sent only now and
// then to bound traffic; see the checker's policy.
Evidence json.RawMessage `json:"evidence,omitempty"`
} }
// ResultsRequest reports finished checks in a batch. // ResultsRequest reports finished checks in a batch.
+40 -2
View File
@@ -5,12 +5,15 @@ package checker
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"log/slog" "log/slog"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/google/uuid"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/check" "git.zb-server.de/ZB-Server/uptimesebi/internal/check"
) )
@@ -38,6 +41,35 @@ type Runner struct {
results chan agentapi.Result results chan agentapi.Result
lastPoll atomic.Int64 lastPoll atomic.Int64
evidenceMu sync.Mutex
evidence map[uuid.UUID]evidenceState
}
// evidenceState remembers what was last sent for a monitor.
type evidenceState struct {
ok bool
sent time.Time
}
// evidenceEvery is how often evidence of healthy checks is sent, so the
// console can show what a working response looks like.
const evidenceEvery = 10 * time.Minute
// shouldSendEvidence decides whether a result carries its evidence: every
// failure, every change between success and failure, and otherwise every
// evidenceEvery. Most results thus stay small.
func (r *Runner) shouldSendEvidence(monitor uuid.UUID, ok bool, now time.Time) bool {
r.evidenceMu.Lock()
defer r.evidenceMu.Unlock()
last, seen := r.evidence[monitor]
send := !ok || !seen || last.ok != ok || now.Sub(last.sent) >= evidenceEvery
if send {
last.sent = now
}
last.ok = ok
r.evidence[monitor] = last
return send
} }
// New returns a Runner that runs at most concurrency checks at once. // New returns a Runner that runs at most concurrency checks at once.
@@ -48,6 +80,8 @@ func New(api API, concurrency int, log *slog.Logger) *Runner {
slots: make(chan struct{}, max(concurrency, 1)), slots: make(chan struct{}, max(concurrency, 1)),
run: check.Run, run: check.Run,
results: make(chan agentapi.Result, 1024), results: make(chan agentapi.Result, 1024),
evidence: map[uuid.UUID]evidenceState{},
} }
} }
@@ -137,12 +171,16 @@ func (r *Runner) execute(ctx context.Context, j agentapi.Job) agentapi.Result {
start := time.Now() start := time.Now()
o := r.run(ctx, check.Spec{ o := r.run(ctx, check.Spec{
Type: check.Type(j.Type), Target: j.Target, Timeout: time.Duration(j.TimeoutMS) * time.Millisecond, Type: check.Type(j.Type), Target: j.Target, Timeout: time.Duration(j.TimeoutMS) * time.Millisecond,
Config: j.Config, AllowPrivate: j.AllowPrivate, Config: j.Config, AllowPrivate: j.AllowPrivate, Capture: true,
}) })
return agentapi.Result{ res := agentapi.Result{
MonitorID: j.MonitorID, ScheduledAt: j.ScheduledAt, StartedAt: start, OK: o.OK, MonitorID: j.MonitorID, ScheduledAt: j.ScheduledAt, StartedAt: start, OK: o.OK,
LatencyMS: int(o.Latency.Milliseconds()), StatusCode: o.StatusCode, Error: o.Error, LatencyMS: int(o.Latency.Milliseconds()), StatusCode: o.StatusCode, Error: o.Error,
} }
if o.Evidence != nil && r.shouldSendEvidence(j.MonitorID, o.OK, start) {
res.Evidence, _ = json.Marshal(o.Evidence)
}
return res
} }
// report batches results and sends them. Unsent results are retried with // report batches results and sends them. Unsent results are retried with
+33
View File
@@ -0,0 +1,33 @@
package checker
import (
"log/slog"
"testing"
"time"
"github.com/google/uuid"
)
func TestEvidencePolicy(t *testing.T) {
r := New(nil, 1, slog.New(slog.DiscardHandler))
m := uuid.New()
now := time.Now()
steps := []struct {
after time.Duration
ok bool
send bool
}{
{0, true, true}, // first result
{time.Minute, true, false}, // healthy, sent recently
{2 * time.Minute, false, true}, // failure
{3 * time.Minute, false, true}, // failures always
{4 * time.Minute, true, true}, // recovery
{5 * time.Minute, true, false}, // healthy again
{4*time.Minute + evidenceEvery, true, true}, // periodic sample
}
for i, s := range steps {
if got := r.shouldSendEvidence(m, s.ok, now.Add(s.after)); got != s.send {
t.Errorf("step %d (ok=%t after %v): send = %t, want %t", i, s.ok, s.after, got, s.send)
}
}
}