-
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.
feat(reporter): add logstash reporter
Add new reporter for logstash Cover with tests
- Loading branch information
Gábor Döbrei
committed
Jul 10, 2015
1 parent
7a16367
commit 4037096
Showing
4 changed files
with
118 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
module.exports.trace = require('./trace'); | ||
module.exports.logstash = require('./logstash'); |
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,40 @@ | ||
var async = require('async'); | ||
var Logstash = require('logstash-client'); | ||
|
||
function LogstashReporter (options) { | ||
this.type = options.type || process.env.LOGSTASH_TYPE || 'tcp'; | ||
this.host = options.host || process.env.LOGSTASH_HOST; | ||
this.port = options.port || process.env.LOGSTASH_PORT; | ||
|
||
//check if everything is ok with config | ||
if (!this.host) { | ||
throw new Error('Missing host'); | ||
} | ||
|
||
if (!this.port) { | ||
throw new Error('Missing port'); | ||
} | ||
|
||
this.logstashClient = new Logstash({ | ||
type: this.type, | ||
host: this.host, | ||
port: this.port, | ||
format: function (line) { | ||
line['@timestamp'] = new Date(); | ||
return JSON.stringify(line) + '\n'; | ||
} | ||
}); | ||
} | ||
|
||
LogstashReporter.prototype.send = function (data, callback) { | ||
var _logstashClient = this.logstashClient; | ||
async.each(data, function (line, cb) { | ||
_logstashClient.send(line, cb); | ||
}, callback); | ||
}; | ||
|
||
function create(options) { | ||
return new LogstashReporter(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,75 @@ | ||
/* jshint expr:true */ | ||
|
||
var LogstashReporter = require('./logstash'); | ||
|
||
var expect = require('chai').expect; | ||
var sinon = require('sinon'); | ||
|
||
describe('The Logstash reporter module', function () { | ||
|
||
it('exists', function () { | ||
expect(LogstashReporter).to.be.ok; | ||
}); | ||
|
||
it('throws an error if host is missing', function () { | ||
var logstashReporter; | ||
try { | ||
logstashReporter = LogstashReporter.create({ | ||
port: 12201 | ||
}); | ||
} catch (ex) { | ||
expect(ex.message).to.eql('Missing host'); | ||
return; | ||
} | ||
|
||
throw new Error('Unhandled error'); | ||
}); | ||
|
||
it('throws an error if port is missing', function () { | ||
var logstashReporter; | ||
try { | ||
logstashReporter = LogstashReporter.create({ | ||
host: 'localhost' | ||
}); | ||
} catch (ex) { | ||
expect(ex.message).to.eql('Missing port'); | ||
return; | ||
} | ||
|
||
throw new Error('Unhandled error'); | ||
}); | ||
|
||
it('can be instantiated w/ host and port', function () { | ||
var logstashReporter = LogstashReporter.create({ | ||
host: 'localhost', | ||
port: 12201 | ||
}); | ||
|
||
expect(logstashReporter).to.be.ok; | ||
}); | ||
|
||
it('can send data to Logstash', function () { | ||
var options = { | ||
host: 'localhost', | ||
port: 15323 | ||
}; | ||
|
||
var logstashReporter = LogstashReporter.create(options); | ||
|
||
var data = [{ | ||
trace: 'very data' | ||
}]; | ||
|
||
sinon.stub(logstashReporter.logstashClient.transport, 'send', function (raw, cb) { | ||
expect(typeof raw).to.be.eq('string'); | ||
var json = JSON.parse(raw); | ||
expect(json['@timestamp']).to.be.ok; | ||
expect(json.trace).to.be.eq('very data'); | ||
cb(null); | ||
}); | ||
|
||
logstashReporter.send(data, function (err) { | ||
expect(err).to.be.null; | ||
}); | ||
}); | ||
}); |
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