// grafana's events. The tool runtime imports this once the broker is bound. It watches unified // alerting and announces when an alert instance starts firing. // // Emits (novox/hq ADR 0041/0042): // module.grafana.alert.firing — an alert instance entered the Alerting state // // A Grafana with no alerting configured simply never has a firing alert, so this observes nothing // and emits nothing — no error, no noise. import { emit } from "@novox/mesh-sdk/events"; import { GrafanaClient, type GrafanaAlert } from "./client.js"; // Constructed lazily so an unconfigured node (no auth) loads this entrypoint without crashing the // events host — it simply watches nothing. let grafana: GrafanaClient | undefined; try { grafana = GrafanaClient.fromEnv(); } catch (err) { console.log(`[grafana] not configured, not watching alerts: ${err}`); } // Firing alerts, by diffing the set currently in the Alerting state. Primed silently on the first // look so alerts already firing when this started are not announced as freshly firing. const firing = new Set(); let primed = false; const alertKey = (a: GrafanaAlert): string => `${a.name}:${Object.entries(a.labels).sort().map(([k, v]) => `${k}=${v}`).join(",")}`; async function pollAlerts(client: GrafanaClient): Promise { const now = new Set(); const byKey = new Map(); for (const a of await client.listAlerts()) { if (a.state.toLowerCase() !== "alerting") continue; const key = alertKey(a); now.add(key); byKey.set(key, a); } if (primed) { for (const key of now) { if (!firing.has(key)) { const a = byKey.get(key)!; await emit("module.grafana.alert.firing", { name: a.name, labels: a.labels, activeAt: a.activeAt }); } } } firing.clear(); for (const key of now) firing.add(key); primed = true; } if (grafana) { const client = grafana; const run = (): void => void pollAlerts(client).catch((err) => console.error(`[grafana] ${err}`)); setInterval(run, 30_000); run(); console.log("[grafana] watching for firing alerts"); }