diff --git a/README.md b/README.md index 6f78513..a3308e0 100755 --- a/README.md +++ b/README.md @@ -12,14 +12,15 @@ Unlike other zipkin instrumentation packages, appmetrics-zipkin will automatical ## Configure Zipkin Endpoint Connecting to a [Zipkin](https://github.com/openzipkin/zipkin) endpoint is done by adding the desired hostname and port to `appmetrics-zipkin.properties` file. -Alternatively, the hostname, port and service name (used by Zipkin to identify your application) can be added when including appmetrics-zipkin into your application: +Alternatively, the hostname, port, timeout (optional) and service name (used by Zipkin to identify your application) can be added when including appmetrics-zipkin into your application: ```js var appzip = require('appmetrics-zipkin')({ host: 'localhost', port: 9411, serviceName:'frontend', - sampleRate: 1.0 + sampleRate: 1.0, + timeout: 5000 }); ``` diff --git a/appmetrics-zipkin.js b/appmetrics-zipkin.js index 54efd22..237e2e8 100755 --- a/appmetrics-zipkin.js +++ b/appmetrics-zipkin.js @@ -53,13 +53,14 @@ module.exports = function(options) { function start(options) { // Set up the zipkin - var host, port, serviceName, sampleRate; + var host, port, serviceName, sampleRate, timeout; if (options) { host = options['host']; port = options['port']; serviceName = options['serviceName']; sampleRate = options['sampleRate']; + timeout = options['timeout']; } // Uses properties from file if present @@ -76,6 +77,9 @@ function start(options) { if (properties.get('sampleRate')) { sampleRate = properties.get('sampleRate'); } + if (properties.get('timeout')) { + timeout = properties.get('timeout'); + } } if (!serviceName) { @@ -90,17 +94,25 @@ function start(options) { if (!sampleRate) { sampleRate = 1.0; } + if (!timeout) { + timeout = 5000; + } // Test if the host & port are valid - tcpp.probe(host, port, function(err, available) { - if (err) { - console.log('Unable to contact Zipkin at ' + host + ':' + port); - return; - } - if (!available) { - console.log('Unable to contact Zipkin at ' + host + ':' + port); - } - }); + tcpp.ping( + { + address: host, + port, + timeout, + attempts: 1, + }, + function(_err, data) { + var available = data.min !== undefined; + + if (!available) { + console.log('Unable to contact Zipkin at ' + host + ':' + port); + } + }); const zipkinUrl = `http://${host}:${port}`; const recorder = new BatchRecorder({ diff --git a/package.json b/package.json index 0f5fcbe..c01df97 100755 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "scripts": { "test": "npm run test:unit && npm run test:integration", "test:unit": "mocha test/unit/", - "test:integration": "mocha -t 3000 test/integration/", + "test:integration": "mocha -t 5000 test/integration/", "pretest": "eslint .", "pretravis": "eslint .", "travis": "" diff --git a/test/integration/request.test.js b/test/integration/request.test.js index ce53109..46262c2 100644 --- a/test/integration/request.test.js +++ b/test/integration/request.test.js @@ -14,7 +14,7 @@ function waitAndGetTraces() { return new Promise(resolve => { setTimeout(() => { // We want to let all background requests going to zipkin complete resolve(getTraces({ zipkinHost, zipkinPort, serviceName })); - }, 1000); + }, 2000); }); } @@ -23,19 +23,28 @@ describe('http requests', () => { let server; let http; + const startupDelay = 2000; + before(() => { - require('../../')({ - host: zipkinHost, - port: zipkinPort, - sampleRate: zipkinSampleRate, - serviceName - }); + return new Promise((resolve) => { - http = require('http'); - return createServer({ http, port: 3000 }) - .then(createdServer => { - server = createdServer; + require('../../')({ + host: zipkinHost, + port: zipkinPort, + sampleRate: zipkinSampleRate, + serviceName }); + + http = require('http'); + createServer({ http, port: 3000 }) + .then(createdServer => { + server = createdServer; + + setTimeout(() => { + return resolve(); + }, startupDelay); + }); + }); }); after(() => { if (server) server.close();