feat: monitors, checks and random distribution to checker instances (M4) #50

Merged
Sebi merged 6 commits from feat/m4-checker into main 2026-11-22 00:45:07 +00:00
Owner

Milestone M4: monitors, checks and distributed checking. Stacked on #47 (M3); merge that first.

Architecture change (#49)

Only cmd/api talks to Postgres and Redis. Checkers and status page servers are agents: they authenticate with a token from api agent create and use the versioned agent API under /agent/v1. Cloud, hybrid and self-hosted deployments now share one protocol.

Scheduling (#11)

  • Per region, a Redis sorted set holds each monitor's next run.
  • Every API replica runs the dispatcher; a Lua script atomically hands due runs to a randomly chosen live checker instance of the region and schedules the next run, so no leader is needed and lost jobs never stop a monitor.
  • Any number of checker instances can share a region and token. Instances long-poll for jobs (doubling as heartbeat); jobs of instances silent for 45 s, or that shut down gracefully, go to the remaining ones.
  • A minute-ly reconciliation repairs Redis against the database.

Checks (#12)

HTTP(S) with expected status, keyword present/absent, redirects, TLS verification and headers; TCP; TLS certificate expiry; DNS (A, AAAA, CNAME, MX, NS, TXT, custom resolver, expected answer); ICMP over unprivileged sockets. Targets on private networks are refused for operator checkers in the cloud edition.

Data (#10, #13)

  • Monitors and region assignments under row-level security.
  • check_results hypertable with compression after 7 days, 90-day retention, hourly and daily continuous aggregates (real-time). It has no row-level security because TimescaleDB refuses compression and continuous aggregates on such tables; every results query takes org_id, enforced by a test.

API

/api/v1/org/monitors CRUD (monitors.read / monitors.write), /api/v1/regions with live instance counts. Operator CLI: api region add, api agent create|list|revoke.

Breaking

cmd/checker and cmd/statuspage need UPTIME_API_URL and UPTIME_AGENT_TOKEN instead of database and Redis settings; the checker's region comes from its token.

Testing

internal/api/checker_test.go runs the API with three in-process checkers in two regions against a local target: random distribution within a region, region isolation, failure reporting, handover when an instance stops. Integration tests now use a separate uptime_test database and run with go test -p 1.

Closes #10, #11, #12, #13, #49

🤖 Generated with Claude Code

https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3

Milestone M4: monitors, checks and distributed checking. Stacked on #47 (M3); merge that first. ## Architecture change (#49) Only `cmd/api` talks to Postgres and Redis. Checkers and status page servers are agents: they authenticate with a token from `api agent create` and use the versioned agent API under `/agent/v1`. Cloud, hybrid and self-hosted deployments now share one protocol. ## Scheduling (#11) - Per region, a Redis sorted set holds each monitor's next run. - Every API replica runs the dispatcher; a Lua script atomically hands due runs to a **randomly chosen live checker instance of the region** and schedules the next run, so no leader is needed and lost jobs never stop a monitor. - Any number of checker instances can share a region and token. Instances long-poll for jobs (doubling as heartbeat); jobs of instances silent for 45 s, or that shut down gracefully, go to the remaining ones. - A minute-ly reconciliation repairs Redis against the database. ## Checks (#12) HTTP(S) with expected status, keyword present/absent, redirects, TLS verification and headers; TCP; TLS certificate expiry; DNS (A, AAAA, CNAME, MX, NS, TXT, custom resolver, expected answer); ICMP over unprivileged sockets. Targets on private networks are refused for operator checkers in the cloud edition. ## Data (#10, #13) - Monitors and region assignments under row-level security. - `check_results` hypertable with compression after 7 days, 90-day retention, hourly and daily continuous aggregates (real-time). It has **no** row-level security because TimescaleDB refuses compression and continuous aggregates on such tables; every results query takes `org_id`, enforced by a test. ## API `/api/v1/org/monitors` CRUD (`monitors.read` / `monitors.write`), `/api/v1/regions` with live instance counts. Operator CLI: `api region add`, `api agent create|list|revoke`. ## Breaking `cmd/checker` and `cmd/statuspage` need `UPTIME_API_URL` and `UPTIME_AGENT_TOKEN` instead of database and Redis settings; the checker's region comes from its token. ## Testing `internal/api/checker_test.go` runs the API with three in-process checkers in two regions against a local target: random distribution within a region, region isolation, failure reporting, handover when an instance stops. Integration tests now use a separate `uptime_test` database and run with `go test -p 1`. Closes #10, #11, #12, #13, #49 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi added this to the M4 Checker milestone 2026-11-22 00:13:31 +00:00
Sebi changed target branch from feat/m3-permissions to main 2026-11-22 00:24:33 +00:00
Sebi added 6 commits 2026-11-22 00:24:33 +00:00
Checks need the same protection as identity provider requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Validate normalizes settings for the API; Run executes them on checkers.
Targets on private networks are refused unless explicitly allowed.

Refs #12

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
check_results has no row-level security because TimescaleDB refuses
compression and continuous aggregates on such tables. Every results
query takes org_id as a parameter, which a test enforces.

Refs #10, #13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Dispatch runs atomically in Redis so every API replica can do it. Jobs of
instances that stop polling return to their region.

Refs #11, #49

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Only cmd/api connects to Postgres and Redis. Agents authenticate with
tokens created by 'api agent create' and use /agent/v1: checkers long
poll for jobs, which doubles as heartbeat, and report results in
batches. Organizations manage monitors through /api/v1/org/monitors.

BREAKING CHANGE: cmd/checker and cmd/statuspage no longer read
UPTIME_DATABASE_URL or UPTIME_REDIS_*; they need UPTIME_API_URL and
UPTIME_AGENT_TOKEN. UPTIME_CHECKER_REGION is replaced by the region bound
to the agent token.

Closes #10
Closes #11
Closes #12
Closes #13
Closes #49

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
Sebi merged commit 9fb2687ae9 into main 2026-11-22 00:45:07 +00:00
Sebi deleted branch feat/m4-checker 2026-11-22 00:45:10 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ZB-Server/uptimesebi#50