feat(agents): talk to the API over a WebSocket and push page changes
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
Checkers and status page servers keep one WebSocket open to the API at /agent/v1/stream and send their requests over it. The API answers them through the agent API as if they had come over HTTP, each authenticated again, so the protocol stays the same and a revoked token ends the stream. While the stream is down agents fall back to plain HTTP, and an API without it is asked again every five minutes. Over the stream the API tells status page servers which of their pages changed, so they fetch them at once instead of on their next refresh: when a region of a monitor changes between up and down, an incident opens or resolves, or anything of the organization is edited. Changes are announced through Redis to every API instance and sent at most every five seconds per stream. A page that changes while it is fetched is fetched once more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
27338f0557
commit
538c43b563
21 files changed
+1091
-45
No files matched your search
@@ -12,6 +12,40 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const latestOK = `-- name: LatestOK :many
|
||||
|
||||
SELECT monitor_id, region, ok FROM latest_results WHERE monitor_id = ANY ($1::uuid[])
|
||||
`
|
||||
|
||||
type LatestOKRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
Ok bool
|
||||
}
|
||||
|
||||
// latest_results holds the newest check result per monitor and region
|
||||
// (see migration 00055) and has row-level security.
|
||||
// Whether the newest result of each region of the monitors ids succeeded.
|
||||
func (q *Queries) LatestOK(ctx context.Context, ids []uuid.UUID) ([]LatestOKRow, error) {
|
||||
rows, err := q.db.Query(ctx, latestOK, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []LatestOKRow{}
|
||||
for rows.Next() {
|
||||
var i LatestOKRow
|
||||
if err := rows.Scan(&i.MonitorID, &i.Region, &i.Ok); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const latestResults = `-- name: LatestResults :many
|
||||
SELECT monitor_id, region, time, ok, latency_ms, status_code, error
|
||||
FROM latest_results
|
||||
@@ -107,8 +141,7 @@ func (q *Queries) LatestResultsForMonitors(ctx context.Context, arg LatestResult
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const upsertLatestResults = `-- name: UpsertLatestResults :exec
|
||||
|
||||
const upsertLatestResults = `-- name: UpsertLatestResults :many
|
||||
INSERT INTO latest_results (monitor_id, region, org_id, time, ok, latency_ms, status_code, error)
|
||||
SELECT DISTINCT ON (r.monitor_id, r.region) r.monitor_id, r.region, r.org_id, r.time, r.ok, r.latency_ms, r.status_code, r.error
|
||||
FROM jsonb_to_recordset($1::jsonb)
|
||||
@@ -118,13 +151,40 @@ ON CONFLICT (monitor_id, region) DO UPDATE
|
||||
SET org_id = excluded.org_id, time = excluded.time, ok = excluded.ok, latency_ms = excluded.latency_ms,
|
||||
status_code = excluded.status_code, error = excluded.error
|
||||
WHERE latest_results.time <= excluded.time
|
||||
RETURNING monitor_id, region, org_id, ok
|
||||
`
|
||||
|
||||
// latest_results holds the newest check result per monitor and region
|
||||
// (see migration 00055) and has row-level security.
|
||||
// Keeps the newest of rows, a JSON array of results, per monitor and
|
||||
// region; results arriving late do not replace newer ones.
|
||||
func (q *Queries) UpsertLatestResults(ctx context.Context, rows []byte) error {
|
||||
_, err := q.db.Exec(ctx, upsertLatestResults, rows)
|
||||
return err
|
||||
type UpsertLatestResultsRow struct {
|
||||
MonitorID uuid.UUID
|
||||
Region string
|
||||
OrgID uuid.UUID
|
||||
Ok bool
|
||||
}
|
||||
|
||||
// Keeps the newest of results, a JSON array, per monitor and
|
||||
// region; results arriving late do not replace newer ones. It returns the
|
||||
// rows it wrote.
|
||||
func (q *Queries) UpsertLatestResults(ctx context.Context, results []byte) ([]UpsertLatestResultsRow, error) {
|
||||
rows, err := q.db.Query(ctx, upsertLatestResults, results)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []UpsertLatestResultsRow{}
|
||||
for rows.Next() {
|
||||
var i UpsertLatestResultsRow
|
||||
if err := rows.Scan(
|
||||
&i.MonitorID,
|
||||
&i.Region,
|
||||
&i.OrgID,
|
||||
&i.Ok,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -201,6 +201,10 @@ type Querier interface {
|
||||
InvitationOrg(ctx context.Context, hash []byte) (uuid.UUID, error)
|
||||
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
||||
LastFailureEvidence(ctx context.Context, arg LastFailureEvidenceParams) (LastFailureEvidenceRow, error)
|
||||
// latest_results holds the newest check result per monitor and region
|
||||
// (see migration 00055) and has row-level security.
|
||||
// Whether the newest result of each region of the monitors ids succeeded.
|
||||
LatestOK(ctx context.Context, ids []uuid.UUID) ([]LatestOKRow, error)
|
||||
LatestResultForMonitor(ctx context.Context, arg LatestResultForMonitorParams) (LatestResultForMonitorRow, error)
|
||||
// Newest result per monitor and region in the org, of the last day.
|
||||
LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error)
|
||||
@@ -426,11 +430,10 @@ type Querier interface {
|
||||
UpdateStatusPage(ctx context.Context, arg UpdateStatusPageParams) (StatusPage, error)
|
||||
UpdateStatusPageGroup(ctx context.Context, arg UpdateStatusPageGroupParams) (StatusPageGroup, error)
|
||||
UpsertAlertmanagerAlert(ctx context.Context, arg UpsertAlertmanagerAlertParams) error
|
||||
// latest_results holds the newest check result per monitor and region
|
||||
// (see migration 00055) and has row-level security.
|
||||
// Keeps the newest of rows, a JSON array of results, per monitor and
|
||||
// region; results arriving late do not replace newer ones.
|
||||
UpsertLatestResults(ctx context.Context, rows []byte) error
|
||||
// Keeps the newest of results, a JSON array, per monitor and
|
||||
// region; results arriving late do not replace newer ones. It returns the
|
||||
// rows it wrote.
|
||||
UpsertLatestResults(ctx context.Context, results []byte) ([]UpsertLatestResultsRow, error)
|
||||
// A new version starts when the document is created or when the edit asks
|
||||
// everyone to accept it again.
|
||||
UpsertLegalDocument(ctx context.Context, arg UpsertLegalDocumentParams) error
|
||||
|
||||
@@ -1,18 +1,24 @@
|
||||
-- latest_results holds the newest check result per monitor and region
|
||||
-- (see migration 00055) and has row-level security.
|
||||
|
||||
-- name: UpsertLatestResults :exec
|
||||
-- Keeps the newest of rows, a JSON array of results, per monitor and
|
||||
-- region; results arriving late do not replace newer ones.
|
||||
-- name: LatestOK :many
|
||||
-- Whether the newest result of each region of the monitors ids succeeded.
|
||||
SELECT monitor_id, region, ok FROM latest_results WHERE monitor_id = ANY (sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: UpsertLatestResults :many
|
||||
-- Keeps the newest of results, a JSON array, per monitor and
|
||||
-- region; results arriving late do not replace newer ones. It returns the
|
||||
-- rows it wrote.
|
||||
INSERT INTO latest_results (monitor_id, region, org_id, time, ok, latency_ms, status_code, error)
|
||||
SELECT DISTINCT ON (r.monitor_id, r.region) r.monitor_id, r.region, r.org_id, r.time, r.ok, r.latency_ms, r.status_code, r.error
|
||||
FROM jsonb_to_recordset(sqlc.arg(rows)::jsonb)
|
||||
FROM jsonb_to_recordset(sqlc.arg(results)::jsonb)
|
||||
AS r(monitor_id uuid, region text, org_id uuid, time timestamptz, ok boolean, latency_ms integer, status_code integer, error text)
|
||||
ORDER BY r.monitor_id, r.region, r.time DESC
|
||||
ON CONFLICT (monitor_id, region) DO UPDATE
|
||||
SET org_id = excluded.org_id, time = excluded.time, ok = excluded.ok, latency_ms = excluded.latency_ms,
|
||||
status_code = excluded.status_code, error = excluded.error
|
||||
WHERE latest_results.time <= excluded.time;
|
||||
WHERE latest_results.time <= excluded.time
|
||||
RETURNING monitor_id, region, org_id, ok;
|
||||
|
||||
-- name: LatestResults :many
|
||||
-- Newest result per monitor and region in the org, of the last day.
|
||||
|
||||
@@ -146,7 +146,7 @@ WHERE published AND suspended_at IS NULL
|
||||
-- System scope: the published pages a status page server serves, those
|
||||
-- of agent_org for an organization's server; the platform's leave the
|
||||
-- pages of organizations serving their own.
|
||||
SELECT p.slug, COALESCE(CASE WHEN p.domain_verified_at IS NOT NULL THEN p.custom_domain END, '')::text AS host
|
||||
SELECT p.slug, COALESCE(CASE WHEN p.domain_verified_at IS NOT NULL THEN p.custom_domain END, '')::text AS host, p.org_id
|
||||
FROM status_pages p
|
||||
JOIN orgs o ON o.id = p.org_id
|
||||
WHERE p.published AND p.suspended_at IS NULL AND o.suspended_at IS NULL
|
||||
|
||||
@@ -470,7 +470,7 @@ func (q *Queries) ReparentStatusPageGroupMonitors(ctx context.Context, arg Repar
|
||||
}
|
||||
|
||||
const servablePages = `-- name: ServablePages :many
|
||||
SELECT p.slug, COALESCE(CASE WHEN p.domain_verified_at IS NOT NULL THEN p.custom_domain END, '')::text AS host
|
||||
SELECT p.slug, COALESCE(CASE WHEN p.domain_verified_at IS NOT NULL THEN p.custom_domain END, '')::text AS host, p.org_id
|
||||
FROM status_pages p
|
||||
JOIN orgs o ON o.id = p.org_id
|
||||
WHERE p.published AND p.suspended_at IS NULL AND o.suspended_at IS NULL
|
||||
@@ -482,8 +482,9 @@ ORDER BY p.slug
|
||||
`
|
||||
|
||||
type ServablePagesRow struct {
|
||||
Slug string
|
||||
Host string
|
||||
Slug string
|
||||
Host string
|
||||
OrgID uuid.UUID
|
||||
}
|
||||
|
||||
// System scope: the published pages a status page server serves, those
|
||||
@@ -498,7 +499,7 @@ func (q *Queries) ServablePages(ctx context.Context, agentOrg *uuid.UUID) ([]Ser
|
||||
items := []ServablePagesRow{}
|
||||
for rows.Next() {
|
||||
var i ServablePagesRow
|
||||
if err := rows.Scan(&i.Slug, &i.Host); err != nil {
|
||||
if err := rows.Scan(&i.Slug, &i.Host, &i.OrgID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
|
||||
Reference in new issue
Block a user