Heartbeat Ping: Application Monitoring Signals

Send regular signals to prove your application is working correctly

The heartbeat ping is a monitoring mechanism based on a simple but powerful principle: your application regularly sends a signal to a monitoring service to prove it's alive and functioning correctly. The absence of this signal triggers an alert, allowing detection of failures that would otherwise go unnoticed.

Unlike classic HTTP monitoring that tests URL availability from the outside, the heartbeat ping is emitted from inside your application. This "inside-out" approach allows monitoring of processes that don't expose an HTTP interface: crons, queue workers, batch scripts, data synchronizations. If the process doesn't run or crashes before completion, no ping is sent and you're alerted.

This technique is particularly effective for processes that run periodically and whose failure can go unnoticed for days: automatic backups, data imports, report generation, temporary file cleanup. The heartbeat ping ensures you'll be alerted if any of these critical processes stops working.

How Heartbeat Ping Works

The heartbeat ping mechanism relies on four components working together to ensure reliable monitoring of your processes.

When to Send the Heartbeat Ping

The placement of the ping in your code is crucial for effective monitoring. Here are different situations and associated best practices.

Integration Examples by Language

Here are complete examples of heartbeat ping integration in different languages and contexts. All examples include proper error handling and best practices.

Bash / Shell (Crontab)

#!/bin/bash
# Backup script with heartbeat ping

PING_URL="https://ping.monitao.com/p/YOUR_TOKEN"

# Execute backup
/usr/bin/mysqldump -u root -p'password' mydb > /backup/db.sql
RESULT=$?

# Ping only if backup succeeded
if [ $RESULT -eq 0 ]; then
    # Verify file is not empty
    if [ -s /backup/db.sql ]; then
        curl -fsS --retry 3 --max-time 10 "$PING_URL" > /dev/null
        echo "Backup OK, ping sent"
    else
        echo "Empty backup file, no ping"
        exit 1
    fi
else
    echo "Backup error, no ping"
    exit 1
fi

This script checks not only the mysqldump return code but also that the generated file isn't empty. Curl uses --retry 3 to retry on temporary network issues, and --max-time 10 to avoid blocking indefinitely.

PHP (Laravel / Symfony)

generateReports();

            // Validation: at least some reports generated
            if ($count === 0) {
                $this->warn('No reports generated');
                // Still ping since job did execute
            }

            // Ping with metrics
            Http::timeout(10)->retry(3)->post(
                'https://ping.monitao.com/p/YOUR_TOKEN',
                ['duration' => $this->executionTime, 'count' => $count]
            );

            $this->info("Done: {$count} reports");

        } catch (\Exception $e) {
            report($e);
            // No ping = MoniTao alert
            return 1;
        }
    }
}

The Laravel example uses the built-in HTTP client with automatic retry. Metrics (duration, item count) are sent with the ping for enriched monitoring. The exception stops the script before the ping.

Python

import requests
import sys
from datetime import datetime

PING_URL = "https://ping.monitao.com/p/YOUR_TOKEN"

def ping_monitao(duration_ms=None, status="success"):
    """Send ping to MoniTao with retry"""
    payload = {"status": status}
    if duration_ms:
        payload["duration"] = duration_ms

    for attempt in range(3):
        try:
            response = requests.post(
                PING_URL,
                json=payload,
                timeout=10
            )
            if response.ok:
                return True
        except requests.RequestException:
            continue
    return False

def main():
    start = datetime.now()
    try:
        # Your business logic
        process_data()
        sync_database()
        generate_reports()

        # Calculate duration and ping
        duration = (datetime.now() - start).total_seconds() * 1000
        ping_monitao(duration_ms=duration)
        print(f"Done in {duration:.0f}ms")

    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        # No ping = alert
        sys.exit(1)

if __name__ == "__main__":
    main()

The Python script includes a reusable function with built-in retry. Execution duration is calculated and sent with the ping, allowing performance trend tracking in MoniTao history.

Node.js

const https = require('https');

const PING_URL = 'https://ping.monitao.com/p/YOUR_TOKEN';

async function pingMonitao(metrics = {}) {
    return new Promise((resolve) => {
        const data = JSON.stringify(metrics);
        const url = new URL(PING_URL);

        const req = https.request({
            hostname: url.hostname,
            path: url.pathname,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': data.length
            },
            timeout: 10000
        }, (res) => resolve(res.statusCode < 400));

        req.on('error', () => resolve(false));
        req.write(data);
        req.end();
    });
}

async function main() {
    const start = Date.now();
    try {
        await processQueue();
        await syncData();

        const duration = Date.now() - start;
        await pingMonitao({ duration, items: processedCount });
        console.log(`Done in ${duration}ms`);

    } catch (error) {
        console.error('Error:', error.message);
        process.exit(1);
    }
}

main();

The Node.js example uses the native https module without external dependencies. The pingMonitao function returns a promise and can be easily integrated into any async/await script.

Implementation Best Practices

These recommendations will help you get the most out of heartbeat pings while avoiding common pitfalls.

Common Mistakes to Avoid

These frequent mistakes can reduce your monitoring effectiveness or generate false alerts.

Concrete Use Cases

Heartbeat pings apply to many situations. Here are the most common cases with their implementation specifics.

Queue Workers

Workers processing queue messages (Redis, RabbitMQ, SQS) are ideal heartbeat candidates. A worker can die silently (OOM, crash, evicted container) without anyone noticing. Send a ping every 1-5 minutes in the worker's main loop. If the worker dies, pings stop and you're alerted before the queue dangerously fills up.

Backup Crons

Backups are critical but their failure is often silent: cron doesn't execute, script crashes, insufficient disk space. Create a daily heartbeat with 4-6 hour grace period. Ping after verifying backup integrity (non-empty file, valid checksum). You'll immediately know if a backup didn't happen.

Data Synchronizations

Synchronization jobs between systems (CRM ↔ ERP, database ↔ data warehouse) must run regularly to maintain consistency. An undetected sync stop causes divergences that accumulate and become expensive to fix. Ping after each successful sync cycle, including the number of synchronized records in metrics.

Integration Checklist

  • Create heartbeat in MoniTao and retrieve ping URL
  • Configure interval matching the actual job frequency
  • Add sufficient grace period (50-100% of interval)
  • Integrate ping at script end, after success validation
  • Add timeout and retries to HTTP call
  • Test manually: run job and verify ping in MoniTao

Heartbeat Ping with MoniTao

MoniTao simplifies heartbeat ping setup with an intuitive interface and advanced features for all your monitoring needs.

Frequently Asked Questions About Heartbeat Ping

What happens if my ping fails due to a network issue?

A single missed ping doesn't trigger an alert thanks to the grace period. Use retries (2-3 attempts) in your code to absorb temporary network issues. If failures persist beyond the grace period, you'll be alerted, which is generally desirable as it indicates a larger problem.

Can I send data with the ping?

Yes, MoniTao accepts a JSON payload with the ping. You can send execution duration, items processed, detailed status, or any other relevant metric. This data is stored in history and allows analyzing trends and detecting gradual degradations.

How to handle multiple environments (prod, staging)?

Create a distinct heartbeat for each environment. Each heartbeat has its own ping URL and alert settings. You can organize your heartbeats by tags or projects in MoniTao for better visibility.

Does the ping slow down my script?

The impact is negligible. An HTTP call with 5-second timeout adds at most 5 seconds to your script, and in practice much less (tens of milliseconds). For very frequent jobs (every minute), you can use an async call that doesn't wait for response.

Should I ping even if my job did nothing?

Yes, absolutely. If your job checks a condition (emails to send, files to process) and finds nothing to do, it should still ping. The ping proves the job executed and checked the condition. Without a ping, you can't distinguish "nothing to do" from "job didn't run at all".

What's the difference between GET and POST for the ping?

Both methods work with MoniTao. GET is simpler (URL only) and sufficient for a basic ping. POST allows sending additional data (metrics, status) in the request body. Use POST if you want to enrich your monitoring with metrics.

Monitor All Your Processes with Heartbeat Ping

The heartbeat ping is a simple but extremely effective technique for monitoring processes that can't be tested from the outside. In just a few lines of code, you can transform any script, cron, or worker into a monitored process, with guaranteed alerts in case of unexpected shutdown.

With MoniTao, heartbeat ping integration takes less than 5 minutes. Create a heartbeat, copy the URL, add an HTTP call at the end of your script, and you're protected against silent failures. No longer discover your problems when it's too late - proactively monitor with heartbeat ping.

Ready to Sleep Soundly?

Start free, no credit card required.