Send down and back-up alerts to Mattermost (#6)
Deploy to Cloudflare / build-and-deploy (push) Skipped
Check / check (push) Successful in 10m48s

The webhook address is private: uptime.config.ts names it as secret:MATTERMOST_WEBHOOK and the
worker reads the Worker secret of that name (bound in deploy.tf from the GitHub Actions secret).
The address is kept out of the logs. Alerts wait 3 minutes before saying something is down.
This commit is contained in:
michael authored and GitHub committed 2026-11-23 10:11:52 -05:00
1 parent cf977e65da
commit 7c3eb7debd
5 files changed
+53 -5

No files matched your search

+2 -1
View File
@@ -2,7 +2,7 @@ import { DurableObject } from 'cloudflare:workers'
import { MonitorTarget } from '../../types/config'
import { workerConfig } from '../../uptime.config'
import { doMonitor, getStatus } from './monitor'
import { formatAndNotify, getWorkerLocation } from './util'
import { formatAndNotify, getWorkerLocation, setWebhookSecrets } from './util'
import { CompactedMonitorStateWrapper, getFromStore, setToStore } from './store'
import pLimit from 'p-limit'
@@ -45,6 +45,7 @@ async function checkMonitor(monitor: MonitorTarget, location: string, env: Env)
const Worker = {
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
setWebhookSecrets(env)
const workerLocation = (await getWorkerLocation()) || 'ERROR'
console.log(`Running scheduled event on ${workerLocation}...`)
+27 -3
View File
@@ -79,6 +79,24 @@ function templateWebhookPlayload(payload: any, message: string) {
}
}
// Cottage: a webhook url may be `secret:NAME`, read at send time from the Worker secret NAME (bound in
// deploy.tf from a GitHub Actions secret), so the address never appears in this public repo or the logs.
let webhookSecrets: Record<string, unknown> = {}
function setWebhookSecrets(env: object) {
webhookSecrets = env as Record<string, unknown>
}
function resolveWebhookUrl(url: string): string | undefined {
if (!url.startsWith('secret:')) return url
const value = webhookSecrets[url.slice('secret:'.length)]
return typeof value === 'string' && value !== '' ? value : undefined
}
function hideUrl(text: string, url: string, real: string): string {
return real === url ? text : text.split(real).join(url)
}
async function webhookNotify(webhook: WebhookConfig, message: string) {
if (Array.isArray(webhook)) {
for (const w of webhook) {
@@ -90,8 +108,13 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
console.log(
'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url
)
const realUrl = resolveWebhookUrl(webhook.url)
if (realUrl === undefined) {
console.log(`Webhook address not configured (${webhook.url}), skipping notification`)
return
}
try {
let url = webhook.url
let url = realUrl
let method = webhook.method
let headers = new Headers(webhook.headers as any)
let payloadTemplated: { [key: string]: string | number } = JSON.parse(
@@ -128,7 +151,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
}
console.log(
`Webhook finalized parameters: ${method} ${url}, headers ${JSON.stringify(
`Webhook finalized parameters: ${method} ${hideUrl(url, webhook.url, realUrl)}, headers ${JSON.stringify(
Object.fromEntries(headers.entries())
)}, body ${JSON.stringify(body)}`
)
@@ -142,7 +165,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
console.log('Webhook notification sent successfully, code: ' + resp.status)
}
} catch (e) {
console.log('Error calling webhook server: ' + e)
console.log('Error calling webhook server: ' + hideUrl(String(e), webhook.url, realUrl))
}
}
@@ -198,4 +221,5 @@ export {
webhookNotify,
formatStatusChangeNotification,
formatAndNotify,
setWebhookSecrets,
}