healthchecks
Advanced tools
Comparing version 1.4.4 to 1.4.5
@@ -25,5 +25,5 @@ // Exports an Express middleware factory. | ||
const Handlebars = require('handlebars'); | ||
const HTTP = require('http'); | ||
const ms = require('ms'); | ||
const Path = require('path'); | ||
const request = require('request'); | ||
const URL = require('url'); | ||
@@ -37,3 +37,3 @@ | ||
// Represents a check outcome with the properties url, reason, etc. | ||
function Outcome(url, expected, error, response, body, elapsed) { | ||
function Outcome(url, expected, error, statusCode, body, elapsed) { | ||
this.url = url; | ||
@@ -49,5 +49,5 @@ this.elapsed = elapsed; | ||
this.statusCode = response.statusCode; | ||
this.statusCode = statusCode; | ||
this.body = body; | ||
if (response.statusCode < 200 || response.statusCode >= 400) | ||
if (statusCode < 200 || statusCode >= 400) | ||
this.reason = 'statusCode'; | ||
@@ -64,4 +64,28 @@ else { | ||
} | ||
this.log(); | ||
} | ||
// Log outcome, visible when DEBUG=healthchecks | ||
Outcome.prototype.log = function() { | ||
switch (this.reason) { | ||
case 'error': { | ||
debug('%s: Server responded with error %s', this.url, this.error); | ||
break; | ||
} | ||
case 'timeout': { | ||
debug('%s: Server response timeout', this.url); | ||
break; | ||
} | ||
case 'statusCode': { | ||
break; | ||
} | ||
case 'body': { | ||
debug('%s: Server response did not contain expected text', this.url); | ||
break; | ||
} | ||
} | ||
}; | ||
// Easy way to log check outcome, shows the URL and the reason check failed | ||
@@ -77,2 +101,3 @@ Outcome.prototype.toString = function() { | ||
case undefined: { | ||
debug('%s: Server responded with status code %s', this.url, this.statusCode); | ||
return this.url; | ||
@@ -112,44 +137,46 @@ } | ||
checkURL = URL.resolve(protocol + '://localhost/', checkURL); | ||
const requestURI = _.assign(URL.parse(checkURL), { host: null, port: port, hostname: hostname }); | ||
const headers = { | ||
'host': URL.parse(checkURL).hostname, | ||
'user-agent': 'Mozilla/5.0 (compatible) Healthchecks http://broadly.com' | ||
const requestURL = _.assign(URL.parse(checkURL), { host: null, port: port, hostname: hostname }); | ||
const request = { | ||
hostname: requestURL.hostname, | ||
port: requestURL.port, | ||
path: requestURL.path, | ||
headers: { | ||
'Host': URL.parse(checkURL).hostname, | ||
'User-Agent': 'Mozilla/5.0 (compatible) Healthchecks http://broadly.com', | ||
'X-Request-Id': requestID || '' | ||
} | ||
}; | ||
if (requestID) | ||
headers['X-Request-Id'] = requestID; | ||
const params = { | ||
uri: URL.format(requestURI), | ||
headers: headers, | ||
timeout: timeout | ||
}; | ||
const start = Date.now(); | ||
request(params, function(error, response, body) { | ||
HTTP.get(request) | ||
.on('response', function(response) { | ||
const elapsed = Date.now() - start; | ||
if (response.statusCode < 200 || response.statusCode >= 400) | ||
resolve(new Outcome(checkURL, expected, null, response.statusCode, null, elapsed)); | ||
else { | ||
const buffers = []; | ||
response.on('data', function(buffer) { | ||
buffers.push(buffer); | ||
}); | ||
response.on('end', function() { | ||
const body = Buffer.concat(buffers).toString(); | ||
resolve(new Outcome(checkURL, expected, null, response.statusCode, body, elapsed)); | ||
}); | ||
} | ||
const elapsed = Date.now() - start; | ||
const outcome = new Outcome(checkURL, expected, error, response, body, elapsed); | ||
resolve(outcome); | ||
}) | ||
.on('error', function(error) { | ||
const elapsed = Date.now() - start; | ||
resolve(new Outcome(checkURL, expected, error, null, null, elapsed)); | ||
}); | ||
switch (outcome.reason) { | ||
case 'error': { | ||
debug('Server responded with error ' + error.message, checkURL); | ||
break; | ||
} | ||
case 'timeout': { | ||
debug('Server response timeout', checkURL); | ||
break; | ||
} | ||
case 'statusCode': { | ||
debug('Server responded with status code ' + response.statusCode, checkURL); | ||
break; | ||
} | ||
case 'body': { | ||
debug('Server response did not contain expected text', checkURL); | ||
break; | ||
} | ||
} | ||
}); | ||
setTimeout(function() { | ||
const elapsed = Date.now() - start; | ||
const error = new Error('ETIMEDOUT'); | ||
error.code = 'ETIMEDOUT'; | ||
resolve(new Outcome(checkURL, expected, error, null, null, elapsed)); | ||
}, timeout); | ||
}); | ||
}); | ||
// Run all checks in parallel | ||
@@ -190,3 +217,3 @@ const allOutcomes = Promise.all(allChecks); | ||
const match = line.match(/^(\S+)\s*(.*)/); | ||
debug('Added check', match[1], match[2]); | ||
//debug('Added check %s %s', match[1], match[2]); | ||
return { | ||
@@ -217,2 +244,3 @@ url: match[1], | ||
}; | ||
debug('Added %d checks', checks.length); | ||
@@ -259,6 +287,6 @@ return checkFunction.bind(context); | ||
// Run all checks | ||
debug('Health checks: running against ' + protocol + '://' + hostname + ':' + port + ' with request-ID ' + requestID); | ||
debug('Running against %s://%s:%d with request-ID %s', protocol, hostname, port, requestID); | ||
runChecks(protocol, hostname, port, requestID) | ||
.then(function(outcomes) { | ||
debug('Health checks: ' + outcomes.passed.length + ' passed and ' + outcomes.failed.length + ' failed'); | ||
debug('%d passed and %d failed', outcomes.passed.length, outcomes.failed.length); | ||
@@ -265,0 +293,0 @@ // Respond with 200 only if all checks passed |
{ | ||
"name": "healthchecks", | ||
"version": "1.4.4", | ||
"version": "1.4.5", | ||
"scripts": { | ||
@@ -10,13 +10,13 @@ "test": "mocha" | ||
"bluebird": "^2.0", | ||
"debug": "^2.0", | ||
"debug": "^2.2.0", | ||
"handlebars": "^3.0", | ||
"lodash": "^3.0", | ||
"ms": "^0.7.0", | ||
"request": "^2.0" | ||
"ms": "^0.7.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^0.19.0", | ||
"eslint": "^0.21.0", | ||
"express": "^4.12.3", | ||
"mocha": "^2.2.4", | ||
"zombie": "^4.0.7" | ||
"request": "^2.0", | ||
"zombie": "^4.0.10" | ||
}, | ||
@@ -23,0 +23,0 @@ "description": "Express middleware that runs health checks on your application", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34771
5
633
5
1
- Removedrequest@^2.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.13.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
Updateddebug@^2.2.0
Updatedms@^0.7.1