Every module named its events the way the old bus spelled a routing key — `module.<module>.<verb>`. Design 29 says a module names an event locally and the mesh works out where it lands, so all 37 were stale against a rule already decided. On the new bus that derives into a namespace belonging to a module called "module", so no cross-module subscription in the mesh matched anything: nothing failed, nothing reacted (novox/hq 04-ISSUES/127). 36 manifests converted, and 43 files of module code with them. The code mattered as much as the manifests: the runtime builds the subject from what `emit()` is handed, so a converted manifest with unconverted code would have had the permission and the subject disagree. Three things the new check found on the way: - `photos` emitted an event its manifest never declared, which the new bus refuses outright. Declared. - `showcase` waited for an event nothing emits, so its demo could never be triggered — only `showcase` may publish under its own name. It emits both halves now. - `distribution` declared an event named after a different module. It emits `image.pushed` under its own name. An event about a *role* belongs on the seat, where the name outlives whoever holds it, but the sdk has no way to publish on a seat yet, so that stays recorded rather than declared. The audit logger's "everything" pattern is `**` rather than the old bus's `#`.
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
// 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<string>();
|
|
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<void> {
|
|
const now = new Set<string>();
|
|
const byKey = new Map<string, GrafanaAlert>();
|
|
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("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");
|
|
}
|