diff --git a/workers/meter/README.md b/workers/meter/README.md index 4eb666a..d7fb015 100644 --- a/workers/meter/README.md +++ b/workers/meter/README.md @@ -83,9 +83,14 @@ nothing there. | `BILLING_DAY` | `13` (cycle starts on the 13th; required, 1-28) | | `CF_ACCOUNT_ID` | the account to meter | | `CF_ANALYTICS_TOKEN` | `sk-meter-analytics-read`: Account Analytics Read, nothing else (setkernel-cloudflare tofu) | -| `ALERT_WEBHOOK_URL` | `https://alerts.setkernel.net/meter` (https only; anything else is refused) | -| `ALERT_WEBHOOK_TOKEN` | bearer the receiver checks | -| `ALERT_ACCESS_CLIENT_ID`, `ALERT_ACCESS_CLIENT_SECRET` | the `sk-meter-alerts` Access service token | +| `ALERT_WEBHOOK_URL` | `https://setkernel-ops.setkernel.workers.dev/alert`, the ops Worker's email relay (https only; anything else is refused) | +| `ALERT_WEBHOOK_TOKEN` | the ops Worker's `OPS_ALERT_TOKEN` (Infisical `hq-workers/prod`, folder `/ops`), copied, never retyped | +| `ALERT_ACCESS_CLIENT_ID`, `ALERT_ACCESS_CLIENT_SECRET` | unused since the relay has no Access app; delete them with the `sk-meter-alerts` token | + +Until 2026-11-09 the URL was `https://alerts.setkernel.net/meter`, the buzz-alerts bridge on +sk-obs01, which hq#1261 removed. An alert that fails is retried every hour, so the alert that +was due arrives on the first run after the two values change. The body carries both shapes +(`src/notify.js`), so the change needs no deploy of its own. The KV namespace `sk-meter` (`f79ca12aeb9347f9851f5dfd53313f39`) is tofu-owned in setkernel-cloudflare: this repo binds it and never creates or deletes it. diff --git a/workers/meter/src/notify.js b/workers/meter/src/notify.js index d8f09c8..5aa33ae 100644 --- a/workers/meter/src/notify.js +++ b/workers/meter/src/notify.js @@ -1,10 +1,19 @@ -// Alert delivery. A Worker can't run the buzz CLI or hold a Buzz identity the way hq's -// observability bridge does, so it POSTs { source, text, mention } to ALERT_WEBHOOK_URL (a Worker -// secret) with ALERT_WEBHOOK_TOKEN as a bearer token, and the receiver sends it to Buzz. The -// receiver is the buzz-alerts bridge's /meter route on sk-obs01 (hq#222, observability/README.md), -// behind Cloudflare Access: ALERT_ACCESS_CLIENT_ID and ALERT_ACCESS_CLIENT_SECRET are the -// sk-meter-alerts service token. A caller may name its own `source`. Without a webhook the alert is logged and the KV flags still act; -// the guard never depends on the alert arriving. +// Alert delivery. sk-meter POSTs one JSON body to ALERT_WEBHOOK_URL (a Worker secret) with +// ALERT_WEBHOOK_TOKEN as a bearer token. The body carries two shapes at once, so either receiver reads it: +// { source, text, mention } the old buzz-alerts bridge's /meter route on sk-obs01 (removed in hq#1261) +// { title, message } the ops Worker's POST /alert (setkernel/hq workers/ops), which emails +// `title` as the subject and `message` as the body (plan v6 E3b: alerts are email) +// ALERT_ACCESS_CLIENT_ID and ALERT_ACCESS_CLIENT_SECRET, when both are set, are sent as a Cloudflare +// Access service token (the old route was behind Access; the ops Worker's is not). A caller may name its +// own `source`. Without a webhook the alert is logged and the KV flags still act; the guard never +// depends on the alert arriving. + +// The subject line is the source and the alert's first line, the body its whole text. +export function payload(alert) { + const source = alert.source ?? "sk-meter"; + const first = String(alert.text ?? "").split("\n").find((l) => l.trim()) ?? "alert"; + return { source, text: alert.text, mention: alert.mention, title: `${source}: ${first.trim()}`.slice(0, 200), message: alert.text }; +} export async function notify(env, alert, fetchImpl = fetch) { const url = env.ALERT_WEBHOOK_URL; @@ -26,7 +35,7 @@ export async function notify(env, alert, fetchImpl = fetch) { // Access answers a refused token with a redirect to its login page; following it could end in // a 200 and count the alert as delivered. A redirect is a failure, retried next hour. redirect: "manual", - body: JSON.stringify({ source: alert.source ?? "sk-meter", text: alert.text, mention: alert.mention }), + body: JSON.stringify(payload(alert)), }); if (!res.ok) throw new Error(`alert webhook answered HTTP ${res.status}`); return true; diff --git a/workers/meter/test/notify.test.js b/workers/meter/test/notify.test.js index a33dd24..f75c2e1 100644 --- a/workers/meter/test/notify.test.js +++ b/workers/meter/test/notify.test.js @@ -2,7 +2,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { notify } from "../src/notify.js"; +import { notify, payload } from "../src/notify.js"; const URL = "https://alerts.example.test/meter"; const ALERT = { text: "x", mention: false }; @@ -39,7 +39,7 @@ test("an Access redirect to its login page is a failure, so the alert is retried await assert.rejects(notify({ ALERT_WEBHOOK_URL: URL }, ALERT, capture(302)), /HTTP 302/); }); -test("the receiver's contract: POST { source, text, mention } as JSON, and its 204 counts as delivered", async () => { +test("the receiver's contract: POST { source, text, mention, title, message } as JSON, and its 204 counts as delivered", async () => { const f = capture(204); const env = { ALERT_WEBHOOK_URL: "https://alerts.setkernel.net/meter", ALERT_WEBHOOK_TOKEN: "t" }; assert.equal(await notify(env, { text: "line 1\nline 2", mention: true }, f), true); @@ -47,7 +47,15 @@ test("the receiver's contract: POST { source, text, mention } as JSON, and its 2 assert.equal(url, "https://alerts.setkernel.net/meter"); assert.equal(init.method, "POST"); assert.equal(init.headers["content-type"], "application/json"); - assert.deepEqual(JSON.parse(init.body), { source: "sk-meter", text: "line 1\nline 2", mention: true }); + assert.deepEqual(JSON.parse(init.body), { source: "sk-meter", text: "line 1\nline 2", mention: true, title: "sk-meter: line 1", message: "line 1\nline 2" }); +}); + +test("the ops Worker's /alert reads title as the subject and message as the body", () => { + assert.deepEqual(payload({ text: "\n DEGRADE: queues_ops 85%\nmore", mention: true }), { + source: "sk-meter", text: "\n DEGRADE: queues_ops 85%\nmore", mention: true, title: "sk-meter: DEGRADE: queues_ops 85%", message: "\n DEGRADE: queues_ops 85%\nmore", + }); + assert.equal(payload({ text: "x".repeat(500) }).title.length, 200); + assert.equal(payload({ text: "" }).title, "sk-meter: alert"); }); test("an http:// URL is refused before anything is sent, so no secret leaves in the clear", async () => { diff --git a/workers/meter/wrangler.toml b/workers/meter/wrangler.toml index 7322970..31d2cd2 100644 --- a/workers/meter/wrangler.toml +++ b/workers/meter/wrangler.toml @@ -27,7 +27,6 @@ id = "f79ca12aeb9347f9851f5dfd53313f39" # BILLING_DAY the day of the month the billing cycle starts (13); required # CF_ACCOUNT_ID the account to meter # CF_ANALYTICS_TOKEN Account Analytics: Read, on this account only -# ALERT_WEBHOOK_URL https://alerts.setkernel.net/meter, forwards { source, text, mention } to Buzz -# ALERT_WEBHOOK_TOKEN bearer token that endpoint checks -# ALERT_ACCESS_CLIENT_ID the sk-meter-alerts Access service token in front of that -# ALERT_ACCESS_CLIENT_SECRET endpoint (hq observability/README.md, "Meter alerts") +# ALERT_WEBHOOK_URL the ops Worker's POST /alert, which emails { title, message } (README.md) +# ALERT_WEBHOOK_TOKEN its OPS_ALERT_TOKEN bearer +# ALERT_ACCESS_CLIENT_ID, ALERT_ACCESS_CLIENT_SECRET unused (the old route's Access token)