From 6963c6f8cb18adfc368de8a8f695ff6184ecf3ed Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 20 Nov 2026 12:13:37 +0000 Subject: [PATCH 1/2] feat: add alert function templates Three Node templates that act on monitor alert events: alerts-to-chat posts firing and resolved alerts to Slack, Discord, Teams or Google Chat; pause-on-usage-spike disables the function or site a usage monitor is scoped to; uptime-incident-log records uptime alerts as incident rows and notifies a Messaging topic. --- node/alerts-to-chat/.gitignore | 133 +++++++++++++ node/alerts-to-chat/.prettierrc.json | 6 + node/alerts-to-chat/README.md | 69 +++++++ node/alerts-to-chat/package-lock.json | 34 ++++ node/alerts-to-chat/package.json | 16 ++ node/alerts-to-chat/src/chat.js | 203 ++++++++++++++++++++ node/alerts-to-chat/src/main.js | 39 ++++ node/pause-on-usage-spike/.gitignore | 133 +++++++++++++ node/pause-on-usage-spike/.prettierrc.json | 6 + node/pause-on-usage-spike/README.md | 68 +++++++ node/pause-on-usage-spike/package-lock.json | 77 ++++++++ node/pause-on-usage-spike/package.json | 19 ++ node/pause-on-usage-spike/src/main.js | 85 ++++++++ node/uptime-incident-log/.gitignore | 133 +++++++++++++ node/uptime-incident-log/.prettierrc.json | 6 + node/uptime-incident-log/README.md | 82 ++++++++ node/uptime-incident-log/package-lock.json | 77 ++++++++ node/uptime-incident-log/package.json | 19 ++ node/uptime-incident-log/src/incidents.js | 89 +++++++++ node/uptime-incident-log/src/main.js | 49 +++++ node/uptime-incident-log/src/notify.js | 77 ++++++++ 21 files changed, 1420 insertions(+) create mode 100644 node/alerts-to-chat/.gitignore create mode 100644 node/alerts-to-chat/.prettierrc.json create mode 100644 node/alerts-to-chat/README.md create mode 100644 node/alerts-to-chat/package-lock.json create mode 100644 node/alerts-to-chat/package.json create mode 100644 node/alerts-to-chat/src/chat.js create mode 100644 node/alerts-to-chat/src/main.js create mode 100644 node/pause-on-usage-spike/.gitignore create mode 100644 node/pause-on-usage-spike/.prettierrc.json create mode 100644 node/pause-on-usage-spike/README.md create mode 100644 node/pause-on-usage-spike/package-lock.json create mode 100644 node/pause-on-usage-spike/package.json create mode 100644 node/pause-on-usage-spike/src/main.js create mode 100644 node/uptime-incident-log/.gitignore create mode 100644 node/uptime-incident-log/.prettierrc.json create mode 100644 node/uptime-incident-log/README.md create mode 100644 node/uptime-incident-log/package-lock.json create mode 100644 node/uptime-incident-log/package.json create mode 100644 node/uptime-incident-log/src/incidents.js create mode 100644 node/uptime-incident-log/src/main.js create mode 100644 node/uptime-incident-log/src/notify.js diff --git a/node/alerts-to-chat/.gitignore b/node/alerts-to-chat/.gitignore new file mode 100644 index 00000000..46afb6b3 --- /dev/null +++ b/node/alerts-to-chat/.gitignore @@ -0,0 +1,133 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# Directory used by Appwrite CLI for local development +.appwrite \ No newline at end of file diff --git a/node/alerts-to-chat/.prettierrc.json b/node/alerts-to-chat/.prettierrc.json new file mode 100644 index 00000000..0a725205 --- /dev/null +++ b/node/alerts-to-chat/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true +} diff --git a/node/alerts-to-chat/README.md b/node/alerts-to-chat/README.md new file mode 100644 index 00000000..a322b5b3 --- /dev/null +++ b/node/alerts-to-chat/README.md @@ -0,0 +1,69 @@ +# πŸ’¬ Node.js Alerts to Chat Function + +Posts every firing and resolved monitor alert to Slack, Discord, Microsoft Teams or Google Chat. + +## 🧰 Usage + +### Event: `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved` + +Receives the alert as the event payload, formats it for the chat app behind `CHAT_WEBHOOK_URL` and posts it. Usage alerts show the metric, value, threshold and peak; uptime alerts show the check and what it found. A resolved alert is a new message with how long it fired. + +Calls that aren't alert events, such as an HTTP request to the function's domain, get a `403`. + +**Response** + +Sample `200` Response: + +```json +{ "ok": true, "target": "slack" } +``` + +Sample `502` Response, when the chat app refuses the message: + +```json +{ "ok": false, "target": "slack", "status": 404 } +``` + +## βš™οΈ Configuration + +| Setting | Value | +| ----------------- | ------------------------------------------------------------ | +| Runtime | Node (18.0) | +| Entrypoint | `src/main.js` | +| Build Commands | `npm install` | +| Permissions | None | +| Events | `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved` | +| Timeout (Seconds) | 15 | + +## πŸ”’ Environment Variables + +### CHAT_WEBHOOK_URL + +Incoming webhook URL of the channel to post to. The host picks the format: + +| Host | Chat app | +| ---------------------------------------------------------- | --------------- | +| `hooks.slack.com` | Slack | +| `discord.com/api/webhooks/…` | Discord | +| `*.environment.api.powerplatform.com`, `*.logic.azure.com` | Microsoft Teams | +| `chat.googleapis.com` | Google Chat | + +| Question | Answer | +| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Required | Yes | +| Sample Value | `https://hooks.slack.com/services/T000/B000/XXXX` | +| Documentation | [Slack](https://api.slack.com/messaging/webhooks), [Discord](https://support.discord.com/hc/en-us/articles/228383668), [Teams](https://support.microsoft.com/en-us/office/create-incoming-webhooks-with-workflows-for-microsoft-teams-8ae491c7-0394-4861-ba59-055e33f75498), [Google Chat](https://developers.google.com/workspace/chat/quickstart/webhooks) | + +### CHAT_TARGET + +The chat app to format for when the webhook URL's host doesn't say, such as a proxy: `slack`, `discord`, `teams` or `google-chat`. + +| Question | Answer | +| ------------ | ------- | +| Required | No | +| Sample Value | `slack` | + +## πŸ“ Notes + +- Events are delivered at least once, so a chat app can rarely show the same alert twice. +- Microsoft Teams Office 365 connector URLs (`*.webhook.office.com`) are retired. Create the webhook with the Workflows app instead. diff --git a/node/alerts-to-chat/package-lock.json b/node/alerts-to-chat/package-lock.json new file mode 100644 index 00000000..80b54e2e --- /dev/null +++ b/node/alerts-to-chat/package-lock.json @@ -0,0 +1,34 @@ +{ + "name": "alerts-to-chat", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "alerts-to-chat", + "version": "1.0.0", + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=18.17.0" + } + }, + "node_modules/prettier": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.10.1.tgz", + "integrity": "sha512-70vp3ZUYoPlFbF9uiTdXHUUCGEIp7d4+6p+8DYbkm2jP7x/3i/paXVF6dopSI3zd/CEitrZN1rc4i85xWmmXlg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + } + } +} diff --git a/node/alerts-to-chat/package.json b/node/alerts-to-chat/package.json new file mode 100644 index 00000000..89239062 --- /dev/null +++ b/node/alerts-to-chat/package.json @@ -0,0 +1,16 @@ +{ + "name": "alerts-to-chat", + "version": "1.0.0", + "description": "", + "main": "src/main.js", + "type": "module", + "scripts": { + "format": "prettier --write ." + }, + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=18.17.0" + } +} diff --git a/node/alerts-to-chat/src/chat.js b/node/alerts-to-chat/src/chat.js new file mode 100644 index 00000000..141ee42e --- /dev/null +++ b/node/alerts-to-chat/src/chat.js @@ -0,0 +1,203 @@ +const TARGETS = ['slack', 'discord', 'teams', 'google-chat']; + +const COMPARATORS = { + greaterThan: '>', + greaterThanEqual: 'β‰₯', + lessThan: '<', + lessThanEqual: '≀', +}; + +const COLORS = { + firing: { hex: '#E5484D', teams: 'Attention' }, + resolved: { hex: '#30A46C', teams: 'Good' }, +}; + +/** + * Picks the chat app from the webhook URL's host, unless CHAT_TARGET names one. + */ +export function detectTarget(url, override) { + if (override) { + if (!TARGETS.includes(override)) { + throw new Error(`CHAT_TARGET must be one of: ${TARGETS.join(', ')}`); + } + return override; + } + + const { hostname, pathname } = new URL(url); + if (hostname === 'hooks.slack.com') { + return 'slack'; + } + if ( + /(^|\.)discord(app)?\.com$/.test(hostname) && + pathname.startsWith('/api/webhooks/') + ) { + return 'discord'; + } + if (hostname === 'chat.googleapis.com') { + return 'google-chat'; + } + if ( + hostname.endsWith('.api.powerplatform.com') || + hostname.endsWith('.logic.azure.com') + ) { + return 'teams'; + } + + throw new Error( + `Can't tell which chat app ${hostname} is. Set CHAT_TARGET to one of: ${TARGETS.join(', ')}` + ); +} + +function number(value) { + return Number(value ?? 0).toLocaleString('en-US', { + maximumFractionDigits: 2, + }); +} + +function duration(from, to) { + const seconds = Math.max( + 0, + Math.round((new Date(to) - new Date(from)) / 1000) + ); + const hours = Math.floor(seconds / 3600); + const minutes = Math.floor((seconds % 3600) / 60); + if (hours > 0) { + return `${hours}h ${minutes}m`; + } + if (minutes > 0) { + return `${minutes}m ${seconds % 60}s`; + } + return `${seconds}s`; +} + +/** + * Turns an alert into a title and the facts every chat app shows. + * Usage alerts compare a metric with a threshold; probe alerts report what the check found. + */ +export function describe(alert) { + const firing = alert.status === 'firing'; + const facts = []; + + if (alert.metric) { + const comparator = COMPARATORS[alert.comparator] ?? alert.comparator; + facts.push(['Metric', alert.metric]); + facts.push([ + 'Value', + `${number(alert.value)} ${comparator} ${number(alert.threshold)}`, + ]); + if (alert.peak !== undefined && alert.peak !== alert.value) { + facts.push(['Peak', number(alert.peak)]); + } + } else { + facts.push(['Check', String(alert.type ?? '').toUpperCase()]); + if (alert.result) { + facts.push(['Result', alert.result]); + } + if (alert.value) { + facts.push(['Latency', `${number(alert.value)} ms`]); + } + } + + if (alert.resourceType && alert.resourceType !== 'project') { + facts.push(['Resource', `${alert.resourceType} ${alert.resourceId}`]); + } + if (alert.severity) { + facts.push(['Severity', alert.severity]); + } + facts.push(['Started', new Date(alert.$createdAt).toUTCString()]); + if (!firing && alert.resolvedAt) { + facts.push(['Duration', duration(alert.$createdAt, alert.resolvedAt)]); + } + facts.push(['Project', process.env.APPWRITE_FUNCTION_PROJECT_ID ?? '']); + + return { + status: alert.status, + title: `${firing ? 'πŸ”΄ Firing' : 'βœ… Resolved'}: ${alert.name}`, + facts: facts.filter(([, value]) => value !== ''), + }; +} + +/** + * Builds the webhook body each chat app expects. + */ +export function formatMessage(target, { status, title, facts }) { + const color = COLORS[status]; + + switch (target) { + case 'slack': + return { + text: title, + attachments: [ + { + color: color.hex, + blocks: [ + { + type: 'section', + text: { type: 'mrkdwn', text: `*${title}*` }, + }, + { + type: 'section', + fields: facts.map(([name, value]) => ({ + type: 'mrkdwn', + text: `*${name}*\n${value}`, + })), + }, + ], + }, + ], + }; + case 'discord': + return { + embeds: [ + { + title, + color: parseInt(color.hex.slice(1), 16), + fields: facts.map(([name, value]) => ({ + name, + value: String(value), + inline: true, + })), + timestamp: new Date().toISOString(), + }, + ], + }; + case 'teams': + return { + type: 'message', + attachments: [ + { + contentType: 'application/vnd.microsoft.card.adaptive', + content: { + $schema: 'http://adaptivecards.io/schemas/adaptive-card.json', + type: 'AdaptiveCard', + version: '1.4', + body: [ + { + type: 'TextBlock', + text: title, + weight: 'Bolder', + size: 'Medium', + color: color.teams, + wrap: true, + }, + { + type: 'FactSet', + facts: facts.map(([name, value]) => ({ + title: name, + value: String(value), + })), + }, + ], + }, + }, + ], + }; + case 'google-chat': + return { + text: [ + `*${title}*`, + ...facts.map(([name, value]) => `${name}: ${value}`), + ].join('\n'), + }; + } +} diff --git a/node/alerts-to-chat/src/main.js b/node/alerts-to-chat/src/main.js new file mode 100644 index 00000000..7ff28591 --- /dev/null +++ b/node/alerts-to-chat/src/main.js @@ -0,0 +1,39 @@ +import { describe, detectTarget, formatMessage } from './chat.js'; + +export default async ({ req, res, log, error }) => { + // Only alert events carry a real alert. Refuse HTTP calls, which anyone + // who can reach the function's domain could forge. + if (req.headers['x-appwrite-trigger'] !== 'event') { + return res.json( + { ok: false, message: 'Only alert events trigger this function.' }, + 403 + ); + } + + const alert = req.bodyJson; + + if (!alert?.monitorId || !['firing', 'resolved'].includes(alert.status)) { + log('Ignored: not a firing or resolved alert.'); + return res.empty(); + } + + const url = process.env.CHAT_WEBHOOK_URL; + if (!url) { + throw new Error('Missing required variable: CHAT_WEBHOOK_URL'); + } + + const target = detectTarget(url, process.env.CHAT_TARGET); + const response = await fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(formatMessage(target, describe(alert))), + }); + + if (!response.ok) { + error(`${target} answered ${response.status}: ${await response.text()}`); + return res.json({ ok: false, target, status: response.status }, 502); + } + + log(`Posted ${alert.status} alert "${alert.name}" to ${target}.`); + return res.json({ ok: true, target }); +}; diff --git a/node/pause-on-usage-spike/.gitignore b/node/pause-on-usage-spike/.gitignore new file mode 100644 index 00000000..46afb6b3 --- /dev/null +++ b/node/pause-on-usage-spike/.gitignore @@ -0,0 +1,133 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# Directory used by Appwrite CLI for local development +.appwrite \ No newline at end of file diff --git a/node/pause-on-usage-spike/.prettierrc.json b/node/pause-on-usage-spike/.prettierrc.json new file mode 100644 index 00000000..0a725205 --- /dev/null +++ b/node/pause-on-usage-spike/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true +} diff --git a/node/pause-on-usage-spike/README.md b/node/pause-on-usage-spike/README.md new file mode 100644 index 00000000..78122a76 --- /dev/null +++ b/node/pause-on-usage-spike/README.md @@ -0,0 +1,68 @@ +# ⏸️ Node.js Pause on Usage Spike Function + +Disables a function or site when a usage monitor scoped to it fires, so a runaway cron, retry loop or traffic spike stops running up the bill. + +## 🧰 Usage + +Pair it with a usage monitor scoped to the resource to protect, for example: + +```js +await monitors.createUsageMonitor({ + monitorId: ID.unique(), + name: 'Image resizer executions', + rules: [ + Query.greaterThan('executions', 10000), + Query.equal('resourceType', ['function']), + Query.equal('resourceId', ['image-resizer']), + ], + window: 3600, + severity: 'critical', +}); +``` + +### Event: `monitors.*.alerts.*.firing` + +When the alert names a function or site and its severity is at least `MIN_SEVERITY`, the template sets that resource's `enabled` to `false`. It leaves alone: + +- project-wide alerts and alerts on databases or buckets, +- alerts about this function itself, +- alerts below `MIN_SEVERITY`. + +It doesn't re-enable the resource when the alert resolves: usage drops because the resource is off, so turning it back on would only start the spike again. Re-enable it from the console once you've found the cause. + +Calls that aren't alert events, such as an HTTP request to the function's domain, get a `403`. + +**Response** + +Sample `200` Response: + +```json +{ "paused": true, "resourceType": "function", "resourceId": "image-resizer" } +``` + +```json +{ "paused": false, "reason": "scope" } +``` + +## βš™οΈ Configuration + +| Setting | Value | +| ----------------- | ---------------------------------------------------------------- | +| Runtime | Node (20.0) | +| Entrypoint | `src/main.js` | +| Build Commands | `npm install` | +| Permissions | None | +| Events | `monitors.*.alerts.*.firing` | +| Timeout (Seconds) | 15 | +| Scopes | `functions.read`, `functions.write`, `sites.read`, `sites.write` | + +## πŸ”’ Environment Variables + +### MIN_SEVERITY + +Lowest monitor severity that pauses its resource: `info`, `warning`, `error` or `critical`. Monitors default to `warning`. + +| Question | Answer | +| ------------ | --------- | +| Required | No | +| Sample Value | `warning` | diff --git a/node/pause-on-usage-spike/package-lock.json b/node/pause-on-usage-spike/package-lock.json new file mode 100644 index 00000000..e58a45ba --- /dev/null +++ b/node/pause-on-usage-spike/package-lock.json @@ -0,0 +1,77 @@ +{ + "name": "pause-on-usage-spike", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "pause-on-usage-spike", + "version": "1.0.0", + "dependencies": { + "node-appwrite": "^32.0.0" + }, + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/bignumber.js": { + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz", + "integrity": "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==", + "license": "MIT", + "engines": { + "node": "*" + } + }, + "node_modules/json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "license": "MIT", + "dependencies": { + "bignumber.js": "^9.0.0" + } + }, + "node_modules/node-appwrite": { + "version": "32.0.0", + "resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-32.0.0.tgz", + "integrity": "sha512-zPLFzNl2RANN9Tg0wTkvEiNBY/eYhExnGADA8jvmh3cq0x2kZ1MI96zLTWlF7+g+vl0dU+NfmEvcTZa8GO6woA==", + "license": "BSD-3-Clause", + "dependencies": { + "json-bigint": "1.0.0", + "undici": "^6.27.0" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/prettier": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.10.1.tgz", + "integrity": "sha512-70vp3ZUYoPlFbF9uiTdXHUUCGEIp7d4+6p+8DYbkm2jP7x/3i/paXVF6dopSI3zd/CEitrZN1rc4i85xWmmXlg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/undici": { + "version": "6.29.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.1.tgz", + "integrity": "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA==", + "license": "MIT", + "engines": { + "node": ">=18.17" + } + } + } +} diff --git a/node/pause-on-usage-spike/package.json b/node/pause-on-usage-spike/package.json new file mode 100644 index 00000000..b6c2f4f1 --- /dev/null +++ b/node/pause-on-usage-spike/package.json @@ -0,0 +1,19 @@ +{ + "name": "pause-on-usage-spike", + "version": "1.0.0", + "description": "", + "main": "src/main.js", + "type": "module", + "scripts": { + "format": "prettier --write ." + }, + "dependencies": { + "node-appwrite": "^32.0.0" + }, + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=20.0.0" + } +} diff --git a/node/pause-on-usage-spike/src/main.js b/node/pause-on-usage-spike/src/main.js new file mode 100644 index 00000000..b61e2e68 --- /dev/null +++ b/node/pause-on-usage-spike/src/main.js @@ -0,0 +1,85 @@ +import { Client, Functions, Sites } from 'node-appwrite'; + +const SEVERITIES = ['info', 'warning', 'error', 'critical']; + +export default async ({ req, res, log }) => { + // Only alert events carry a real alert. Refuse HTTP calls, which anyone + // who can reach the function's domain could forge. + if (req.headers['x-appwrite-trigger'] !== 'event') { + return res.json( + { ok: false, message: 'Only alert events trigger this function.' }, + 403 + ); + } + + const alert = req.bodyJson; + + if (alert?.status !== 'firing' || alert.type !== 'usage') { + log('Ignored: not a firing usage alert.'); + return res.json({ paused: false, reason: 'not-firing-usage' }); + } + + const minimum = process.env.MIN_SEVERITY || 'warning'; + if (!SEVERITIES.includes(minimum)) { + throw new Error(`MIN_SEVERITY must be one of: ${SEVERITIES.join(', ')}`); + } + if (SEVERITIES.indexOf(alert.severity) < SEVERITIES.indexOf(minimum)) { + log(`Ignored: severity ${alert.severity} is below ${minimum}.`); + return res.json({ paused: false, reason: 'severity' }); + } + + // Never pause the function that is doing the pausing. + if (alert.resourceId === process.env.APPWRITE_FUNCTION_ID) { + log('Ignored: the alert is about this function.'); + return res.json({ paused: false, reason: 'self' }); + } + + const client = new Client() + .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT) + .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) + .setKey(req.headers['x-appwrite-key']); + + // Only a monitor scoped to one function or site names something to pause. + // Disabling twice is harmless, so a repeated event changes nothing. + switch (alert.resourceType) { + case 'function': { + const functions = new Functions(client); + const resource = await functions.get({ functionId: alert.resourceId }); + if (resource.enabled) { + await functions.update({ + functionId: resource.$id, + name: resource.name, + enabled: false, + }); + } + break; + } + case 'site': { + const sites = new Sites(client); + const resource = await sites.get({ siteId: alert.resourceId }); + if (resource.enabled) { + await sites.update({ + siteId: resource.$id, + name: resource.name, + framework: resource.framework, + enabled: false, + }); + } + break; + } + default: + log( + `Ignored: "${alert.name}" watches a ${alert.resourceType}, not a function or site.` + ); + return res.json({ paused: false, reason: 'scope' }); + } + + log( + `Paused ${alert.resourceType} ${alert.resourceId}: "${alert.name}" fired at ${alert.value} against ${alert.threshold}.` + ); + return res.json({ + paused: true, + resourceType: alert.resourceType, + resourceId: alert.resourceId, + }); +}; diff --git a/node/uptime-incident-log/.gitignore b/node/uptime-incident-log/.gitignore new file mode 100644 index 00000000..46afb6b3 --- /dev/null +++ b/node/uptime-incident-log/.gitignore @@ -0,0 +1,133 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# Directory used by Appwrite CLI for local development +.appwrite \ No newline at end of file diff --git a/node/uptime-incident-log/.prettierrc.json b/node/uptime-incident-log/.prettierrc.json new file mode 100644 index 00000000..0a725205 --- /dev/null +++ b/node/uptime-incident-log/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true +} diff --git a/node/uptime-incident-log/README.md b/node/uptime-incident-log/README.md new file mode 100644 index 00000000..2e9eee4c --- /dev/null +++ b/node/uptime-incident-log/README.md @@ -0,0 +1,82 @@ +# 🚨 Node.js Uptime Incident Log Function + +Turns uptime monitor alerts (HTTP, DNS, TCP and SMTP checks) into incident rows in a table, and tells a Messaging topic's subscribers when a service goes down and comes back. + +## 🧰 Usage + +### Event: `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved` + +On the first alert, the template creates the `status` database and its `incidents` table. Then it keeps one row per alert, under the alert's ID: + +| Column | Value | +| ----------------- | ----------------------------------------------- | +| `monitorId` | Monitor that raised the alert | +| `name` | Monitor name | +| `type` | `http`, `dns`, `tcp` or `smtp` | +| `status` | `firing`, then `resolved` | +| `severity` | Monitor severity | +| `result` | What the failing check found, such as `timeout` | +| `startedAt` | When the alert fired | +| `resolvedAt` | When it resolved | +| `downtimeSeconds` | How long it was down | + +A status page can list the rows, or subscribe to them with Realtime. The table is private: grant `read("any")` on it from the console to read it from a public page. + +When `MESSAGING_TOPIC_ID` is set, each change is also sent to that topic, by email, SMS and push, each only if the topic has subscribers of that kind. + +Usage alerts are ignored. Calls that aren't alert events, such as an HTTP request to the function's domain, get a `403`. + +**Response** + +Sample `200` Response: + +```json +{ "recorded": true, "incident": "6745f0a1c3e2b7d4a9f0", "notified": ["email"] } +``` + +## βš™οΈ Configuration + +| Setting | Value | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| Runtime | Node (20.0) | +| Entrypoint | `src/main.js` | +| Build Commands | `npm install` | +| Permissions | None | +| Events | `monitors.*.alerts.*.firing`, `monitors.*.alerts.*.resolved` | +| Timeout (Seconds) | 15 | +| Scopes | `databases.read`, `databases.write`, `tables.read`, `tables.write`, `rows.read`, `rows.write`, `topics.read`, `messages.write` | + +## πŸ”’ Environment Variables + +### APPWRITE_DATABASE_ID + +ID of the database for the incidents table. Created if it doesn't exist. + +| Question | Answer | +| ------------ | -------- | +| Required | No | +| Sample Value | `status` | + +### APPWRITE_TABLE_ID + +ID of the incidents table. Created if it doesn't exist. + +| Question | Answer | +| ------------ | ----------- | +| Required | No | +| Sample Value | `incidents` | + +### MESSAGING_TOPIC_ID + +ID of the Messaging topic to notify. Leave it empty to only record incidents. + +| Question | Answer | +| ------------- | ---------------------------------------------------------------------- | +| Required | No | +| Sample Value | `status-updates` | +| Documentation | [Appwrite: Topics](https://appwrite.io/docs/products/messaging/topics) | + +## πŸ“ Notes + +- Events are delivered at least once. A repeated event updates the same row, and each message has a fixed ID per incident and state, so subscribers aren't notified twice. +- A late firing event never reopens a resolved incident. diff --git a/node/uptime-incident-log/package-lock.json b/node/uptime-incident-log/package-lock.json new file mode 100644 index 00000000..aca58424 --- /dev/null +++ b/node/uptime-incident-log/package-lock.json @@ -0,0 +1,77 @@ +{ + "name": "uptime-incident-log", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "uptime-incident-log", + "version": "1.0.0", + "dependencies": { + "node-appwrite": "^32.0.0" + }, + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/bignumber.js": { + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.3.1.tgz", + "integrity": "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ==", + "license": "MIT", + "engines": { + "node": "*" + } + }, + "node_modules/json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "license": "MIT", + "dependencies": { + "bignumber.js": "^9.0.0" + } + }, + "node_modules/node-appwrite": { + "version": "32.0.0", + "resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-32.0.0.tgz", + "integrity": "sha512-zPLFzNl2RANN9Tg0wTkvEiNBY/eYhExnGADA8jvmh3cq0x2kZ1MI96zLTWlF7+g+vl0dU+NfmEvcTZa8GO6woA==", + "license": "BSD-3-Clause", + "dependencies": { + "json-bigint": "1.0.0", + "undici": "^6.27.0" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/prettier": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.10.1.tgz", + "integrity": "sha512-70vp3ZUYoPlFbF9uiTdXHUUCGEIp7d4+6p+8DYbkm2jP7x/3i/paXVF6dopSI3zd/CEitrZN1rc4i85xWmmXlg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/undici": { + "version": "6.29.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.29.1.tgz", + "integrity": "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA==", + "license": "MIT", + "engines": { + "node": ">=18.17" + } + } + } +} diff --git a/node/uptime-incident-log/package.json b/node/uptime-incident-log/package.json new file mode 100644 index 00000000..0d777c1f --- /dev/null +++ b/node/uptime-incident-log/package.json @@ -0,0 +1,19 @@ +{ + "name": "uptime-incident-log", + "version": "1.0.0", + "description": "", + "main": "src/main.js", + "type": "module", + "scripts": { + "format": "prettier --write ." + }, + "dependencies": { + "node-appwrite": "^32.0.0" + }, + "devDependencies": { + "prettier": "^3.2.5" + }, + "engines": { + "node": ">=20.0.0" + } +} diff --git a/node/uptime-incident-log/src/incidents.js b/node/uptime-incident-log/src/incidents.js new file mode 100644 index 00000000..388f5266 --- /dev/null +++ b/node/uptime-incident-log/src/incidents.js @@ -0,0 +1,89 @@ +const COLUMNS = [ + { key: 'monitorId', type: 'varchar', size: 36, required: true }, + { key: 'name', type: 'varchar', size: 128, required: true }, + { key: 'type', type: 'varchar', size: 16, required: true }, + { key: 'status', type: 'varchar', size: 16, required: true }, + { key: 'severity', type: 'varchar', size: 16, required: false }, + { key: 'result', type: 'varchar', size: 64, required: false }, + { key: 'startedAt', type: 'datetime', required: true }, + { key: 'resolvedAt', type: 'datetime', required: false }, + { key: 'downtimeSeconds', type: 'integer', required: false }, +]; + +async function ignoreConflict(promise) { + try { + return await promise; + } catch (err) { + if (err.code !== 409) { + throw err; + } + } +} + +/** + * Creates the database and the incidents table on the first alert. + */ +export async function ensureTable(tablesDB, databaseId, tableId) { + try { + await tablesDB.getTable({ databaseId, tableId }); + return; + } catch (err) { + if (err.code !== 404) { + throw err; + } + } + + await ignoreConflict(tablesDB.create({ databaseId, name: 'Status' })); + await ignoreConflict( + tablesDB.createTable({ + databaseId, + tableId, + name: 'Incidents', + columns: COLUMNS, + }) + ); +} + +/** + * Keeps one row per alert, under the alert's ID, so a repeated event updates + * the same incident instead of opening another. + */ +export async function recordIncident(tablesDB, databaseId, tableId, alert) { + const rowId = alert.$id; + const data = { + monitorId: alert.monitorId, + name: alert.name, + type: alert.type, + severity: alert.severity ?? null, + result: alert.result ?? null, + startedAt: alert.$createdAt, + }; + + if (alert.status === 'resolved') { + const downtime = Math.round( + (new Date(alert.resolvedAt) - new Date(alert.$createdAt)) / 1000 + ); + return tablesDB.upsertRow({ + databaseId, + tableId, + rowId, + data: { + ...data, + status: 'resolved', + resolvedAt: alert.resolvedAt, + downtimeSeconds: Math.max(0, downtime), + }, + }); + } + + // A late or repeated firing event must not reopen a resolved incident. + const created = await ignoreConflict( + tablesDB.createRow({ + databaseId, + tableId, + rowId, + data: { ...data, status: 'firing' }, + }) + ); + return created ?? tablesDB.getRow({ databaseId, tableId, rowId }); +} diff --git a/node/uptime-incident-log/src/main.js b/node/uptime-incident-log/src/main.js new file mode 100644 index 00000000..7c2e85de --- /dev/null +++ b/node/uptime-incident-log/src/main.js @@ -0,0 +1,49 @@ +import { Client, Messaging, TablesDB } from 'node-appwrite'; +import { ensureTable, recordIncident } from './incidents.js'; +import { notifySubscribers } from './notify.js'; + +const PROBES = ['http', 'dns', 'tcp', 'smtp']; + +export default async ({ req, res, log }) => { + // Only alert events carry a real alert. Refuse HTTP calls, which anyone + // who can reach the function's domain could forge. + if (req.headers['x-appwrite-trigger'] !== 'event') { + return res.json( + { ok: false, message: 'Only alert events trigger this function.' }, + 403 + ); + } + + const alert = req.bodyJson; + + if ( + !PROBES.includes(alert?.type) || + !['firing', 'resolved'].includes(alert.status) + ) { + log('Ignored: not a firing or resolved uptime alert.'); + return res.json({ recorded: false }); + } + + const client = new Client() + .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT) + .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) + .setKey(req.headers['x-appwrite-key']); + + const tablesDB = new TablesDB(client); + const databaseId = process.env.APPWRITE_DATABASE_ID || 'status'; + const tableId = process.env.APPWRITE_TABLE_ID || 'incidents'; + + await ensureTable(tablesDB, databaseId, tableId); + const incident = await recordIncident(tablesDB, databaseId, tableId, alert); + log(`Incident ${incident.$id} for "${alert.name}" is ${incident.status}.`); + + const topicId = process.env.MESSAGING_TOPIC_ID; + const sent = topicId + ? await notifySubscribers(new Messaging(client), topicId, alert, incident) + : []; + if (sent.length > 0) { + log(`Notified topic ${topicId} by ${sent.join(', ')}.`); + } + + return res.json({ recorded: true, incident: incident.$id, notified: sent }); +}; diff --git a/node/uptime-incident-log/src/notify.js b/node/uptime-incident-log/src/notify.js new file mode 100644 index 00000000..b213af1c --- /dev/null +++ b/node/uptime-incident-log/src/notify.js @@ -0,0 +1,77 @@ +function summary(alert, incident) { + const check = String(alert.type).toUpperCase(); + if (incident.status === 'firing') { + const result = alert.result ? `: ${alert.result}` : ''; + return { + title: `Down: ${alert.name}`, + text: `${alert.name} is down. The ${check} check failed${result} at ${new Date(alert.$createdAt).toUTCString()}.`, + }; + } + + const minutes = Math.max(1, Math.round(incident.downtimeSeconds / 60)); + return { + title: `Recovered: ${alert.name}`, + text: `${alert.name} is back up after about ${minutes} min of downtime. The ${check} check passed at ${new Date(alert.resolvedAt).toUTCString()}.`, + }; +} + +async function send(create) { + try { + await create(); + return true; + } catch (err) { + // The message ID is fixed per incident and state, so a repeated event conflicts. + if (err.code === 409) { + return false; + } + throw err; + } +} + +/** + * Sends the incident's state change to a topic, once per channel its subscribers use. + */ +export async function notifySubscribers(messaging, topicId, alert, incident) { + const topic = await messaging.getTopic({ topicId }); + const { title, text } = summary(alert, incident); + const prefix = `${incident.$id}-${incident.status}`; + const topics = [topicId]; + const sent = []; + + if ( + topic.emailTotal > 0 && + (await send(() => + messaging.createEmail({ + messageId: `${prefix}-email`, + subject: title, + content: text, + topics, + }) + )) + ) { + sent.push('email'); + } + if ( + topic.smsTotal > 0 && + (await send(() => + messaging.createSms({ messageId: `${prefix}-sms`, content: text, topics }) + )) + ) { + sent.push('sms'); + } + if ( + topic.pushTotal > 0 && + (await send(() => + messaging.createPush({ + messageId: `${prefix}-push`, + title, + body: text, + topics, + }) + )) + ) { + sent.push('push'); + } + + return sent; +} From 8d692ca653d5feba559498912cc56dc456e4e118 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 20 Nov 2026 12:22:21 +0000 Subject: [PATCH 2/2] fix(uptime-incident-log): keep message IDs within 36 characters Alert IDs are 32 characters, so the state and channel suffix has to stay at three. --- node/uptime-incident-log/src/notify.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/node/uptime-incident-log/src/notify.js b/node/uptime-incident-log/src/notify.js index b213af1c..28612a5d 100644 --- a/node/uptime-incident-log/src/notify.js +++ b/node/uptime-incident-log/src/notify.js @@ -34,7 +34,8 @@ async function send(create) { export async function notifySubscribers(messaging, topicId, alert, incident) { const topic = await messaging.getTopic({ topicId }); const { title, text } = summary(alert, incident); - const prefix = `${incident.$id}-${incident.status}`; + // Message IDs fit 36 characters: the 32-character alert ID, then the state and channel. + const prefix = `${incident.$id}-${incident.status === 'firing' ? 'f' : 'r'}`; const topics = [topicId]; const sent = []; @@ -42,7 +43,7 @@ export async function notifySubscribers(messaging, topicId, alert, incident) { topic.emailTotal > 0 && (await send(() => messaging.createEmail({ - messageId: `${prefix}-email`, + messageId: `${prefix}e`, subject: title, content: text, topics, @@ -54,7 +55,7 @@ export async function notifySubscribers(messaging, topicId, alert, incident) { if ( topic.smsTotal > 0 && (await send(() => - messaging.createSms({ messageId: `${prefix}-sms`, content: text, topics }) + messaging.createSms({ messageId: `${prefix}s`, content: text, topics }) )) ) { sent.push('sms'); @@ -63,7 +64,7 @@ export async function notifySubscribers(messaging, topicId, alert, incident) { topic.pushTotal > 0 && (await send(() => messaging.createPush({ - messageId: `${prefix}-push`, + messageId: `${prefix}p`, title, body: text, topics,