-- Dead man's switches become rows of their own. -- -- 004 kept a team's switches in one string with one timeout and one severity, -- which was enough to configure them and not enough to show them: there was no -- thing to list, nothing to hang a status on, and every switch in a team had to -- share a deadline. A row per switch gives each its own name, matcher, timeout -- and severity, and gives the Team → Switches page something to be a list of. -- -- The matcher keeps the syntax the string used, one matcher per row: -- `alertname=Watchdog,cluster=prod`. The unit of monitoring is still the -- fingerprint, so a matcher that many clusters satisfy is still one switch row -- watching several independent heartbeats. CREATE TABLE deadman_switches ( id BIGSERIAL PRIMARY KEY, team_id BIGINT NOT NULL REFERENCES teams(id) ON DELETE CASCADE, -- What the owner calls it. Defaults to the matcher when they do not say. name TEXT NOT NULL, -- "," separates the label conditions, "=" is exact equality, and alertname is -- mandatory: it is what keeps the sweeper's candidate query on an index. matcher TEXT NOT NULL, -- Seconds of silence before the switch is declared dead. Never zero: a switch -- that cannot fire is deleted, not disabled. timeout_seconds BIGINT NOT NULL CHECK (timeout_seconds > 0), -- The severity its incidents open at. See 004 for why they carry their own. severity TEXT NOT NULL DEFAULT 'critical', created_at BIGINT NOT NULL DEFAULT FLOOR(EXTRACT(EPOCH FROM now()))::bigint ); CREATE INDEX deadman_switches_team_idx ON deadman_switches (team_id); -- Carry every team's configuration over, one row per matcher. A team whose -- timeout was zero had switches turned off, which is now "no rows". INSERT INTO deadman_switches (team_id, name, matcher, timeout_seconds, severity) SELECT c.team_id, btrim(m), btrim(m), c.timeout_seconds, c.severity FROM deadman_configs c, LATERAL regexp_split_to_table(c.matchers, ';') AS m WHERE c.timeout_seconds > 0 AND btrim(m) <> '' ORDER BY c.team_id; -- The server seeds environment defaults into teams once, and remembers that it -- did. An install that had a row per team was already seeded; without this -- marker the first start after upgrading would seed teams that had switched -- theirs off. INSERT INTO settings (key, value) SELECT 'deadman_seeded', '1' WHERE EXISTS (SELECT 1 FROM deadman_configs); DROP TABLE deadman_configs;