feat(alerting): add monitor alert groups with default routing
This commit is contained in:
1 parent
52819e64bb
commit
8544e5a585
26 files changed
+2938
-1473
No files matched your search
@@ -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;
|
||||
Reference in new issue
Block a user