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

+1
View File
@@ -105,6 +105,7 @@ jobs:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
TF_VAR_APPS_US_EAST_TARGET: ${{ secrets.APPS_US_EAST_TARGET }}
TF_VAR_MATTERMOST_WEBHOOK: ${{ secrets.MATTERMOST_WEBHOOK }}
D1_ID: ${{ env.D1_ID }}
# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
+10
View File
@@ -23,6 +23,12 @@ variable "APPS_US_EAST_TARGET" {
sensitive = true
}
variable "MATTERMOST_WEBHOOK" {
# read from $TF_VAR_MATTERMOST_WEBHOOK (the GitHub Actions secret of the same name)
type = string
sensitive = true
}
variable "enable_do_migration" {
type = bool
default = false
@@ -70,6 +76,10 @@ resource "cloudflare_workers_script" "uptimeflare_worker" {
name = "APPS_US_EAST_TARGET"
type = "secret_text"
text = var.APPS_US_EAST_TARGET
}, {
name = "MATTERMOST_WEBHOOK"
type = "secret_text"
text = var.MATTERMOST_WEBHOOK
}]
}
+13 -1
View File
@@ -57,7 +57,19 @@ const workerConfig: WorkerConfig = {
headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' },
},
],
// Alerts come from Grafana Cloud (hearth infra/monitoring). A webhook here could page too.
// Down and back-up messages go to the founder's Mattermost (channel "alerts"). The webhook address
// is private: the worker reads it from the Worker secret MATTERMOST_WEBHOOK (GitHub Actions secret of
// the same name, bound in deploy.tf). Grafana Cloud (hearth infra/monitoring) alerts there too.
notification: {
webhook: {
url: 'secret:MATTERMOST_WEBHOOK',
payloadType: 'json',
payload: { text: '$MSG', username: 'Status' },
},
timeZone: 'Etc/UTC',
// Wait a few minutes before saying something is down, so a single failed check doesn't page.
gracePeriod: 3,
},
}
// Notices shown on the page: incident updates and planned maintenance. Newest last.
+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,
}