Heartbeats

Heartbeats are designed for monitoring asynchronous scripts such as CRON jobs or serverless workers.

Benefits of using heartbeats

Every heartbeat comes with a unique URL you need to periodically make requests to, in order for the heartbeat NOT to create a new incident.

Think database backup script you run every day in your CRON job. How do you know it runs correctly? Add a simple curl "<your-heartbeat-url>" command at the end of the script to monitor it.

We will the current on-call person if the script fails to make a request to the URL every X minutes.

Example usage

We have a background job that makes a daily database backup of our primary PostgreSQL database.

We want to get alerted if the background job doesn't run successfully.

Create a heartbeat

  • Go to IntegrationsCreate heartbeat

  • Name your heartbeat — "Daily database backup"

  • Change Expect a heartbeat every selection to 1 day

  • Optionally configure On-call escalation settings

  • Click Save heartbeat

  • Copy the secret URL on the Heartbeat detail page, you will need it later

This heartbeat will expect us to make a GET or POST request to the URL provided to us every day since the first request.

Set up a CRON and a background job

1. Add a CRON task that will execute backup_database.sh background job every day at midnight:

0 0 * * * ruby /home/deploy/backup_database.sh >/dev/null 2>&1

2. Include the curl call to the heartbeat URL at the end of your backup script:

backup_database.rb
#!/usr/bin/env bash
set -o errexit
set -o xtrace
date=`date "+%Y-%m-%d_%H:%M:%S"`
file="/dumps/betteruptime.$date.dump"
time dokku postgres:export betteruptime > "$file"
/usr/local/bin/aws s3 cp "$file" s3://betteruptime-dbbackups/
rm "$file"
# you get this URL in the Better Uptime dashboard
curl "https://betteruptime.com/api/v1/heartbeat/XXXXXXXXXXXXXXXXXXXXXX"

What happens here: The Heartbeat URL we created above expects a GET or POST request every day since making the first request.

If the code above fails, our background job won't make the request to the Heartbeat URL. In that case, Heartbeat will alert the current on-call person and create an Incident.

Pausing, On-call escalation, Removing heartbeats

Heartbeats share traits like pausing, on-call escalation, and deletion with Monitors.

See the Monitors docs.