Skip to content

Commit

Permalink
feat(reporter): adding reporters
Browse files Browse the repository at this point in the history
Refactor code, move data sending from collector to reporter
Adding reporter skeleton
Create default reporter
Create trace reporter
Cover trace reporter w/ tests
  • Loading branch information
gergelyke authored and Gábor Döbrei committed Jul 9, 2015
1 parent 60b1639 commit c2c52b5
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 61 deletions.
20 changes: 7 additions & 13 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ var path = require('path');

var debug = require('debug')('seetru-collector');

var superagent = require('superagent');
var microtime = require('microtime');

var getNamespace = require('continuation-local-storage').getNamespace;

var COLLECTOR_API_SAMPLE = 'http://seetru-collector-staging.herokuapp.com:80/service/sample';

/*
* @class Collector
* @constructs Collector
Expand All @@ -26,6 +23,7 @@ function Collector(options) {
this.sampleRate = 1;
this.store = {};
this.isSampled = true;
this.reporter = options.reporter;

this.on(Collector.CLIENT_SEND, this.onClientSend);
this.on(Collector.CLIENT_RECV, this.onClientReceive);
Expand Down Expand Up @@ -260,17 +258,13 @@ Collector.prototype._send = function (logFile) {

_this.sampleRate = Math.floor(data.length / _this.sampleSize) || 1;

superagent
.post(COLLECTOR_API_SAMPLE)
.set('Authorization', 'Bearer ' + _this.apiKey)
.send(dataBag)
.end(function (err) {
if (err) {
debug(err);
}
_this.reporter.send(dataBag, function (err) {
if (err) {
debug(err);
}

fs.unlink(logFile);
});
fs.unlink(logFile);
});

});

Expand Down
46 changes: 21 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var http = require('http');
var https = require('https');
var fs = require('fs');
var path = require('path');
var superagent = require('superagent');

var Collector = require('./collector');
var Shimmer = require('./Shimmer');
Expand Down Expand Up @@ -35,8 +34,7 @@ function getConfig() {
// we have no config file, let's try with ENV variables

config = {
appName: process.env.RISINGTRACE_APP_NAME,
apiKey: process.env.RISINGTRACE_API_KEY
appName: process.env.RISINGTRACE_APP_NAME
};

}
Expand All @@ -49,34 +47,19 @@ function getConfig() {
throw new Error('Missing appName');
}

if (!config.apiKey) {
throw new Error('Missing apiKey');
if (!config.reporter) {
console.warn('Missing reporter, we cannot send the report :(');
config.reporter = {
send: function () {
}
};
}

return config;
}

function getService(config, callback) {
superagent
.post(collectorConfig.collectorApi)
.set('Authorization', 'Bearer ' + config.apiKey)
.send({
name: config.appName
})
.end(function (err, res) {
if (err && err.status === 409) {
return getService(config, callback);
} else if (err) {
return callback(err);
}

return callback(null, res.body);
});
}

var config = getConfig();
var collector = new Collector(config);
collector.startCollecting();

Shimmer.wrap(http.Server.prototype, 'http.Server.prototype', ['on', 'addListener'],
function (addListener) {
Expand Down Expand Up @@ -112,17 +95,30 @@ Shimmer.wrap(https, 'https', 'request', function (original) {
return require('./wraps/http.request')(original, collector, config);
});

function setService(config, callback) {
var reporter = config.reporter;

if (!reporter.getService) {
return callback(null, {
key: config.appName
});
}

reporter.getService(callback);
}

/*
* @method seetru
*/
function seetru() {

getService(config, function (err, service) {
setService(config, function (err, service) {
if (err) {
return console.log(err);
}

collector.setService(service.key);
collector.startCollecting();
});

getOrphanTraces(config, function (err, traces) {
Expand Down
1 change: 1 addition & 0 deletions lib/reporters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.trace = require('./trace');
50 changes: 50 additions & 0 deletions lib/reporters/trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var superagent = require('superagent');
var COLLECTOR_API_SAMPLE = 'http://seetru-collector-staging.herokuapp.com:80/service/sample';
var COLLECTOR_API_SERVICE = 'http://seetru-collector-staging.herokuapp.com:80/service';

function TraceReporter (options) {
this.apiKey = options.apiKey || process.env.RISINGTRACE_API_KEY;
this.appName = options.appName || process.env.RISINGTRACE_APP_NAME;

//check if everything is ok with config
if (!this.apiKey) {
throw new Error('Missing apiKey');
}

if (!this.appName) {
throw new Error('Missing appName');
}
}

TraceReporter.prototype.send = function (data, callback) {
superagent
.post(COLLECTOR_API_SAMPLE)
.set('Authorization', 'Bearer ' + this.apiKey)
.send(data)
.end(callback);
};

TraceReporter.prototype.getService = function(callback) {
var _this = this;
superagent
.post(COLLECTOR_API_SERVICE)
.set('Authorization', 'Bearer ' + _this.apiKey)
.send({
name: _this.appName
})
.end(function (err, res) {
if (err && err.status === 409) {
return _this.getService(callback);
} else if (err) {
return callback(err);
}

return callback(null, res.body);
});
};

function create(options) {
return new TraceReporter(options);
}

module.exports.create = create;
104 changes: 104 additions & 0 deletions lib/reporters/trace.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* jshint expr:true */

var TraceReporter = require('./trace');

var expect = require('chai').expect;
var nock = require('nock');

describe('The Trace reporter module', function () {

it('exists', function () {
expect(TraceReporter).to.be.ok;
});

it('throws an error if apiKey is missing', function () {
var traceReporter;
try {
traceReporter = TraceReporter.create({
appName: 'test'
});
} catch (ex) {
expect(ex.message).to.eql('Missing apiKey');
return;
}

throw new Error('Unhandled error');
});

it('throws an error if appName is missing', function () {
var traceReporter;
try {
traceReporter = TraceReporter.create({
apiKey: 'test'
});
} catch (ex) {
expect(ex.message).to.eql('Missing appName');
return;
}

throw new Error('Unhandled error');
});

it('can be instantiated w/ appName and apiKey', function () {
var traceReporter = TraceReporter.create({
appName: 'test',
apiKey: 'test'
});

expect(traceReporter).to.be.ok;
});

it('can send data to Collector server', function () {
var options = {
appName: 'testName',
apiKey: 'testApiKey'
};

var data = {
trace: 'very data'
};

nock('http://seetru-collector-staging.herokuapp.com:80', {
reqheaders: {
'authorization': 'Bearer testApiKey'
}
})
.post('/service/sample', data)
.reply(201);

var traceReporter = TraceReporter.create(options);

traceReporter.send(data, function (err) {
expect(err).to.be.null;
});
});

it('can get the service key of appName from Collector server', function () {
var options = {
appName: 'testName',
apiKey: 'testApiKey'
};

var data = {
name: options.appName
};

nock('http://seetru-collector-staging.herokuapp.com:80', {
reqheaders: {
'authorization': 'Bearer testApiKey'
}
})
.post('/service', data)
.reply(201, {
key: 1
});

var traceReporter = TraceReporter.create(options);

traceReporter.getService(function (err, service) {
expect(err).to.be.null;
expect(service.key).to.be.eq(1);
});
});

});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
},
"devDependencies": {
"async": "^0.9.0",
"express": "^4.12.3",
"chai": "^2.2.0",
"express": "^4.12.3",
"istanbul": "^0.3.13",
"jscs": "^1.12.0",
"jshint": "^2.6.3",
"mocha": "^2.2.4",
"nock": "^2.7.0",
"pre-commit": "^1.0.6",
"sinon": "^1.14.1",
"supertest": "^0.15.0"
Expand Down
27 changes: 5 additions & 22 deletions test/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var supertest = require('supertest');
var expect = require('chai').expect;

describe('The collector', function () {
Expand All @@ -17,37 +16,21 @@ describe('The collector', function () {
throw new Error('Unhandled error');
});

it('throws an error if apiKey is missing', function () {
it('does not throw error if appName is present', function () {
process.env.RISINGTRACE_APP_NAME = 'test';
var seetru;
try {
seetru = require('../lib');
} catch (ex) {
expect(ex.message).to.eql('Missing apiKey');
return;
} finally {
//reset our env variable\
process.env.RISINGTRACE_APP_NAME = undefined;
}

throw new Error('Unhandled error');
});

it('does not throw error if apiKey and appName is present', function () {
process.env.RISINGTRACE_APP_NAME = 'test';
process.env.RISINGTRACE_API_KEY = 'test';

var seetru = require('../lib');
require('../lib');

process.env.RISINGTRACE_APP_NAME = undefined;
process.env.RISINGTRACE_API_KEY = undefined;
});

});

describe('collects orphan traces (stacks)', function () {

it('sends the traces to the trace collector api');
it('sends the traces to the trace collector api', function () {

});

});

Expand Down

0 comments on commit c2c52b5

Please sign in to comment.