Permalink
Browse files

Do not emit an error when a field is missing

  • Loading branch information...
1 parent adff984 commit 544afd9849d907d072fbc0db8a1a971744c9923d Bertrand Paquet committed Oct 13, 2012
Showing with 18 additions and 12 deletions.
  1. +2 −2 lib/lib/base_component.js
  2. +12 −7 lib/outputs/output_statsd.js
  3. +4 −3 test/test_40_integration.js
@@ -110,8 +110,8 @@ BaseComponent.prototype.replaceByFields = function(data, s) {
replaced = data['@fields'][key];
}
if (!replaced) {
- this.emit('error', new Error('Unable to find field: ' + key));
- replaced = '';
+ logger.debug('Unable to find field', key);
+ return undefined;
}
return this.replaceByFields(data, result[1] + replaced + result[3]);
}
@@ -57,13 +57,18 @@ OutputElasticStatsd.prototype.afterLoadConfig = function(callback) {
OutputElasticStatsd.prototype.process = function(data) {
var raw = this.replaceByFields(data, this.raw);
- logger.debug('Send to statsd packet', raw);
- var message = new Buffer(raw);
- this.client.send(message, 0, message.length, this.port, this.host, function(err, bytes) {
- if (err || bytes != message.length) {
- this.error_buffer.emit('error', new Error('Error while send data to statsd:' + err));
- }
- }.bind(this));
+ if (raw) {
+ logger.debug('Send to statsd packet', raw);
+ var message = new Buffer(raw);
+ this.client.send(message, 0, message.length, this.port, this.host, function(err, bytes) {
+ if (err || bytes != message.length) {
+ this.error_buffer.emit('error', new Error('Error while send data to statsd:' + err));
+ }
+ }.bind(this));
+ }
+ else {
+ logger.debug('Unable to replace fields on', this.raw, 'input', data);
+ }
}
OutputElasticStatsd.prototype.close = function(callback) {
@@ -328,10 +328,12 @@ vows.describe('Integration :').addBatch({
statsd.bind(17878);
createAgent([
'input://file://input1.txt',
+ 'filter://regex://?regex=(line2)&fields=unknown_field',
'output://statsd://127.0.0.1:17878?metric_type=increment&metric_key=toto.bouh.#{unknown_field}',
], function(agent) {
setTimeout(function() {
fs.appendFileSync('input1.txt', 'line1\n');
+ fs.appendFileSync('input1.txt', 'line2\n');
setTimeout(function() {
agent.close(function() {
callback(errors, received);
@@ -345,9 +347,8 @@ vows.describe('Integration :').addBatch({
check: function(errors, data) {
fs.unlinkSync('input1.txt');
- assert.deepEqual(data.sort(), ['toto.bouh.:1|c'].sort());
- assert.equal(errors.length, 1);
- assert.ok(errors[0].toString().match(/unknown_field/));
+ assert.deepEqual(data.sort(), ['toto.bouh.line2:1|c'].sort());
+ assert.equal(errors.length, 0);
}
},
}).addBatch({

0 comments on commit 544afd9

Please sign in to comment.