Introduction

As a developer, you want to make sure your endpoints are fast and reliable after each deployment.

In this guide, we will show you how to use OpenStatus for testing latency in GitHub Actions to verify your endpoint’s performance.

We’ll deploy a basic API endpoint and run a latency test in a GitHub Action with OpenStatus. If the latency is higher than expected, we will roll back the deployment.

All the code showcased in this guide is available on GitHub.

Prerequisites

  • A Cloudflare Account.
  • A GitHub Account.
  • An OpenStatus Account.

Our API Endpoint

For this guide, we are using a simple Hono server that returns Hello OpenStatus!. We are deploying it on Cloudflare Workers. Cloudflare Workers is a serverless platform that allows you to deploy your code globally. They are fast and reliable. But sometimes you might introduce a bug that slows down your endpoint.

import { Hono } from "hono";

const app = new Hono();

app.get("/", async (c) => {
  return c.text("Hello OpenStatus!");
});

export default app;

Setting up the test

For the test, we are using the OpenStatus API to run a global latency test on our endpoint. If the latency is higher than expected, we will roll back the deployment. We expect our endpoint to have a 75th percentile latency of less than 1000ms in all regions.

You need to get your OpenStatus API key from the OpenStatus dashboard.

To get your API key, go to:

  1. Settings -> 2. API Tokens -> 3. Create API Key
OpenStatus API Key Creation

GitHub Settings

If you want to run the test locally copy your API key to a .env file.

OPENSTATUS_API_KEY=your-api-key

Here is our test first we warm up our endpoint and then run the latency test with OpenStatus.

test("should fail if p75 > 1000", async () => {

  // Let's warm our endpoint
  await fetch("https://github-action-rollback.thibaultleouay.workers.dev/");
  // Run the test
  const options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-openstatus-key": process.env.OPENSTATUS_API_KEY || "",
    },
    body: '{"url":"https://github-action-rollback.thibaultleouay.workers.dev/","method":"GET","regions":["ams","iad","gru","hkg","jnb","syd"],"runCount":2,"aggregated":true}',
  };

  const data = await fetch("https://api.openstatus.dev/v1/check", options);
  const json = await data.json();
  const result = schema.parse(json);
  expect(result.aggregated.firstByte.p75 < 1000).toBe(true);
});

Setting up the GitHub Actions

We will use GitHub Actions to deploy our endpoint and run the test. We are using the package.json command to deploy our endpoint instead of the Cloudflare Workers Action and run the test

You need to set up your secrets in the GitHub repository settings for the OpenStatus API key and Cloudflare API token.

Go to 1. Settings -> 2. Secrets and variables -> 3. New repository secret.

GitHub Secrets

GitHub Settings

Here is the GitHub Action workflow file:


name: Deploy

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: Deploy 🔥
    runs-on: ubuntu-latest
    env:
      OPENSTATUS_API_KEY: ${{ secrets.OPENSTATUS_API_KEY }}
      CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
      CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

    steps:
      - name: ⬇️ Checkout repo
        uses: actions/checkout@v3

      - name: 🔥 Install bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest

      - name: 📥 Download deps
        run: bun install

      - name: 🔥 Deploy
        run: bun run deploy


      - name: 🧪 Test
        run: bun test
        id: test

      - name: 🚀 Rollback
        if: failure() && steps.test.outcome == 'failure'
        run: bun run rollback

In the workflow file, we have this step it will only run if the test fails:

      - name: 🚀 Rollback
        if: failure() && steps.test.outcome == 'failure'
        run: bun run rollback

and in our package.json we have the following script:

{
  "scripts": {
    "rollback": "wrangler rollback --message 'Integration test failed'"
  }
}

If you want to see the example in action, you can check the GitHub repository actions

Conclusion

We have successfully set up a GitHub Action that deploys our endpoint our Cloudflare Workers and runs a latency test against it with OpenStatus.

Don’t ever let your users experience slow endpoints, use OpenStatus to monitor your endpoint’s performance and roll back deployments if needed.