Heartbeats

A backup that runs every night at 02:00, a batch that must be done before the office opens: you do not want to check them, you want to hear when they did not happen. A heartbeat is a monitor that waits for a call. Your job POSTs a secret url when it finishes; Sartar alerts when nothing came by the time you set, tells you when the call finally arrives late, and gives up after a second delay.

Create the monitor

In the web interface, choose the Heartbeat type when you create a monitor. With the CLI:

sartar monitor create --name "/prod/nightly backup" --type heartbeat.inbound \
  --daily-at 02:00 --late-after 1h --lost-after 4h \
  --require-header "X-API: 123"

A call counts from the expected time on: one that arrives earlier is refused with 409 and the time to come back at (counted as out of window, never alerted). If your job may finish a little early, set the expected time a little earlier.

The url is printed at creation, and sartar monitor heartbeat url reads it again (with a key that may update the monitor). There is no --url and no region: nothing is fetched.

Call it

curl -fsS -X POST https://heartbeat.sartar.app/h/<token> \
  -H 'X-API: 123' \
  -H 'content-type: application/json' \
  -d '{"ok":true,"message":"12 GB, 14 min"}'

Only POST is accepted. The body is optional: an empty call means "done". Sent as JSON, ok and message are read:

pg_dump … && STATUS=true || STATUS=false
curl -fsS -X POST https://heartbeat.sartar.app/h/<token> -H 'X-API: 123' \
  -H 'content-type: application/json' -d "{\"ok\":$STATUS}"

Call it with an api key

Rather keep an api key than a secret url? Call the heartbeat through the api, with a key whose role holds monitor:report (the Reporter role):

sartar monitor report --name "/prod/nightly backup" --ok --message "12 GB, 14 min"
sartar monitor report --name "/prod/nightly backup" --failed "pg_dump: connection refused"

or POST /api/monitors/<uuid>/results with {"ok": true, "message": "…"}. Only ok and message are accepted. Everything else is the same as a call to the url: it counts from the expected time on (409 before), at most 10 calls a minute, and it shows in the history, with the method API. The required headers do not apply: the key is what proves who calls.

What happens around the expected time

Each expected run goes through these states:

State Meaning
sleeping the window is not open yet
waiting the window is open, nothing came, the alert delay has not passed
received the call came: on time, late, or reporting a failure
late the alert delay passed with nothing: the alert went out, Sartar still waits
lost the give-up delay passed with nothing: Sartar no longer waits for this run

After lost, the next run starts on its own: sleeping, then waiting. The monitor stays in alert, red in the list, until a run arrives on time again. A run that arrived late shows as a warning. Acknowledging the alert stops the reminders; muting it or a maintenance silences it like any other monitor.

Tuning your job

A job rarely works the first time. To try it without waiting for the expected time, use Wait for a call now (in the monitor's editor) or:

sartar monitor heartbeat arm --name "/prod/nightly backup"

A run opens at once, with your delays, and the status starts again from unknown: launch your job and its call is judged straight away (on time, or a failure if it sends "ok": false). The expected time then takes over again. Repeat as often as needed.

Changing the expected time takes effect when you save: if today's new time passed less than the alert delay ago, that is the run being waited for. A call before the expected time is refused (409) with the expected time, and counted out of window.

Notifications

The monitor notifies like any other monitor (its target, its policy, its mute and maintenances), with its own messages:

Securing the url

The url carries a long random token: anyone who has it can call it. Add required headers (--require-header "Name: value", up to five): a call without them is refused. The url and the values are shown to whoever may edit the monitor, never to a reader.

Refused calls never alert

A wrong header, too many calls (more than 10 a minute), a body over 16 KB or JSON that does not parse: the call is refused (401, 429, 413, 400) and counted, never alerted. If your job keeps sending a wrong header, the alert you get is the one for a job that did not call: at the alert delay. To see what happened:

sartar monitor heartbeat url --name "/prod/nightly backup"
sartar monitor heartbeat state --name "/prod/nightly backup"
sartar monitor heartbeat arm --name "/prod/nightly backup"
sartar monitor heartbeat stats --name "/prod/nightly backup" --week
sartar monitor heartbeat regenerate --name "/prod/nightly backup"
sartar monitor heartbeat headers --name "/prod/nightly backup" --require-header "X-API: 456"

stats counts the calls of the day or of the week (Monday to Sunday): accepted ones — on time, outside any window, repeated — and refused ones by cause, then lists the last calls with their address and result.

The url and the headers afterwards

regenerate makes a new url, to revoke one that leaked: the old one stops working at once. headers replaces the required headers (--none removes them). In Terraform, the url is the heartbeat_url attribute, refreshed on every read, and the headers are heartbeat_headers (see Terraform).

What it does not do