-
Notifications
You must be signed in to change notification settings - Fork 36
/
healthchecks.js
66 lines (55 loc) · 2.09 KB
/
healthchecks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var requireHook = require('@instana/core').util.requireHook;
var logger;
logger = require('../logger').getLogger('metrics/healthchecks', function(newLogger) {
logger = newLogger;
});
var timeBetweenHealthcheckCalls;
var healthy = 1;
var unhealthy = 0;
var adminPluginHealthcheck;
var timeoutHandle;
exports.payloadPrefix = 'healthchecks';
exports.currentPayload = {};
requireHook.onModuleLoad('admin-plugin-healthcheck', function onAdminPluginHealthcheckLoaded(_adminPluginHealthcheck) {
adminPluginHealthcheck = _adminPluginHealthcheck;
});
exports.activate = function(config) {
timeBetweenHealthcheckCalls = config.metrics.timeBetweenHealthcheckCalls;
if (adminPluginHealthcheck != null) {
gatherHealthcheckResults();
}
};
function gatherHealthcheckResults() {
adminPluginHealthcheck
.getHealthCheckResult()
.then(function onHealthcheckResults(adminHealthcheckResults) {
var results = {};
var previousResults = exports.currentPayload;
// eslint-disable-next-line no-restricted-syntax
for (var key in adminHealthcheckResults) {
// eslint-disable-next-line no-prototype-builtins
if (adminHealthcheckResults.hasOwnProperty(key)) {
var result = adminHealthcheckResults[key];
var checkHealthy = result.healthy ? healthy : unhealthy;
var changed = previousResults[key] == null || previousResults[key].healthy !== checkHealthy;
results[key] = {
healthy: checkHealthy,
since: changed ? new Date().getTime() : previousResults[key].since
};
}
}
exports.currentPayload = results;
timeoutHandle = setTimeout(gatherHealthcheckResults, timeBetweenHealthcheckCalls);
timeoutHandle.unref();
})
.catch(function onHealthcheckResultFailure(err) {
exports.currentPayload = {};
logger.warn('Unexpected error while getting healthcheck results', err);
timeoutHandle = setTimeout(gatherHealthcheckResults, timeBetweenHealthcheckCalls);
timeoutHandle.unref();
});
}
exports.deactivate = function() {
clearTimeout(timeoutHandle);
};