Health Check
npm install pac-health-check```
$ npm install pac-health-check
js
var healthCheck = require('pac-health-check');/ Sample configuration /
const configuration = {
cron: ' /5 ',
max_retry: 2,
services: {
api: [{
name: 'localhost 304',
url: 'http://localhost:' + port + '/304',
method: 'GET',
expectedStatusCode: "200|202|304"
}, {
name: 'localhost 200',
url: 'http://localhost:' + port + '/200',
method: 'GET',
expectedStatusCode: 200
}],
mssql: [{
user: "username",
password: "password",
server: "server",
port: 123,
database: "database",
connectionTimeout: 2000
}],
cas: [{
seeds: "seed ip",
keyspace: "key space",
username: "username",
password: "password"
}],
zkp: [{
connectionString: ":/"
}],
kaf: [{
connectionString: "://"
}]
}
};
//Init job
var job = healthCheck.initJob(configuration)
// Wait 5000ms to let the job run
setTimeout(function () {
var result = healthCheck.reportJob();
console.log(JSON.stringify(result));
//Stop the job
job.stop();
}, 5000)
`$3
healthCheck.reportOnDemand(configuration)It is recommended to set a ttl
configuration.ttl (in seconds).
If ttl is set, healthCheck will leverage local cache.`js
var healthCheck = require('pac-health-check');//Sample configuration
var configuration = {
ttl: 600,
services: {
api: [{
name: 'httpstat.us 202',
url: 'http://httpstat.us/202',
method: 'GET',
expectedStatusCode: "200|204",
noncritical: true
}, {
name: 'httpstat.us 200',
url: 'https://httpstat.us/200',
method: 'GET',
expectedStatusCode: 200
}
]
}
};
healthCheck.reportOnDemand(configuration)
.then(function (result) {
console.log(JSON.stringify(result));
})
.catch(function (error) {
console.log(error);
});
`$3
`js
var healthCheck = require('pac-health-check');
healthCheck.offline();
`Once
offline method is called:
- Both cron job and on demand request will stop perform actual health check. And health check cannont be resumed.
- Health check status is DOWN, previous report is still accessible. (on demand request caching subjects to ttl)Specification
$3
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :----------------------------------------------- | :-------------------- | :---------------------------------------------------------------------------- |
|C | 0-1 | cron | String |For health check by cron job flavor. Cron expression to run health check job(will run on init). |
|C | 0-1 | ttl | Number | For healch check on demand flavor. The time to live as number in seconds for local health check report cache. 0 = unlimited |
|O | 0-1 | max_retry | Number | Max retry to determine the application status is
DOWN (critical dependency is DOWN). Default is 3.|
|R | 1 | services | Object |services config for running actaul health check.|
|R | 1-N | →{service} | Array of Object | Health Check for Supported Services |
|C | 0-1 | →→noncritical | Boolean | If this service is critical. Optional, default is undefined/false. |Supported Services:
-
api: Api Service
- mssql: Microsoft SQL Server Service
- cas: Cassandra Service
- zkp: Zookeeper Service
- kaf: kafka Service $3
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| status | String | Value is
UP, DOWN, DEGRADED. Check Overall Status section for detail. |
| max_retry | Number | From max_retry property in configuration, Default is 3. |
| services | Object | Health check report in detail for each service in configuration. |
| → {service}[index].status | String | Value is UP, DOWN. Check each service section of detail. |
| → {service}[index].noncritical | Boolean | From configuration services.{service}[index].noncritical property|
Overall Status
-
status is UP, When services.{service}[index].status is UP in all services.-
status is DOWN, When any non noncritical servise services.{service}[index].status is DOWN.-
status is DEGRADED, only if any noncritical servise services.{service}[index].status is DOWN.
#### Service
apiConfiguration
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :------| :------| :------------------------------ |
|R | 1 | name | String | Name of the API |
|R | 1 | url | String | Url for health check|
|R | 1 | method | String | Http method, only support
GET for now. |
|O | 1 | services.api[index].expectedStatusCode | Number/String | Optional, accpet: Number(e.g.200); RegEx String(e.g."202|204"). Default value is "^[1-3][0-9]{2}$". |Report
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| services.api[index].name | String | From configuration |
| services.api[index].url | String | From configuration |
| services.api[index]. method | String | From configuration |
| services.api[index].status | String | Check Api Status |
| services.api[index].expectedStatusCode | String | From configuration.|
| services.api[index].currentStatusCode | Number | Actaul http status from response. |
Api Status
-
report.api[index].status is UP by default. -
report.api[index].status is DOWN, when health check result actualStatusCode is not matching expectedStatusCode.#### Service
mssqlConfiguration
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :----------------------------------------------- | :-------------------- | :----------------------------------------------------------------------------
|R | 1 | server | String | Mssql Server hostname or IP |
|R | 1 | port | Number | Mssql Server port|
|R | 1 | database | String | Mssql database |
|C | 1 | user | String | Mssql username |
|C | 1 | password | String | Mssql password |
|O | 1 | connectionTimeout | Number | Optional, in miliseconds, default is 15000|
Report
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| services.mssql[index].server | String | From configuration |
| services.mssql[index].database | String | From configuration |
| services.mssql[index].status | String | Value is
UP by default, DOWN when cannot connect to MsSql. |#### Service
casConfiguration
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :----------------------------------------------- | :-------------------- | :----------------------------------------------------------------------------
|R | 1 | seeds | String | Cassandra seeds |
|R | 1 | keyspace | Number | Cassandra keyspace |
|C | 1 | username | String | username |
|C | 1 | password | String | password |
Report
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| services.cas[index].seeds | String | From configuration |
| services.cas[index].keyspace | String | From configuration |
| services.cas[index].status | String | Value is
UP by default, DOWN when cannot connect to Cassandra. |#### Service
zkpConfiguration
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :----------------------------------------------- | :-------------------- | :----------------------------------------------------------------------------
|R | 1 | connectionString | String | Zookeeper connection string. E.g.
|Report
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| services.zkp[index].connectionString | String | From configuration |
| services.zkp[index].status | String | Value is
UP by default, DOWN when cannot connect to Zookeeper. |#### Service
kafConfiguration
| RCO | Multitude | Property | Type | Description |
| :--- | :--- | :----------------------------------------------- | :-------------------- | :----------------------------------------------------------------------------
|R | 1 | connectionString | String | Kafka connection string. E.g.
|Report
| Property | Type | Description |
| :-------------------------------------- | :------------------------ | :------------------------------------------------------------ |
| services.kaf[index].connectionString | String | From configuration |
| services.kaf[index].status | String | Value is
UP by default, DOWN when cannot connect to Kafka. |$3
Heath Check
`json
{
"status": "UP",
"max_retry": 3,
"api": [{
"name": "localhost 304",
"url": "http://localhost:9999/304",
"method": "GET",
"expectedStatusCode": "200|202|304",
"currentStatusCode": 304,
"noncritical": true,
"status": "UP"
}, {
"name": "localhost 200",
"url": "http://localhost:9999/200",
"method": "GET",
"expectedStatusCode": 200,
"currentStatusCode": 200,
"status": "UP"
}],
"mssql": [{
"server": "10.1.1.1",
"database": "Access",
"status": "UP"
}],
"cas": [{
"seeds": "10.1.1.2",
"keyspace": "dev_def",
"status": "UP"
}],
"zkp": [{
"connectionString": "10.1.1.3:2181/dev",
"status": "UP"
}],
"kaf": [{
"connectionString": "10.1.1.4:2181/dev/kafka",
"status": "UP"
}]
}
`Heath Check Offline
`json
{
"status": "DOWN",
"max_retry": 2,
"api": [{
"name": "localhost 202",
"url": "http://localhost:9999/202",
"method": "GET",
"expectedStatusCode": 202,
"currentStatusCode": 202,
"status": "UP"
}, {
"name": "localhost 202 2",
"url": "http://localhost:9999/202",
"method": "GET",
"expectedStatusCode": 202,
"currentStatusCode": 202,
"status": "UP"
}],
"mssql": [{
"server": "10.231.22.213",
"database": "Access",
"status": "UP"
}, {
"server": "10.231.22.212",
"database": "Access",
"status": "UP"
}],
"cas": [{
"seeds": "10.231.23.40",
"keyspace": "dev_def",
"status": "UP"
}, {
"seeds": "10.231.23.41",
"keyspace": "dev_def",
"status": "UP"
}],
"zkp": [{
"connectionString": "dev-usw-r1-def-h1:2181/dev",
"status": "UP"
}, {
"connectionString": "dev-usw-r2-def-h2:2181/dev",
"status": "UP"
}],
"kaf": [{
"connectionString": "dev-usw-r1-def-h1:2181/dev/kafka",
"status": "UP"
}, {
"connectionString": "dev-usw-r2-def-h2:2181/dev/kafka",
"status": "UP"
}],
"offline": true
}
``|File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
|-----------------|----------|----------|----------|----------|-------------------|
|All files | 100 | 97.41 | 100 | 100 | |
| src | 100 | 95 | 100 | 100 | |
| → consts.js | 100 | 100 | 100 | 100 | |
| → healthcheck.js | 100 | 94.44 | 100 | 100 | 66,96,99 |
| src/component | 100 | 100 | 100 | 100 | |
| → api.js | 100 | 100 | 100 | 100 | |
| → cas.js | 100 | 100 | 100 | 100 | |
| → kaf.js | 100 | 100 | 100 | 100 | |
| → mssql.js | 100 | 100 | 100 | 100 | |
| → zkp.js | 100 | 100 | 100 | 100 | |