Heartbeats
A dead-man's switch for cron jobs, pipelines and services. Page when the pings stop.
A heartbeat inverts the usual monitoring question. Instead of telling us when something is wrong, your job tells us when it is fine, and we page you when it stops saying so. This catches the failures that make no noise: a cron job that never started, a pipeline that hung, a process that died.
#How it works
- You create a heartbeat with an interval, for example "every day, plus 15 minutes of grace".
- Your job pings it each time it succeeds.
- If a ping is not received by
interval + graceafter the last one, an alert opens with aliasheartbeat:<name>and pages the team's escalation policy. - The next ping closes the alert and the heartbeat is
upagain.
States: new (never pinged yet), up, down (its alert is open).
#Create one
Configuring needs an admin key. It is idempotent, so you can run it on every deploy ("heartbeats as code").
treason heartbeat set nightly-backup --every 1d --grace 15m --priority P1 \
--message "The nightly backup did not report in"await treason.heartbeats.upsert("nightly-backup", {
intervalSeconds: 86_400,
graceSeconds: 900,
priority: "P1",
message: "The nightly backup did not report in",
});curl -X PUT $TREASON_API_URL/v1/heartbeats/nightly-backup \
-H "Authorization: Bearer $TREASON_ADMIN_KEY" -H "Content-Type: application/json" \
-d '{"intervalSeconds":86400,"graceSeconds":900,"priority":"P1"}'The clock starts when you create it, so a new heartbeat gets a full interval before its first ping is due. Updating or re-enabling restarts it the same way.
#Ping it
Any key that can create alerts (alert scope) can ping. That is what a job should carry.
curl -X POST $TREASON_API_URL/v1/heartbeats/nightly-backup/ping \
-H "Authorization: Bearer $TREASON_API_KEY"const { recovered } = await treason.heartbeats.ping("nightly-backup");treason heartbeat ping nightly-backupThe response says whether the ping recovered the heartbeat, that is, closed an open alert.
#Wrap a command
For anything you can run from a shell, treason run does the right thing without changing the script:
# cron: ping on success; a failure or a missing run both page
0 3 * * * treason run --heartbeat nightly-backup -- /opt/backup.sh
# also open an alert immediately, with the output, when the command fails
0 3 * * * treason run --heartbeat nightly-backup --alert "Nightly backup failed" --priority P1 -- /opt/backup.shOn success it pings the heartbeat and closes the --alert alert if one is open. On failure it opens the alert with the exit code and the last few lines of output, and the command's exit code is passed through. A command that fails with only --heartbeat simply does not ping: the missing ping is the signal.
#Choosing interval and grace
- Interval is how often a ping is expected. Between 10 seconds and one year.
- Grace is how late a ping may be before it counts as missed. Set it to your job's normal run time plus some slack, so a slow night is not an incident.
- Alert on absence, not on slowness: for latency, send your own alert from the job.
#Monitoring the monitor
A heartbeat is the right way to know your own watcher is alive: have the watcher ping its own heartbeat after each pass. The uptime sentinel example does exactly this.
#Details
- Pings are idempotent and safe to retry; the SDK retries them.
- One alert per outage: a heartbeat that stays down does not page again, and one that recovers and fails later opens a new alert.
- Heartbeats are checked by our workers every 15 seconds, so an alert opens within about 15 seconds of the deadline.
- Deleting a heartbeat does not close an alert it already opened.