HealthCheck Modules for Compass Test Automation, One of Libraries in CTA-OSS Framework
We aim to give you brief guidelines here.
'use strict';
const HealthCheck = require('cta-healthcheck');
const instance = new HealthCheck(dependencies, configuration);
const status = {
...
};
instance.update(status);
The constructor requires dependencies and configuration.
The HealthCheck provides update() method to update the status of service.
Because the HealthCheck is a tool, the configuration is vital.
'use strict';
const configuration = {
name: 'healthcheck',
module: 'cta-healthcheck',
dependencies: {
express: 'express',
},
properties: {
file: './healths.json',
queue: 'cta.healths',
topic: 'cta.healths',
url: 'http://domain.com:3000/healthcheck',
},
};
The configuration has these following fields:
- name - defines a name of the HealthCheck tool which can be any name
- module - must be 'cta-healthcheck'
- dependencies - defines dependencies, see on dependencies section
- properties - defines properties, see on properties section
Here are examples.
'use strict';
const dependencies = {
express: 'sample.express',
messaging: 'sample.messaging',
request: 'sample.request',
};
...
const tools = [{
name: 'sample.express',
module: 'cta-expresswrapper',
properties: {
port: 8080,
},
}, {
name: 'sample.messaging',
module: 'cta-messaging',
properties: {
provider: 'rabbitmq',
parameters: {
url: 'amqp://localhost?heartbeat=60',
ack: 'auto',
},
},
}, {
name: 'sample.request',
module: 'cta-tool-request',
properties: {}
}];
The HealthCheck requires express as dependencies.
-
express (required) - defines a name of the express tool
-
messaging (optional) - defines a name of the messaging tool
-
request (optional) - defines a name of the request tool
'use strict';
const properties = {
file: './healths.json',
queue: 'cta.healths',
topic: 'cta.healths',
url: 'http://domain.com:3000/healthcheck',
};
-
file - defines full path to a file where to store healths for resiliency
-
queue - defines messaging queue name where to produce HealthCheck updates
-
topic - defines messaging topic name where to publish HealthCheck updates
-
url - defines api url of a server (basically the healthcheck data service) where to post HealthCheck updates
This shows how update() method has been using:
...
const instance = new HealthCheck(dependencies, configuration);
const status = {
...
};
instance.update(status);
The Status has the structure as following:
const status = {
name: string;
service: string;
status: string;
reason: string;
};
- name - defines the name of application
- service - defines the name of service
- status - defines the current status of service: green, yellow, and red
- green - the service is in proper status
- yellow - the service is in critical status, but it can still be used properly
- red - the service is in not-working status, but it cannot be used properly
- reason - defines the reason of status
const Brick = require('cta-brick');
class SampleBrick extends Brick {
constructor(cementHelper, config) {
super(cementHelper, config); // calling Brick's constructor
// HealthCheck is available as dependencies.
// By calling update(), the HealthCheck sends a status.
this.cementHelper.dependencies.healthcheck.update({
name: 'SampleApplication',
service: 'SampleService',
status: 'green',
});
}
}
module.exports = SampleBrick;
The Brick has HealthCheck as its dependencies. By calling Brick's constructor, the HealthCheck is available as SampleBrick's dependencies. Calling update() sends a status.
The HealthCheck provides a query via Express.
There four ways to query:
REST Query - GET :: /healthcheck
// with no mode provided
{
'status': 'green'
}
REST Query - GET :: /healthcheck?mode=full
// with 'full' mode provided
{
status: 'green',
statuses: {
SampleApplication: {
status: 'green',
current: {
services: {
alpha: {
date: '2017-12-25T18:00:00.000Z',
status: 'green'
},
beta: {
date: '2017-12-24T11:00:00.000Z',
status: 'green'
}
}
},
previous: {
services: {
alpha: {
date: '2017-12-24T12:00:00.000Z',
status: 'red'
}
}
}
}
}
}
REST Query - GET :: /healthcheck?mode=current
// with 'current' mode provided
{
status: 'green',
statuses: {
SampleApplication: {
status: 'green',
current: {
services: {
alpha: {
date: '2017-12-25T18:00:00.000Z',
status: 'green'
},
beta: {
date: '2017-12-24T11:00:00.000Z',
status: 'green'
}
}
}
}
}
}
REST Query - GET :: /healthcheck?mode=previous
// with 'previous' mode provided
{
status: 'green',
statuses: {
SampleApplication: {
status: 'green',
previous: {
services: {
alpha: {
date: '2017-12-24T12:00:00.000Z',
status: 'red'
}
}
}
}
}
}
Should we change specific, close-to-'ExpressJS' dependencies.express to a common name, dependencies.[restapp]?
const config = {
...
dependencies: {
express: 'my-express', => restapp: 'my-express',
},
...
};