Rutilus - API Heartbeat - Node
This module makes constant requests to urls and logs whether the response was successful and the response time.
The module can be set to throw errors if the URL does not respond in order to crash containers.
Example: if we are deploying a cluster of containers with several APIs and want to make sure all the APIs are responding properly, we can set up this module to watch all the APIs and throw an error to crash the container (which will be restarted) in case one of them stops responding.
A request will be considered unsuccessful if the application does not receive a response before the next request. For example: if we set the API Heartbeat to make one request every 10 seconds, a response that takes more than 10 secons to be received will be considered unsuccessful.
This module does not have dependencies.
Installing
npm install rutilus_apiheartbeat_node --save
Using
Require the module in the project and call it by passing 2 parameters, the last one being optional:
First parameter
An array of objects that describe the APIs to be monitored:
[
{
title: String (optional, defaults to "Untitled"),
address: String,
useHttps: true (optional, defaults to "false"),
logDirectory: '/logs' (optional, defaults to ""),
logFile: String,
timeSpan: Number (optional, defaults to "10000"),
maxFileSizeBytes: Number (optional, defaults to "10000000"),
crashWhenFail: Boolean (optional, defaults to "false"),
}
]
- title: A meaningful title for the API
- address: The URL of the api
- useHttps: Use HTTP or HTTPS for making the request?
- logDirectory: Directory where the logs must be saved
- logFile: Name of the file for the logs
- timeSpan: Time span (ms) for every heartbeat. Minimum: 10 seconds.
- maxFileSizeBytes: Maximum size for the log file - when the size exceeds the one specified, the old logs get archived ("ARCHIVE_" + log file name). If there already is an archived version, it gets overwritten.
- crashWhenFail: If set to true, when an API fails, this module will throw an error. Useful for crashing containers and making them restart.
Second parameter (optional, defaults to 0)
A number that delays the start of the heartbeat.
Example: 10000 (for 10 seconds)
Example
const APIHeartbeat = require('rutilus_apiheartbeat_node');
APIHeartbeat([{
title: 'My API 1',
address: 'https://www.api1.com/',
useHttps: true,
logDirectory: '/logs',
logFile: 'api1.log',
timeSpan: 10000,
maxFileSizeBytes: 1000000,
crashWhenFail: true,
}, {
title: 'My API 2',
address: 'http://www.api2.com:3000/',
useHttps: false,
logDirectory: '/logs/api2',
logFile: 'api2.log',
timeSpan: 60000,
}], 5000);