Skip to content

Commit

Permalink
fix(reporter): adding sync data sending
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyke committed Sep 12, 2015
1 parent cd4e533 commit 3507692
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
23 changes: 15 additions & 8 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ Collector.prototype.onCrash = function (data) {

this.traces.push(trace);

this._send();
this._send({
isSync: true
});
};

Collector.prototype.onClientSend = function (data) {
Expand Down Expand Up @@ -177,7 +179,8 @@ Collector.prototype.report = function (data) {
this.partials[traceId].events.push(dataToSend);
};

Collector.prototype._send = function () {
Collector.prototype._send = function (options) {
options = options || {};
debug('sending logs to the trace service');
if (this.traces.length > 0) {
var dataBag = {};
Expand All @@ -188,12 +191,16 @@ Collector.prototype._send = function () {

this.traces = [];

this.reporter.send(dataBag, function (err) {
debug('logs sent to the trace service');
if (err) {
return debug(err);
}
});
if (options.isSync) {
this.reporter.sendSync(dataBag);
} else {
this.reporter.send(dataBag, function (err) {
debug('logs sent to the trace service');
if (err) {
return debug(err);
}
});
}
}
};

Expand Down
14 changes: 14 additions & 0 deletions lib/reporters/trace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var https = require('https');
var url = require('url');
var requestSync = require('sync-request');
var debug = require('debug')('risingstack/trace');

var bl = require('bl');
Expand Down Expand Up @@ -27,6 +28,19 @@ function TraceReporter (options) {
}
}

// USE THIS WITH CAUTION, IT WILL BE BLOCKING
TraceReporter.prototype.sendSync = function (data) {
requestSync('POST', COLLECTOR_API_SAMPLE, {
json: data,
headers: {
'Authorization': 'Bearer ' + this.apiKey,
'Content-Type': 'application/json',
'X-Reporter-Version': package.version
},
timeout: 100
});
};

TraceReporter.prototype.send = function (data, callback) {

var opts = url.parse(COLLECTOR_API_SAMPLE);
Expand Down
13 changes: 13 additions & 0 deletions lib/reporters/trace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe('The Trace reporter module', function () {
expect(traceReporter).to.be.ok;
});

it.skip('can send data to the Collector server synchronously', function () {
var traceReporter = TraceReporter.create({
appName: 'test',
apiKey: 'test'
});

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

traceReporter.sendSync(data);
});

it('can send data to Collector server', function (done) {
var options = {
appName: 'testName',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"logstash-client": "^1.0.0",
"microtime": "^2.0.0",
"node-uuid": "^1.4.3",
"qs": "^4.0.0"
"qs": "^4.0.0",
"sync-request": "^2.0.1"
},
"devDependencies": {
"async": "^1.4.2",
Expand Down

0 comments on commit 3507692

Please sign in to comment.