-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
190 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports.trace = require('./trace'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters