Monitoring Website Uptime

In this guide, we'll walk you through the process of using the Telemetry SDK to monitor the uptime of a website by sending regular heartbeats. By the end of this guide, you'll have a setup that logs and analyzes your website's uptime data.

Prerequisites

  • A valid API key for Telemetry

  • Basic understanding of JavaScript and Node.js

Step 1: Install Telemetry SDK

First, you need to install the Telemetry SDK in your project. If you haven't done so already, run the following command:

npm install telemetry-sh

Step 2: Initialize Telemetry

After installing the SDK, import and initialize Telemetry in your project. Replace YOUR_API_KEY with your actual Telemetry API key.

import telemetry from "telemetry-sh";

telemetry.init("YOUR_API_KEY");

Step 3: Set Up Heartbeat Function

To monitor uptime, you can set up a heartbeat function that will periodically send a ping to the Telemetry service. The heartbeat will log the status of your website, allowing you to monitor its availability.

Here's a basic example of a heartbeat function:

const sendHeartbeat = async (url) => {
  try {
    const response = await axios.get(url);

    telemetry.log("website_uptime", {
      url: url,
      status: response.status,
      is_online: true,
      timestamp: new Date().toISOString()
    });
  } catch (error) {
    telemetry.log("website_uptime", {
      url: url,
      status: error.response ? error.response.status : 500,
      is_online: false,
      timestamp: new Date().toISOString()
    });
  }
};

Step 4: Automate Heartbeats

To continuously monitor uptime, you'll want to send heartbeats at regular intervals. You can achieve this by using setInterval to run the sendHeartbeat function periodically. Here's an example that checks the website every 5 minutes:

const urlToMonitor = "https://example.com"; // Replace with your website URL
const interval = 5 * 60 * 1000; // 5 minutes in milliseconds

setInterval(() => {
  sendHeartbeat(urlToMonitor);
}, interval);

Step 5: Query and Analyze Uptime Data

Once you've logged enough data, you can query it using Telemetry's query API to analyze your website's uptime. For example, you can calculate the percentage of time your website was online:

const results = await telemetry.query(`
  SELECT
    url,
    COUNT(*) AS total_pings,
    SUM(CASE WHEN is_online THEN 1 ELSE 0 END) AS online_pings,
    (SUM(CASE WHEN is_online THEN 1 ELSE 0 END) * 100.0 / COUNT(*)) AS uptime_percentage
  FROM
    website_uptime
  GROUP BY
    url
`);

console.log(results);

Step 6: Explore Data with Telemetry's UI

Telemetry's UI allows you to visualize and explore your uptime data interactively. Visit Telemetry Dashboard and log in with your credentials to create dashboards, charts, and more based on your uptime monitoring data.

Conclusion

By following these steps, you can effectively monitor the uptime of your website using the Telemetry SDK. This setup allows you to track when your website is online or offline, helping you maintain and improve your site's availability.

Last updated