feat(alerting): add monitor alert groups with default routing

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 18:25:36 +01:00
1 parent 52819e64bb
commit 8544e5a585
26 files changed
+2938 -1473

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
package migrations
import (
"context"
"os"
"strings"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
func TestAlertGroupsBackfill(t *testing.T) {
dsn := os.Getenv("UPTIME_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("UPTIME_TEST_DATABASE_URL not set")
}
ctx := context.Background()
if _, err := Up(ctx, dsn); err != nil {
t.Fatal(err)
}
conn, err := pgx.Connect(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer func() { _ = conn.Close(ctx) }()
tx, err := conn.Begin(ctx)
if err != nil {
t.Fatal(err)
}
// Exercise both directions inside a transaction; existing test data and
// goose's migration history remain unchanged after rollback.
defer func() { _ = tx.Rollback(ctx) }()
if _, err := tx.Exec(ctx, "SET LOCAL app.scope = 'system'"); err != nil {
t.Fatal(err)
}
raw, err := files.ReadFile("sql/00039_alert_groups.sql")
if err != nil {
t.Fatal(err)
}
up, down, ok := strings.Cut(string(raw), "-- +goose Down")
if !ok {
t.Fatal("missing down migration")
}
if _, err := tx.Exec(ctx, down); err != nil {
t.Fatalf("down: %v", err)
}
orgID := uuid.New()
if _, err := tx.Exec(ctx, `INSERT INTO orgs (id,slug,name) VALUES ($1,$2,'Existing organization')`, orgID, "migration-"+orgID.String()); err != nil {
t.Fatal(err)
}
if _, err := tx.Exec(ctx, `INSERT INTO monitors (org_id,name,type,target,interval_seconds,timeout_ms) VALUES ($1,'Existing monitor','tcp','example.test:443',60,1000)`, orgID); err != nil {
t.Fatal(err)
}
if _, err := tx.Exec(ctx, `INSERT INTO notification_channels (org_id,name,type,config) VALUES ($1,'Existing channel','webhook','{}')`, orgID); err != nil {
t.Fatal(err)
}
if _, err := tx.Exec(ctx, up); err != nil {
t.Fatalf("up: %v", err)
}
var groups, monitors, channels int
err = tx.QueryRow(ctx, `SELECT
(SELECT count(*) FROM alert_groups WHERE org_id=$1 AND is_default),
(SELECT count(*) FROM monitor_alert_groups WHERE org_id=$1),
(SELECT count(*) FROM alert_group_channels WHERE org_id=$1)`, orgID).Scan(&groups, &monitors, &channels)
if err != nil {
t.Fatal(err)
}
if groups != 1 || monitors != 1 || channels != 1 {
t.Fatalf("backfilled groups/monitors/channels = %d/%d/%d, want 1/1/1", groups, monitors, channels)
}
}
@@ -0,0 +1,78 @@
-- +goose Up
CREATE TABLE alert_groups (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
is_default boolean NOT NULL DEFAULT false,
UNIQUE (id, org_id)
);
CREATE UNIQUE INDEX alert_groups_default_idx ON alert_groups (org_id) WHERE is_default;
ALTER TABLE notification_channels ADD UNIQUE (id, org_id);
CREATE TABLE alert_group_channels (
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
group_id uuid NOT NULL,
channel_id uuid NOT NULL,
PRIMARY KEY (group_id, channel_id),
FOREIGN KEY (group_id, org_id) REFERENCES alert_groups (id, org_id) ON DELETE CASCADE,
FOREIGN KEY (channel_id, org_id) REFERENCES notification_channels (id, org_id) ON DELETE CASCADE
);
CREATE TABLE monitor_alert_groups (
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
monitor_id uuid NOT NULL,
group_id uuid NOT NULL,
PRIMARY KEY (monitor_id, group_id),
FOREIGN KEY (monitor_id, org_id) REFERENCES monitors (id, org_id) ON DELETE CASCADE,
FOREIGN KEY (group_id, org_id) REFERENCES alert_groups (id, org_id) ON DELETE CASCADE
);
CREATE INDEX monitor_alert_groups_group_idx ON monitor_alert_groups (group_id);
CREATE INDEX alert_group_channels_channel_idx ON alert_group_channels (channel_id);
INSERT INTO alert_groups (org_id, name, is_default) SELECT id, 'Default', true FROM orgs;
INSERT INTO alert_group_channels (org_id, group_id, channel_id)
SELECT c.org_id, g.id, c.id FROM notification_channels c JOIN alert_groups g ON g.org_id = c.org_id AND g.is_default;
INSERT INTO monitor_alert_groups (org_id, monitor_id, group_id)
SELECT m.org_id, m.id, g.id FROM monitors m JOIN alert_groups g ON g.org_id = m.org_id AND g.is_default;
-- Defaults apply to every creation path, including CLI and imports.
-- +goose StatementBegin
CREATE FUNCTION alert_groups_defaults() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF TG_TABLE_NAME = 'orgs' THEN
INSERT INTO alert_groups (org_id, name, is_default) VALUES (NEW.id, 'Default', true);
ELSIF TG_TABLE_NAME = 'monitors' THEN
INSERT INTO monitor_alert_groups (org_id, monitor_id, group_id)
SELECT NEW.org_id, NEW.id, id FROM alert_groups WHERE org_id = NEW.org_id AND is_default;
ELSE
INSERT INTO alert_group_channels (org_id, group_id, channel_id)
SELECT NEW.org_id, id, NEW.id FROM alert_groups WHERE org_id = NEW.org_id AND is_default;
END IF;
RETURN NEW;
END
$$;
-- +goose StatementEnd
CREATE TRIGGER org_alert_group AFTER INSERT ON orgs FOR EACH ROW EXECUTE FUNCTION alert_groups_defaults();
CREATE TRIGGER monitor_alert_group AFTER INSERT ON monitors FOR EACH ROW EXECUTE FUNCTION alert_groups_defaults();
CREATE TRIGGER channel_alert_group AFTER INSERT ON notification_channels FOR EACH ROW EXECUTE FUNCTION alert_groups_defaults();
ALTER TABLE alert_groups ENABLE ROW LEVEL SECURITY;
ALTER TABLE alert_groups FORCE ROW LEVEL SECURITY;
CREATE POLICY alert_groups_tenant ON alert_groups FOR ALL
USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id());
ALTER TABLE alert_group_channels ENABLE ROW LEVEL SECURITY;
ALTER TABLE alert_group_channels FORCE ROW LEVEL SECURITY;
CREATE POLICY alert_group_channels_tenant ON alert_group_channels FOR ALL
USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id());
ALTER TABLE monitor_alert_groups ENABLE ROW LEVEL SECURITY;
ALTER TABLE monitor_alert_groups FORCE ROW LEVEL SECURITY;
CREATE POLICY monitor_alert_groups_tenant ON monitor_alert_groups FOR ALL
USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id());
-- +goose Down
DROP TRIGGER channel_alert_group ON notification_channels;
DROP TRIGGER monitor_alert_group ON monitors;
DROP TRIGGER org_alert_group ON orgs;
DROP FUNCTION alert_groups_defaults();
DROP TABLE monitor_alert_groups;
DROP TABLE alert_group_channels;
DROP TABLE alert_groups;
ALTER TABLE notification_channels DROP CONSTRAINT notification_channels_id_org_id_key;