Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Make timeout configurable #51

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```

Expand Down
32 changes: 22 additions & 10 deletions appmetrics-zipkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""
Expand Down
31 changes: 20 additions & 11 deletions test/integration/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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();
Expand Down