fh-health
Module to add health checks to an application. This is a health check to verify the running application (connectivity, etc.) is ok, and not a replacemnt for actual testing. The health check returns a JSON response formatted as shown below (using a single whitespace between key and values):
The "details" Array contains objects describing the results of each test which are formatted like so.
Usage
If running within fh-nodeapp the module should be initialised from your main.js file as shown below. This will setup a new endpoint in your application called "health", so ensure none of your endpoints are called health to avoid conflicts. Alternatively you can just call health.init() and manage the endpoint yourself.
// With fh-nodeappvar health = ;// This will add a health endpoint automatically.health; // Standard usage with an express appvar health = ;health;app;
Adding Tests
Adding tests is done via two functions. addTest(description, testFn) and addCriticalTest(description, testFn). The testFn function is a function that must have the format:
{ // ...Do some stuff... // ................... ifanErrorOccurred return ; else return ; } health;
Critical Tests - addCriticalTest(name, fn)
Are those that result in the health endpoint returning a "crit" status if they pass a non null err argument (the first argument) to their callback. If a critical test has no issues then they have a status of "ok".
Standard Tests - addTest(name, fn)
Added via addTest are tests that can return an error to their callback without causing a "crit" status, but will instead cause a "warn" status.
Simple Example
var request = ;var health = ;health; health;
This example if successful would return the following response:
status: 'ok' summary: 'No issues to report. All tests passed without error' details: description: 'Test a request to www.google.com is successful' test_status: 'ok' result: 'Successfully loaded google.com' runtime: 2106
If this example encountered a status code that wasn't 200 the following would be returned:
status: 'warn' summary: 'Some non-critical tests encountered issues. See the "details" object for specifics.' details: description: 'Test a request to www.google.com is successful' test_status: 'warn' result: 'Google responded with a status code of {CODE}' runtime: 2341
Timeouts
The default timeout for running tests is 25 seconds. After 25 seconds the test runner will ignore results returned from any unfinished tests.
The timeout can be modified like so:
// Set the max running time to 60 secondsvar health = ;health;
If a timeout occurs on a critical test then the overall status returned will be "crit". If a timeout occurs on a regular test then a status of "warn" will be returned.
Usage Pattern
You can include test cases in separate modules which is perfectly valid, or alternatively have all tests in a single file.
index.js
var health = ;health; var app = ;app;
myOtherModule.js
var health = ; health;health;
myOtherOtherModule.js
var health = ; health;