Skip to content

Commit 8924e36

Browse files
committed
Merge pull request #28 from RisingStack/feature/trace-reporter
feat(trace-reporter): use JSON to send data to Trace
2 parents f1460a7 + 07a6fdd commit 8924e36

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

lib/reporters/trace.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var http = require('http');
22
var url = require('url');
3-
var qs = require('qs');
43

54
var config = require('../config');
65

@@ -31,7 +30,8 @@ TraceReporter.prototype.send = function (data, callback) {
3130
path: opts.path,
3231
method: 'POST',
3332
headers: {
34-
'Authorization': 'Bearer ' + this.apiKey
33+
'Authorization': 'Bearer ' + this.apiKey,
34+
'Content-Type': 'application/json'
3535
}
3636
}, function () {
3737
return callback(null);
@@ -40,7 +40,7 @@ TraceReporter.prototype.send = function (data, callback) {
4040
return callback(err);
4141
});
4242

43-
req.write(qs.stringify(data));
43+
req.write(JSON.stringify(data));
4444
req.end();
4545
};
4646

@@ -55,12 +55,19 @@ TraceReporter.prototype.getService = function(callback) {
5555
path: opts.path,
5656
method: 'POST',
5757
headers: {
58-
'Authorization': 'Bearer ' + this.apiKey
58+
'Authorization': 'Bearer ' + this.apiKey,
59+
'Content-Type': 'application/json'
5960
}
6061
}, function (res) {
6162
res.setEncoding('utf8');
6263
res.on('data', function (chunk) {
63-
return callback(null, JSON.parse(chunk));
64+
var res;
65+
try {
66+
res = JSON.parse(chunk);
67+
} catch (ex) {
68+
return callback(ex);
69+
}
70+
return callback(null, res);
6471
});
6572
})
6673
.on('error', function (err) {
@@ -71,7 +78,7 @@ TraceReporter.prototype.getService = function(callback) {
7178
}
7279
});
7380

74-
req.write(qs.stringify({
81+
req.write(JSON.stringify({
7582
name: _this.appName
7683
}));
7784
req.end();

lib/reporters/trace.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var TraceReporter = require('./trace');
22

33
var expect = require('chai').expect;
44
var nock = require('nock');
5-
var qs = require('qs');
65

76
var config = require('../config');
87

@@ -65,10 +64,11 @@ describe('The Trace reporter module', function () {
6564

6665
nock(collectorApi, {
6766
reqheaders: {
68-
'Authorization': 'Bearer testApiKey'
67+
'Authorization': 'Bearer testApiKey',
68+
'Content-Type': 'application/json'
6969
}
7070
})
71-
.post(collectorApiSampleEndpoint, qs.stringify(data))
71+
.post(collectorApiSampleEndpoint, JSON.stringify(data))
7272
.reply(201);
7373

7474
var traceReporter = TraceReporter.create(options);
@@ -91,10 +91,11 @@ describe('The Trace reporter module', function () {
9191

9292
nock(collectorApi, {
9393
reqheaders: {
94-
'Authorization': 'Bearer testApiKey'
94+
'Authorization': 'Bearer testApiKey',
95+
'Content-Type': 'application/json'
9596
}
9697
})
97-
.post(collectorApiServiceEndpoint, qs.stringify(data))
98+
.post(collectorApiServiceEndpoint, JSON.stringify(data))
9899
.reply(201, {
99100
key: 1
100101
});

0 commit comments

Comments
 (0)