Skip to content

Commit

Permalink
Merge pull request #7 from statful/remove-request-lib-dependency
Browse files Browse the repository at this point in the history
Remove request lib dependency
  • Loading branch information
mistic authored Oct 6, 2016
2 parents aee595f + c0c83b6 commit e2a1e5d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function (grunt) {
lines: 93,
statements:93,
branches: 83,
functions: 94
functions: 93
},
root: './lib',
reportFormats: ['lcov']
Expand Down
43 changes: 31 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

var dgram = require('dgram'),
blocked = require('blocked'),
request = require('request').defaults({
forever: false
}),
https = require('https'),
URL = require('url'),
httpsAgent = new https.Agent({ keepAlive: true }),
merge = require('merge'),
zlib = require('zlib'),
Readable = require('stream').Readable,
Expand Down Expand Up @@ -60,17 +60,36 @@ function getGzipStreamFromString(string) {
* @returns {*|exports}
*/
function performRequest(options, logger) {
return request(options, function callback(error, response) {
if (logger) {
if (error) {
logger.error('An error occurred: ' + error);
}
var url = URL.parse(options.url);
var req;

if (response && response.statusCode !== 201) {
logger.error('Unexpected status: ' + response.statusCode);
}
}
options.agent = httpsAgent;
options.hostname = url.hostname;
options.port = url.port;
options.path = url.path;

req = https.request(options, function callback(res) {
res.setEncoding('utf8');
});

req.setTimeout(options.timeout);

req.on('error', function(e) {
logger.error('An error occurred: ' + e.message);
});

req.on('timeout', function(){
logger.error('A timeout occurred on a request to host: ' + options.hostname +
' port: ' + options.port + ' and path: ' + options.path);
});

// write data to request body and end the request if is an uncompressed request and has a body
if (options.body) {
req.write(options.body);
req.end();
}

return req;
}

/**
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"dependencies": {
"blocked": "^1.1.0",
"merge": "^1.2.0",
"request": "^2.63.0",
"unique-concat": "^0.2.2"
},
"devDependencies": {
Expand Down
48 changes: 40 additions & 8 deletions spec/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ var expect = require('chai').expect;
var sinon = require('sinon');

describe('When sending metrics', function () {
var httpPort = Math.floor(Math.random() * 20000) + 10001;
var udpPort = Math.floor(Math.random() * 10000) + 1000;
var apiConf = {
host: '127.0.0.1',
port: httpPort,
token: 'my-token'
};
var httpPort;
var udpPort;
var apiConf;

beforeEach(function(){
httpPort = Math.floor(Math.random() * 20000) + 10001;
udpPort = Math.floor(Math.random() * 10000) + 1000;
apiConf = {
host: '127.0.0.1',
port: httpPort,
token: 'my-token'
};
});

it('should send metrics through UDP', function (done) {
// Given
Expand Down Expand Up @@ -669,16 +675,41 @@ describe('When sending metrics', function () {
}
});

it('should send uncompressed aggregated metrics of same agg and aggFreq through HTTPS', function (done) {
// Given
httpsServer.start(httpPort, '127.0.0.1', onResponse);

var victim = new Client({
systemStats: false,
transport: 'api',
api: apiConf,
flushSize: 2
}, logger);

// When
victim.aggregatedPut('my_metric1', 1, 'avg', 60);
victim.aggregatedPut('my_metric2', 1, 'avg', 60);

// Then
function onResponse(lines) {
httpsServer.stop();

expect(lines.toString()).to.match(/^application\.my_metric1 1 \d+\napplication\.my_metric2 1 \d+$/);
done();
}
});

it('should send aggregated metrics even with lower global sample rate defined', function (done) {
// Given
var randomStub = sinon.stub(Math, 'random').returns(0.01);
httpsServer.start(httpPort, '127.0.0.1', onResponse);

var victim = new Client({
systemStats: false,
transport: 'api',
api: apiConf,
flushSize: 2,
sampleRate: 1
sampleRate: 10
}, logger);

// When
Expand All @@ -687,6 +718,7 @@ describe('When sending metrics', function () {

// Then
function onResponse(lines) {
randomStub.restore();
httpsServer.stop();

expect(lines.toString()).to.match(/^application\.my_metric1 1 \d+\napplication\.my_metric2 1 \d+$/);
Expand Down

0 comments on commit e2a1e5d

Please sign in to comment.