A simple koa middleware to monitor the API.
npm i --save koa-healthcheck
Every check object must looks like:
const check = {
name: 'The optional check name',
type: 'The check type',
configuration: {},
};
Attributes:
- name: optional, he's used in the response of the '/healtcheck' to identify the state of each check. Default value is
no-name-
with ine index of this check. - type: required, if the type isn't defined, the check will be skipped.
- configuration: required, contain a specific configuration for your check.
A DB check object must looks like:
const mySequelizeConf = {
database: 'my-db',
username: null,
password: null,
options: {
storage: './tests.sqlite',
dialect: 'sqlite'
},
};
const myDBCheck = {
type: 'db',
configuration: mySequelizeConf
};
A ping check object must looks like:
const myPingCheck = {
type: 'ping',
configuration: {
url: 'http://my-awesome-site.io/another-url',
},
};
In the index of your project:
const Koa = require('koa');
// Import the middleware:
const healthcheckPlugin = require('koa-healthcheck');
const app = new Koa();
const myChecks = [myDBCheck, myPingCheck];
const versionOfMyApp = '1.2.3';
// Just setup the middleware:
healthcheckPlugin(app, myChecks, versionOfMyApp);
app.listen(3456); // enjoy !
Check if the API is up and return "pong"
GET http://my-api.com/ping
HTTP/1.1 200 OK
"pong"
Check the state of whole dependencies of your app.
Return and object of states.
GET http://my-api.com/healthcheck
HTTP/1.1 200 OK
{
"my-first-check": 0,
"my-second-check": 0,
"my-third-check": 0
}
The key is the name of the test. The value possible are :
0
when the check is OK1
when the check is KO
Return the current version of your application based on the setup.
GET http://my-api.com/version
HTTP/1.1 200 OK
"X.Y.Z"
Return the amont of request returned with 2XX, 4XX and 5XX status.
GET http://my-api.com/metrics
HTTP/1.1 200 OK
{
"Http2XX": 2,
"Http4XX": 1,
"Http5XX": 1
}
npm run test
To be able to monitor databases (Mysql, Postrges, sqlite) we're using Sequelize.