pac-health-check v1.1.1
Check
Install
$ npm install pac-health-check
Usage
Health Check by Cron Job
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: "<host>:<port>/<root>"
}],
kaf: [{
connectionString: "<host>:<port>/<root>/<kafka root>"
}]
}
};
//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)
Healch Check on Demand
healthCheck.reportOnDemand(configuration)
It is recommended to set a ttl configuration.ttl
(in seconds).
If ttl
is set, healthCheck will leverage local cache.
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);
});
Health Check Offline
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 tottl
)
Specification
Common Configuration Object
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 Servicemssql
: Microsoft SQL Server Servicecas
: Cassandra Servicezkp
: Zookeeper Servicekaf
: kafka Service
Common Application Report Object
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
isUP
, Whenservices.{service}[index].status
isUP
in all services.status
isDOWN
, When any nonnoncritical
serviseservices.{service}[index].status
isDOWN
.status
isDEGRADED
, only if anynoncritical
serviseservices.{service}[index].status
isDOWN
.
Service api
Configuration
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.apiindex.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.apiindex.name | String | From configuration |
services.apiindex.url | String | From configuration |
services.apiindex. method | String | From configuration |
services.apiindex.status | String | Check Api Status |
services.apiindex.expectedStatusCode | String | From configuration. |
services.apiindex.currentStatusCode | Number | Actaul http status from response. |
Api Status
report.api[index].status
isUP
by default.report.api[index].status
isDOWN
, when health check resultactualStatusCode
is not matchingexpectedStatusCode
.
Service mssql
Configuration
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.mssqlindex.server | String | From configuration |
services.mssqlindex.database | String | From configuration |
services.mssqlindex.status | String | Value is UP by default, DOWN when cannot connect to MsSql. |
Service cas
Configuration
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.casindex.seeds | String | From configuration |
services.casindex.keyspace | String | From configuration |
services.casindex.status | String | Value is UP by default, DOWN when cannot connect to Cassandra. |
Service zkp
Configuration
RCO | Multitude | Property | Type | Description |
---|---|---|---|---|
R | 1 | connectionString | String | Zookeeper connection string. E.g. <host>:<port>/<root> |
Report
Property | Type | Description |
---|---|---|
services.zkpindex.connectionString | String | From configuration |
services.zkpindex.status | String | Value is UP by default, DOWN when cannot connect to Zookeeper. |
Service kaf
Configuration
RCO | Multitude | Property | Type | Description |
---|---|---|---|---|
R | 1 | connectionString | String | Kafka connection string. E.g. <host>:<port>/<root>/<kafka root> |
Report
Property | Type | Description |
---|---|---|
services.kafindex.connectionString | String | From configuration |
services.kafindex.status | String | Value is UP by default, DOWN when cannot connect to Kafka. |
Sample Report
Heath Check
{
"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
{
"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
}
Test
Test Coverage
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 |
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago