diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8bc9e94 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Hyra + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/index.js b/index.js new file mode 100644 index 0000000..ebd1bb8 --- /dev/null +++ b/index.js @@ -0,0 +1,37 @@ +const axios = require("axios").default; + +const pageId = "ckqu7nm8617021395n6p5xvlvlk"; +const metricId = "ckxo16fcc189614o8ond2p5splu"; + +const path = "https://edge.koal.us"; + +require("dotenv").config(); + +const checkApi = () => { + const start = new Date().getTime(); + axios.get(path).then(response => { + const latency = new Date().getTime() - start; + + axios.post(`https://api.instatus.com/v1/${pageId}/metrics/${metricId}`, + { + timestamp: Math.floor(new Date()), + value: latency + }, + { + headers: { + Authorization: "Bearer " + process.env.API_KEY + } + }).then(result => { + console.log('All went well with latency of ' + latency); + setTimeout(() => { + checkApi(); + }, 30 * 1000) + }).catch(err => { + console.log(err); + }) + }).catch(err => { + console.log(err); + }) +} + +checkApi(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9ae858e --- /dev/null +++ b/package-lock.json @@ -0,0 +1,72 @@ +{ + "name": "api-latency", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "api-latency", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "axios": "^0.24.0", + "dotenv": "^10.0.0" + } + }, + "node_modules/axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "dependencies": { + "follow-redirects": "^1.14.4" + } + }, + "node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "engines": { + "node": ">=10" + } + }, + "node_modules/follow-redirects": { + "version": "1.14.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", + "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + } + }, + "dependencies": { + "axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "requires": { + "follow-redirects": "^1.14.4" + } + }, + "dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==" + }, + "follow-redirects": { + "version": "1.14.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", + "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..fd5be29 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "api-latency", + "version": "1.0.0", + "description": "Latency monitor for Atlassian StatusPage", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/hyra-io/API-Latency.git" + }, + "author": "Hyra", + "license": "MIT", + "bugs": { + "url": "https://github.com/hyra-io/API-Latency/issues" + }, + "homepage": "https://github.com/hyra-io/API-Latency#readme", + "dependencies": { + "axios": "^0.24.0", + "dotenv": "^10.0.0" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8ccc604 --- /dev/null +++ b/readme.md @@ -0,0 +1,30 @@ +### Based off of [Hyra's Atlassian Statuspage monitor](https://github.com/hyra-io/API-Latency) + +# API Latency +This is a very simple monitoring script that will monitor the latency of your API server. It simply makes a request to a HTTP or HTTPS path every 30 seconds and reports the latency to [Instatus](https://instatus.com) via their API. This is used in a metric graph. + +## Installation +There are a few prerequisites to run this project locally. You'll need: +* Node.js - this was built using Node.js v16.11 +* All the dependencies in the package.json + +Clone the repo to your system + +To install the dependencies simply run `npm install`. + +You'll then need to set up your environment variables. + +## Operation +Once you've installed the latency monitor, you'll need to run it. To run it, we'd recommend using a process manager like [pm2](https://pm2.io/). + +## Secrets +We store secrets in a `.env` file stored in the root directory of the project - you can't see this file because it's in our `.gitignore`. + +Please see the format for this file below + +``` +API_KEY= +``` + +## License +This project is licensed under [MIT License](LICENSE)