From c08cc9e212618e63625942afe0268013354ba712 Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Fri, 6 Nov 2026 09:05:41 -0500 Subject: [PATCH] Alert routes: post a firing alarm once, with an optional daily reminder Route editor gets 'When an alarm keeps firing' (every time, the default; once until it clears; once then a daily reminder) and per-alarm overrides. Alert history folds an episode's firings into one row with a count, and the alert page links a held repeat to the alarm that posted. Docs updated. --- dashboard/e2e/integrations.spec.ts | 82 +++++++++++ dashboard/src/app/alerts/detail/page.tsx | 9 ++ dashboard/src/app/alerts/outcome.tsx | 20 +++ dashboard/src/app/alerts/page.tsx | 30 +++- dashboard/src/app/settings/alerts/page.tsx | 152 +++++++++++++++++++- dashboard/src/lib/alert-episodes.ts | 91 ++++++++++++ dashboard/src/lib/api.ts | 19 +++ docs/content/docs/concepts/aws-accounts.mdx | 10 ++ 8 files changed, 403 insertions(+), 10 deletions(-) create mode 100644 dashboard/src/lib/alert-episodes.ts diff --git a/dashboard/e2e/integrations.spec.ts b/dashboard/e2e/integrations.spec.ts index 0837a26c..1767d684 100644 --- a/dashboard/e2e/integrations.spec.ts +++ b/dashboard/e2e/integrations.spec.ts @@ -459,6 +459,88 @@ test.describe("Alert routes", () => { expect(saved.post_recoveries).toBe(true); }); + test("a route saved before the repeat setting posts every time, and saving keeps it that way", async ({ page }) => { + const db = await mockApi(page); + await page.goto("/settings/alerts"); + const card = page.getByTestId("route-prod-alarms"); + await expect(card.getByText("Repeats: every time it fires")).toBeVisible(); + await card.getByRole("button", { name: "Edit rules" }).click(); + const repeats = page.getByRole("region", { name: "When an alarm keeps firing" }); + await expect(repeats.getByRole("radio", { name: /Every time it fires/ })).toBeChecked(); + await page.getByRole("button", { name: "Save route" }).click(); + await expect(page.getByRole("heading", { level: 1, name: "Alert routes" })).toBeVisible(); + const saved = callsTo(db, "PUT", /\/api\/alert-routes$/).at(-1)!.body as { repeat: string; alarm_repeat: Record }; + expect(saved.repeat).toBe("every_time"); + expect(saved.alarm_repeat).toEqual({}); + }); + + test("the repeat setting saves per route, an alarm can have its own, and pausing keeps both", async ({ page }) => { + const db = await mockApi(page); + await page.goto("/settings/alerts"); + await page.getByTestId("route-prod-alarms").getByRole("button", { name: "Edit rules" }).click(); + const repeats = page.getByRole("region", { name: "When an alarm keeps firing" }); + await repeats.getByRole("radio", { name: /Once, then a daily reminder/ }).check(); + await expect(repeats).toContainText("one message a day while it is still firing"); + + // One alarm on the topic keeps posting every time. + await pickOption(repeats.getByRole("combobox", { name: "Alarm to give its own setting" }), "prod-db-cpu"); + await repeats.getByRole("button", { name: "Give it its own setting" }).click(); + const own = repeats.getByRole("list", { name: "Alarms with their own setting" }); + await expect(own.getByRole("listitem")).toHaveCount(1); + await expect(own.getByRole("combobox", { name: "Repeat setting for prod-db-cpu" })).toContainText("Every time it fires"); + await pickOption(own.getByRole("combobox", { name: "Repeat setting for prod-db-cpu" }), "until_clear"); + await shot(page, "alerts-editor-repeat"); + + await page.getByRole("button", { name: "Save route" }).click(); + await expect(page.getByRole("heading", { level: 1, name: "Alert routes" })).toBeVisible(); + const saved = callsTo(db, "PUT", /\/api\/alert-routes$/).at(-1)!.body as { repeat: string; alarm_repeat: Record }; + expect(saved.repeat).toBe("daily"); + expect(saved.alarm_repeat).toEqual({ "prod-db-cpu": "until_clear" }); + + const card = page.getByTestId("route-prod-alarms"); + await expect(card.getByText("Repeats: once, then a daily reminder")).toBeVisible(); + await expect(card.getByText("1 alarm with their own repeat setting")).toBeVisible(); + + // Pausing the route sends the setting back unchanged. + await card.getByRole("switch").click(); + await expect.poll(() => callsTo(db, "PUT", /\/api\/alert-routes$/).length).toBe(2); + const paused = callsTo(db, "PUT", /\/api\/alert-routes$/).at(-1)!.body as { enabled: boolean; repeat: string; alarm_repeat: Record }; + expect(paused).toMatchObject({ enabled: false, repeat: "daily", alarm_repeat: { "prod-db-cpu": "until_clear" } }); + }); + + test("a firing alarm's repeats show as one row with a count", async ({ page }) => { + const OPEN = "01JC0000000000000000000001"; + const fire = (n: number, min: number, outcome: string, suppressed = false) => ({ + id: `01JC00000000000000000000${String(n).padStart(2, "0")}`, received_at: ago(min * MIN), topic_arn: TOPIC, repo: "", kind: "cloudwatch_alarm", + title: "ALARM: example-queue-depth", outcome, ticket_id: "ALERT-QUEUE", episode: OPEN, episode_fires: n, suppressed, + }); + await mockApi(page, { + alerts: [ + fire(5, 2, "suppressed", true), + { id: "01JC0000000000000000000004", received_at: ago(5 * MIN), topic_arn: TOPIC, repo: "", kind: "cloudwatch_alarm", title: "OK: example-queue-depth", outcome: "clearing", ticket_id: "ALERT-QUEUE", episode: OPEN }, + fire(3, 8, "reminded"), + fire(2, 12, "suppressed", true), + fire(1, 15, "notified"), + ALERTS[3], + ], + }); + await page.goto("/settings/alerts"); + const timeline = page.getByRole("list", { name: "Recent alerts" }); + // The four firings fold into one row; the OK and the other alert stay rows. + await expect(timeline.locator(":scope > li")).toHaveCount(3); + const episode = page.getByTestId("alert-episode"); + await expect(episode).toHaveCount(1); + await expect(episode).toContainText("Fired 5 times: 1 posted, 1 reminder, 2 held back"); + await expect(episode.getByRole("link", { name: "ALARM: example-queue-depth" })).toHaveAttribute("href", new RegExp(`id=${OPEN}$`)); + await episode.getByText(/Fired 5 times/).click(); + const firings = episode.getByRole("list", { name: "Firings of ALARM: example-queue-depth" }); + await expect(firings.getByRole("listitem")).toHaveCount(4); + await expect(firings).toContainText("Still firing, held back"); + await expect(firings).toContainText("Daily reminder"); + await expect(timeline.getByRole("listitem").filter({ hasText: "OK: example-queue-depth" })).toContainText("OK, waiting to clear"); + await shot(page, "alerts-episode"); + }); + test("adding a route refuses a topic in an account that isn't connected", async ({ page }) => { await mockApi(page); await page.goto("/settings/alerts"); diff --git a/dashboard/src/app/alerts/detail/page.tsx b/dashboard/src/app/alerts/detail/page.tsx index 80bc57ae..8aed92a5 100644 --- a/dashboard/src/app/alerts/detail/page.tsx +++ b/dashboard/src/app/alerts/detail/page.tsx @@ -129,6 +129,15 @@ function AlertDetail() {

{outcomeDetail(a.outcome)}

+ {a.episode && a.episode !== a.id && ( +

+ Part of one firing that started with{" "} + + the first alarm + + {a.episode_fires ? `; this was firing number ${a.episode_fires}.` : "."} +

+ )}
Repo
{a.repo || "—"}
diff --git a/dashboard/src/app/alerts/outcome.tsx b/dashboard/src/app/alerts/outcome.tsx index a98066fa..c0080fba 100644 --- a/dashboard/src/app/alerts/outcome.tsx +++ b/dashboard/src/app/alerts/outcome.tsx @@ -81,6 +81,26 @@ export const OUTCOMES: Record< tone: "muted", group: "ignored", }, + suppressed: { + label: "Held back", + detail: + "Still firing: the route posts this alarm once until it clears, so this repeat was recorded but not posted, and no new run started.", + tone: "muted", + group: "handled", + }, + reminded: { + label: "Daily reminder", + detail: "Still firing a day after it was last posted, so one reminder was posted saying since when and how many times it fired.", + tone: "info", + group: "handled", + }, + clearing: { + label: "OK, waiting", + detail: + "The alarm went back to OK. It counts as cleared once it stays OK through a quiet window (30 minutes or more); an alarm inside the window keeps it the same episode.", + tone: "muted", + group: "handled", + }, route_paused: { label: "Route paused", detail: "The route for this topic is paused.", diff --git a/dashboard/src/app/alerts/page.tsx b/dashboard/src/app/alerts/page.tsx index 47bc8dbd..202c0b3a 100644 --- a/dashboard/src/app/alerts/page.tsx +++ b/dashboard/src/app/alerts/page.tsx @@ -20,7 +20,9 @@ import { usePagedList, useTeamRepos, } from "@/components/list"; -import { KIND_LABEL, OUTCOMES, OUTCOME_GROUPS, humanize, outcomeDetail, outcomeLook } from "./outcome"; +import { KIND_LABEL, OUTCOMES, OUTCOME_GROUPS, OutcomeBadge, humanize, outcomeDetail, outcomeLook } from "./outcome"; +import { groupEpisodes, groupSummary } from "@/lib/alert-episodes"; +import { linkButton } from "@/components/ui"; export default function AlertsHistoryGuarded() { return ( @@ -121,24 +123,42 @@ function AlertsHistory() { ) : ( - {shown.map((a) => { + {groupEpisodes(shown).map((g) => { + const a = g.head; const look = outcomeLook(a.outcome); + const repeats = groupSummary(g); return ( Alert with no title} titleText={a.title || undefined} href={`/alerts/detail?id=${a.id}`} meta={[a.kind ? KIND_LABEL[a.kind] ?? humanize(a.kind) : null, !repo ? a.repo || null : null]} - summary={outcomeDetail(a.outcome)} + summary={repeats ?? outcomeDetail(a.outcome)} chips={ } - time={a.received_at} + time={g.latest.received_at} + details={ + repeats ? ( +
    + {g.members.map((m) => ( +
  1. + + {m.received_at ? new Date(m.received_at).toLocaleString() : "Unknown time"} + + +
  2. + ))} +
+ ) : undefined + } + detailsLabel={repeats ? `${g.members.length} firings` : undefined} + detailsFor={repeats ? `firings of ${a.title || "this alert"}` : undefined} /> ); })} diff --git a/dashboard/src/app/settings/alerts/page.tsx b/dashboard/src/app/settings/alerts/page.tsx index e3edef81..5db408aa 100644 --- a/dashboard/src/app/settings/alerts/page.tsx +++ b/dashboard/src/app/settings/alerts/page.tsx @@ -9,6 +9,7 @@ import { type AlertBudget, type AlertEvent, type AlertNotifySettings, + type AlertRepeat, type AlertRoute, type AlertRule, type AwsConnection, @@ -17,6 +18,7 @@ import { type TopicAlarm, } from "@/lib/api"; import { timeAgo } from "@/lib/data-sources"; +import { REPEAT_LABEL, REPEAT_OPTIONS, groupEpisodes, groupSummary, routeRepeat } from "@/lib/alert-episodes"; import { RoleGuard } from "@/components/role-guard"; import { useToast } from "@/components/toast"; import { useConfirm } from "@/components/confirm-dialog"; @@ -59,7 +61,7 @@ export default function AlertsPageGuarded() { ); } -const EMPTY_ROUTE: AlertRoute = { topic_arn: "", rules: [], enabled: true, notify_mode: "team", post_recoveries: false }; +const EMPTY_ROUTE: AlertRoute = { topic_arn: "", rules: [], enabled: true, notify_mode: "team", post_recoveries: false, repeat: "every_time", alarm_repeat: {} }; const EMPTY_RULE: AlertRule = { alarms: [], terms: [], repo: "", instructions: "" }; const NOTIFY_LABEL: Record = { @@ -137,6 +139,9 @@ const OUTCOME_CHIP: Record 0 && + `${Object.keys(r.alarm_repeat ?? {}).length} alarm${Object.keys(r.alarm_repeat ?? {}).length === 1 ? "" : "s"} with their own repeat setting`, r.rules.length === 0 ? "No rules: chat only" : `${r.rules.length} rule${r.rules.length === 1 ? "" : "s"}`, ]} summary={ @@ -955,13 +963,16 @@ function EventsTimeline({

No alerts match. Choose All to see every alert.

) : (
    - {shown.map((e) => { + {groupEpisodes(shown).map((g) => { + const e = g.head; const resolves = pairs.get(e.id); const recovery = resolvedBy.get(e.id); + const repeats = groupSummary(g); + const last = g.latest.received_at; return ( -
  1. -
  2. +
    )} + {repeats && ( +
    + + {repeats} + {e.received_at ? `, since ${timeAgo(e.received_at)}` : ""} + +
      + {g.members.map((m) => ( +
    1. + + {m.received_at ? timeAgo(m.received_at) : "Unknown time"} + + +
    2. + ))} +
    +
    + )}
    @@ -1297,6 +1326,108 @@ function RuleEditor({ ); } +/** + * How often an alert that keeps firing posts: the route's setting, and the + * alarms (by name) that have their own. + */ +function RepeatSettings({ + value, + overrides, + alarms, + onChange, +}: { + value: AlertRepeat; + overrides: Record; + alarms: TopicAlarm[] | null; + onChange: (repeat: AlertRepeat, overrides: Record) => void; +}) { + const [adding, setAdding] = useState(""); + const names = Object.keys(overrides).sort(); + const choices = (alarms ?? []).map((a) => a.name).filter((n) => !(n in overrides)); + const setOne = (name: string, r: AlertRepeat) => onChange(value, { ...overrides, [name]: r }); + const removeOne = (name: string) => { + const next = { ...overrides }; + delete next[name]; + onChange(value, next); + }; + const add = () => { + const name = adding.trim(); + if (!name || name in overrides) return; + // A new alarm starts from the other choice than the route's, the reason to add one. + setOne(name, value === "every_time" ? "until_clear" : "every_time"); + setAdding(""); + }; + const options = REPEAT_OPTIONS.map((o) => ({ value: o.value, label: o.label })); + return ( +
    +

    When an alarm keeps firing

    +

    + An alarm that flips between OK and ALARM can post over and over. Choose how often it posts while it is still firing. Every firing is + still recorded on the Alerts page. +

    +
    + How often this route posts an alarm that keeps firing + {REPEAT_OPTIONS.map((o) => ( + + ))} +
    + +
    +

    Alarms with their own setting

    +

    An alarm listed here uses its own choice instead of the route's. It applies to CloudWatch alarms, by name.

    + {names.length > 0 && ( +
      + {names.map((name) => ( +
    • + {name} + ({ value: n, label: n }))} + className="min-w-0 sm:max-w-80" + /> + ) : ( + + {(props) => ( + setAdding(e.target.value)} placeholder="example-queue-depth" className={inputClass} /> + )} + + )} + +
    +
    + + ); +} + function RouteEditor({ initial, isNew, @@ -1376,6 +1507,8 @@ function RouteEditor({ notify_mode: mode, ...(mode === "custom" ? channelsPayload(channels, initial.webhooks) : {}), post_recoveries: !!route.post_recoveries, + repeat: routeRepeat(route), + alarm_repeat: route.alarm_repeat ?? {}, }); toast("Route saved"); onSaved(); @@ -1502,6 +1635,15 @@ function RouteEditor({ )} + {mode !== "off" && ( + setRoute({ ...route, repeat, alarm_repeat })} + /> + )} +
    setRoute({ ...route, enabled })} label="Route is on" onLabel="On" offLabel="Paused" />
    diff --git a/dashboard/src/lib/alert-episodes.ts b/dashboard/src/lib/alert-episodes.ts new file mode 100644 index 00000000..7b68d879 --- /dev/null +++ b/dashboard/src/lib/alert-episodes.ts @@ -0,0 +1,91 @@ +import type { AlertEvent, AlertRepeat } from "@/lib/api"; + +/** The repeat settings in the order the editor offers them, in plain words. */ +export const REPEAT_OPTIONS: { value: AlertRepeat; label: string; help: string }[] = [ + { + value: "every_time", + label: "Every time it fires", + help: "Each alarm posts a message. This is how routes worked before this setting.", + }, + { + value: "until_clear", + label: "Once, until it clears", + help: "The first alarm posts. While it keeps firing, even when it flips to OK and back, nothing more posts and no second fix starts. Once it has stayed OK for 30 minutes (longer for alarms that check less often), the next alarm posts again.", + }, + { + value: "daily", + label: "Once, then a daily reminder", + help: "Like once until it clears, plus one message a day while it is still firing, saying since when and how many times it fired.", + }, +]; + +export const REPEAT_LABEL: Record = Object.fromEntries(REPEAT_OPTIONS.map((o) => [o.value, o.label])) as Record< + AlertRepeat, + string +>; + +/** The setting a route uses; routes saved before it existed post every time. */ +export function routeRepeat(r: { repeat?: AlertRepeat | null }): AlertRepeat { + return r.repeat && r.repeat in REPEAT_LABEL ? r.repeat : "every_time"; +} + +/** Outcomes of a delivery that is the alert recovering, not firing. */ +const RECOVERY_OUTCOMES = new Set(["clearing", "recovered", "no_open_alert"]); + +function isFire(e: AlertEvent): boolean { + return !RECOVERY_OUTCOMES.has(e.outcome) && e.kind !== "recovery" && !/^OK: /.test(e.title ?? ""); +} + +/** One row of history: an alert, or a firing alarm's episode with its repeats. */ +export interface EpisodeGroup { + /** The alert the row shows: the one that opened the episode when it is loaded. */ + head: AlertEvent; + /** Every delivery in the group, newest first (just `head` for a lone alert). */ + members: AlertEvent[]; + /** Deliveries held back (recorded, not posted) and reminders posted. */ + held: number; + reminders: number; + /** The newest delivery: where the row sits in the list. */ + latest: AlertEvent; +} + +/** + * Pure: alerts (newest first) with each episode's firings folded into one + * row, placed where its newest firing is. Recoveries stay rows of their own, + * as do alerts on routes that post every time (they carry no episode). + */ +export function groupEpisodes(events: AlertEvent[]): EpisodeGroup[] { + const groups: EpisodeGroup[] = []; + const byEpisode = new Map(); + for (const e of events) { + const key = e.episode && isFire(e) ? e.episode : null; + const known = key ? byEpisode.get(key) : undefined; + if (known) { + known.members.push(e); + continue; + } + const g: EpisodeGroup = { head: e, members: [e], held: 0, reminders: 0, latest: e }; + groups.push(g); + if (key) byEpisode.set(key, g); + } + for (const g of groups) { + const key = g.head.episode; + g.head = g.members.find((m) => m.id === key) ?? g.members[g.members.length - 1]; + g.held = g.members.filter((m) => m.suppressed || m.outcome === "suppressed").length; + g.reminders = g.members.filter((m) => m.outcome === "reminded").length; + } + return groups; +} + +/** "Fired 7 times: 1 posted, 1 reminder, 5 held back", for a group with repeats. */ +export function groupSummary(g: EpisodeGroup): string | null { + if (g.members.length < 2) return null; + const fires = Math.max(g.members.length, ...g.members.map((m) => m.episode_fires ?? 0)); + const posted = g.members.length - g.held - g.reminders; + const parts = [ + posted > 0 ? `${posted} posted` : null, + g.reminders ? `${g.reminders} reminder${g.reminders === 1 ? "" : "s"}` : null, + g.held ? `${g.held} held back` : null, + ].filter(Boolean); + return `Fired ${fires} times: ${parts.join(", ")}`; +} diff --git a/dashboard/src/lib/api.ts b/dashboard/src/lib/api.ts index e7769ea8..0d256810 100644 --- a/dashboard/src/lib/api.ts +++ b/dashboard/src/lib/api.ts @@ -2619,9 +2619,19 @@ export interface AlertRoute { webhooks?: WebhooksView; /** Post recoveries (one per open alert message) and other non-alarm state changes. Off by default. */ post_recoveries?: boolean; + /** How often a firing alert posts. Absent on routes saved before the setting: every time. */ + repeat?: AlertRepeat; + /** CloudWatch alarms, by exact name, with their own repeat setting. */ + alarm_repeat?: Record; updated_at?: string; } +/** + * How often a route posts an alert that keeps firing: every time; once until + * it clears; or once, then a reminder a day while it is still firing. + */ +export type AlertRepeat = "every_time" | "until_clear" | "daily"; + /** The team's daily spending limit for alert runs, and the last 24 hours' spend. */ export interface AlertBudget { /** Most tokens (input + output) alert runs may use per rolling day. */ @@ -2685,6 +2695,15 @@ export interface AlertEvent { ticket_id?: string | null; /** For a recovery: the ticket of the alert it resolves (newer events only). */ recovers_ticket?: string | null; + /** + * On routes that post once: the alert that opened this alert's episode + * (the first ALARM, which posted). Repeats while it kept firing carry it too. + */ + episode?: string | null; + /** How many times the alert had fired in its episode when this arrived. */ + episode_fires?: number | null; + /** Recorded but not posted: the alarm was still firing in its episode. */ + suppressed?: boolean; } export interface AlertEventDetail { diff --git a/docs/content/docs/concepts/aws-accounts.mdx b/docs/content/docs/concepts/aws-accounts.mdx index d340b156..454256bd 100644 --- a/docs/content/docs/concepts/aws-accounts.mdx +++ b/docs/content/docs/concepts/aws-accounts.mdx @@ -66,6 +66,16 @@ With several accounts, the page lists every account's state with an update link To start work from CloudWatch alarms, add an alert route under **Settings**, then **Alerts**, and subscribe your SNS topic to the address it shows. Save and turn on the route first: CoderHelm only confirms subscriptions for topics with a route that's on. See [Webhooks](/api/webhooks) for how messages are verified. +### Alarms that keep firing + +An alarm that flips between OK and ALARM can post over and over. Each route has a setting, **When an alarm keeps firing**, and a CloudWatch alarm can have its own: + +- **Every time it fires** (the default): each alarm posts. Routes saved before this setting work this way. +- **Once, until it clears**: the first alarm posts. While it keeps firing nothing more posts, and no second fix run starts. +- **Once, then a daily reminder**: the same, plus one message a day saying it is still firing, since when and how many times. + +An alarm has cleared once it stays OK for 30 minutes, or two of its own evaluation spans when that is longer (up to 6 hours). Going back to ALARM inside that window still counts as the same firing. After a real clear the recovery posts once, if the route posts recoveries, and the next alarm posts right away. Every firing is still recorded; the Alerts page shows them as one row with a count. + ## Running your own install On a self-hosted install, the role trusts **your install's own AWS account**, never CoderHelm's, and the stack links come from your install's release. Reading the install's own account works the same way. See [Integrations](/self-host/integrations) in the self-hosting guide.