Openstatus www.openstatus.dev

๐Ÿ“š How to use OpenStatus in CI (#878)

* ๐Ÿšง wip guide

* ๐Ÿšง wip

* ๐Ÿšง wip

* ๐Ÿ“ guide latency ci

authored by

Thibault Le Ouay and committed by
GitHub
8301b23a 23b3102e

+224 -2
+8
apps/docs/guides/introduction.mdx
··· 1 + --- 2 + title: Introduction 3 + --- 4 + 5 + Here's some content for the getting started with OpenStatus. 6 + 7 + - How to use OpenStatus to test latency in GitHub Actions 8 +
+182
apps/docs/guides/latency-ci-integration-test.mdx
··· 1 + --- 2 + title: How to use OpenStatus in GitHub Actions to test your Cloudflare Workers latency. 3 + description: Learn how to use OpenStatus for testing latency in your GitHub Actions to verify your Cloudflare Workers' performance. 4 + --- 5 + 6 + ## Introduction 7 + 8 + As a developer, you want to make sure your endpoints are fast and reliable after each deployment. 9 + 10 + In this guide, we will show you how to use OpenStatus for testing latency in GitHub Actions to verify your endpoint's performance. 11 + 12 + 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. 13 + 14 + All the code showcased in this guide is available on [GitHub](https://github.com/openstatusHQ/github-action-rollback). 15 + 16 + 17 + ## Prerequisites 18 + 19 + - A Cloudflare Account. 20 + - A GitHub Account. 21 + - An [OpenStatus](https://www.openstatus.dev) Account. 22 + 23 + 24 + ## Our API Endpoint 25 + 26 + For this guide, we are using a simple [Hono](https://www.hono.dev) server that returns `Hello OpenStatus!`. 27 + We are deploying it on [Cloudflare Workers](https://workers.cloudflare.com/). 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. 28 + 29 + 30 + ```typescript 31 + import { Hono } from "hono"; 32 + 33 + const app = new Hono(); 34 + 35 + app.get("/", async (c) => { 36 + return c.text("Hello OpenStatus!"); 37 + }); 38 + 39 + export default app; 40 + ``` 41 + 42 + 43 + ## Setting up the test 44 + 45 + For the test, we are using the [OpenStatus API](https://docs.openstatus.dev/api-reference/check/post-check) to run a global latency test on our endpoint. 46 + If the latency is higher than expected, we will roll back the deployment. 47 + We expect our endpoint to have a 75th percentile latency of less than 1000ms in all regions. 48 + 49 + You need to get your OpenStatus API key from the OpenStatus dashboard. 50 + 51 + To get your API key, go to: 52 + 53 + 1. Settings -> 2. API Tokens -> 3. Create API Key 54 + 55 + <Frame caption="GitHub Settings"> 56 + <img 57 + src="/images/guides/latency-ci-integration-test/openstatus-api-token.png" 58 + alt="OpenStatus API Key Creation" 59 + /> 60 + </Frame> 61 + 62 + If you want to run the test locally copy your API key to a `.env` file. 63 + 64 + ```env 65 + OPENSTATUS_API_KEY=your-api-key 66 + ``` 67 + 68 + Here is our test first we warm up our endpoint and then run the latency test with OpenStatus. 69 + 70 + 71 + 72 + ```typescript 73 + test("should fail if p75 > 1000", async () => { 74 + 75 + // Let's warm our endpoint 76 + await fetch("https://github-action-rollback.thibaultleouay.workers.dev/"); 77 + // Run the test 78 + const options = { 79 + method: "POST", 80 + headers: { 81 + "Content-Type": "application/json", 82 + "x-openstatus-key": process.env.OPENSTATUS_API_KEY || "", 83 + }, 84 + body: '{"url":"https://github-action-rollback.thibaultleouay.workers.dev/","method":"GET","regions":["ams","iad","gru","hkg","jnb","syd"],"runCount":2,"aggregated":true}', 85 + }; 86 + 87 + const data = await fetch("https://api.openstatus.dev/v1/check", options); 88 + const json = await data.json(); 89 + const result = schema.parse(json); 90 + expect(result.aggregated.firstByte.p75 < 1000).toBe(true); 91 + }); 92 + ``` 93 + 94 + ## Setting up the GitHub Actions 95 + 96 + We will use GitHub Actions to deploy our endpoint and run the test. 97 + We are using the bun CLI to deploy our endpoint instead of the Cloudflare Workers Action and run the test 98 + 99 + You need to set up your secrets in the GitHub repository settings for the OpenStatus API key and Cloudflare API token. 100 + 101 + Go to 1. Settings -> 2. Secrets and variables -> 3. New repository secret. 102 + 103 + 104 + <Frame caption="GitHub Settings"> 105 + <img 106 + src="/images/guides/latency-ci-integration-test/github-api-token.png" 107 + alt="GitHub Secrets" 108 + /> 109 + </Frame> 110 + 111 + 112 + 113 + Here is the GitHub Action workflow file: 114 + 115 + ```yaml 116 + 117 + name: Deploy 118 + 119 + on: 120 + push: 121 + branches: 122 + - main 123 + 124 + jobs: 125 + deploy: 126 + name: Deploy ๐Ÿ”ฅ 127 + runs-on: ubuntu-latest 128 + env: 129 + OPENSTATUS_API_KEY: ${{ secrets.OPENSTATUS_API_KEY }} 130 + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} 131 + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} 132 + 133 + steps: 134 + - name: โฌ‡๏ธ Checkout repo 135 + uses: actions/checkout@v3 136 + 137 + - name: ๐Ÿ”ฅ Install bun 138 + uses: oven-sh/setup-bun@v1 139 + with: 140 + bun-version: latest 141 + 142 + - name: ๐Ÿ“ฅ Download deps 143 + run: bun install 144 + 145 + - name: ๐Ÿ”ฅ Deploy 146 + run: bun run deploy 147 + 148 + 149 + - name: ๐Ÿงช Test 150 + run: bun test 151 + id: test 152 + 153 + - name: ๐Ÿš€ Rollback 154 + if: failure() && steps.test.outcome == 'failure' 155 + run: bun run rollback 156 + ``` 157 + 158 + In the workflow file, we have this step it will only run if the test fails: 159 + 160 + ```yaml 161 + - name: ๐Ÿš€ Rollback 162 + if: failure() && steps.test.outcome == 'failure' 163 + run: bun run rollback 164 + ``` 165 + 166 + and in our package.json we have the following script: 167 + 168 + ```json 169 + { 170 + "scripts": { 171 + "rollback": "wrangler rollback --message 'Integration test failed'" 172 + } 173 + } 174 + ``` 175 + 176 + If you want to see the example in action, you can check the [GitHub repository actions](https://github.com/openstatusHQ/github-action-rollback/actions) 177 + 178 + 179 + ## Conclusion 180 + 181 + We have successfully set up a GitHub Action that deploys our endpoint our Cloudflare workers and runs a latency test against it with OpenStatus. 182 + Don't ever let your users experience slow endpoints, use OpenStatus to monitor your endpoint's performance and roll back deployments if needed.
apps/docs/images/guides/latency-ci-integration-test/github-api-token.png

This is a binary file and will not be displayed.

apps/docs/images/guides/latency-ci-integration-test/openstatus-api-token.png

This is a binary file and will not be displayed.

+8
apps/docs/mint.json
··· 51 51 "url": "api-reference" 52 52 }, 53 53 { 54 + "name": "Guides", 55 + "url": "guides" 56 + }, 57 + { 54 58 "name": "Contributor Guides", 55 59 "url": "contributing" 56 60 } ··· 202 206 ] 203 207 } 204 208 ] 209 + }, 210 + { 211 + "group": "Guides", 212 + "pages": ["guides/introduction", "guides/latency-ci-integration-test"] 205 213 } 206 214 ], 207 215 "analytics": {
+24
packages/tinybird/datasources/check_response.datasource
··· 1 + VERSION 0 2 + 3 + SCHEMA > 4 + `requestId` Int64 `json:$.requestId`, 5 + `workspaceId` Int64 `json:$.workspaceId`, 6 + `latency` Int64 `json:$.latency`, 7 + `region` String `json:$.region`, 8 + `time` DateTime `json:$.time`, 9 + `headers` String `json:$.headers`, 10 + `timing_connectDone` Int64 `json:$.timing.connectDone`, 11 + `timing_connectStart` Int64 `json:$.timing.connectStart`, 12 + `timing_dnsDone` Int64 `json:$.timing.dnsDone`, 13 + `timing_dnsStart` Int64 `json:$.timing.dnsStart`, 14 + `timing_firstByteDone` Int64 `json:$.timing.firstByteDone`, 15 + `timing_firstByteStart` Int64 `json:$.timing.firstByteStart`, 16 + `timing_tlsHandshakeDone` Int64 `json:$.timing.tlsHandshakeDone`, 17 + `timing_tlsHandshakeStart` Int64 `json:$.timing.tlsHandshakeStart`, 18 + `timing_transferDone` Int64 `json:$.timing.transferDone`, 19 + `timing_transferStart` Int64 `json:$.timing.transferStart` 20 + 21 + ENGINE "MergeTree" 22 + ENGINE_SORTING_KEY "workspaceId, requestId, time" 23 + ENGINE_TTL "" 24 + ENGINE_PARTITION_KEY ""
+2 -2
packages/tinybird/datasources/ping_response.datasource
··· 10 10 `workspaceId` String `json:$.workspaceId`, 11 11 `cronTimestamp` Int64 `json:$.cronTimestamp`, 12 12 `message` Nullable(String) `json:$.message`, 13 - `timing` Nullable(String) `json:$.message`, 14 - `headers` Nullable(String) `json:$.message` 13 + `timing` Nullable(String) `json:$.timing`, 14 + `headers` Nullable(String) `json:$.headers`, 15 15 16 16 ENGINE "MergeTree" 17 17 ENGINE_SORTING_KEY "monitorId, cronTimestamp"