-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Collect healthcheck results
- Loading branch information
Showing
10 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Changelog | ||
|
||
## Unreleased | ||
- Collect healthcheck results. | ||
|
||
## 1.23.1 | ||
- Support CPU profiling in Node.js >=7.0.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
var requireHook = require('../util/requireHook'); | ||
var logger = require('../logger').getLogger('metrics/healthchecks'); | ||
|
||
var timeBetweenHealthcheckCalls; | ||
var healthy = 1; | ||
var unhealthy = 0; | ||
|
||
var adminPluginHealthcheck; | ||
var timeoutHandle; | ||
|
||
exports.payloadPrefix = 'healthchecks'; | ||
exports.currentPayload = {}; | ||
|
||
requireHook.on('admin-plugin-healthcheck', function onAdminPluginHealthcheckLoaded(_adminPluginHealthcheck) { | ||
adminPluginHealthcheck = _adminPluginHealthcheck; | ||
}); | ||
|
||
exports.activate = function(config) { | ||
timeBetweenHealthcheckCalls = config.timeBetweenHealthcheckCalls || 3000; | ||
|
||
if (adminPluginHealthcheck != null) { | ||
gatherHealthcheckResults(); | ||
} | ||
}; | ||
|
||
function gatherHealthcheckResults() { | ||
adminPluginHealthcheck.getHealthCheckResult() | ||
.then(function onHealthcheckResults(adminHealthcheckResults) { | ||
var results = {}; | ||
var previousResults = exports.currentPayload; | ||
|
||
for (var key in adminHealthcheckResults) { | ||
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); | ||
}) | ||
.catch(function onHealthcheckResultFailure(err) { | ||
exports.currentPayload = {}; | ||
logger.warn('Unexpected error while getting healthcheck results', err); | ||
timeoutHandle = setTimeout(gatherHealthcheckResults, timeBetweenHealthcheckCalls); | ||
}); | ||
} | ||
|
||
exports.deactivate = function() { | ||
clearTimeout(timeoutHandle); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
var expect = require('chai').expect; | ||
var semver = require('semver'); | ||
|
||
var agentStubControls = require('../apps/agentStubControls'); | ||
var expressControls = require('../apps/expressControls'); | ||
var config = require('../config'); | ||
var utils = require('../utils'); | ||
|
||
describe('metrics/healthchecks', function() { | ||
// admin uses JavaScript language features which aren't available in all | ||
// Node.js versions | ||
if (!semver.satisfies(process.versions.node, '>=6.0.0')) { | ||
return; | ||
} | ||
this.timeout(config.getTestTimeout()); | ||
|
||
var start = new Date().getTime(); | ||
|
||
agentStubControls.registerTestHooks(); | ||
expressControls.registerTestHooks({ | ||
enableTracing: false | ||
}); | ||
|
||
beforeEach(function() { | ||
return agentStubControls.waitUntilAppIsCompletelyInitialized(expressControls.getPid()); | ||
}); | ||
|
||
it('must report health status', function() { | ||
var healthyTimestamp; | ||
return utils.retry(function() { | ||
return agentStubControls.getLastMetricValue(expressControls.getPid(), ['healthchecks']) | ||
.then(function(healthchecks) { | ||
expect(healthchecks.configurable.healthy).to.equal(1); | ||
expect(healthchecks.configurable.since).to.be.gte(start); | ||
healthyTimestamp = healthchecks.configurable.since; | ||
}); | ||
}) | ||
.then(function() { | ||
return expressControls.setUnhealthy(); | ||
}) | ||
.then(function() { | ||
return utils.retry(function() { | ||
return agentStubControls.getLastMetricValue(expressControls.getPid(), ['healthchecks']) | ||
.then(function(healthchecks) { | ||
expect(healthchecks.configurable.healthy).to.equal(0); | ||
expect(healthchecks.configurable.since).to.be.gt(healthyTimestamp); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |