Skip to content

Commit

Permalink
Merge pull request #20 from kturney/deflate-option
Browse files Browse the repository at this point in the history
add option to disable deflate
  • Loading branch information
Ron Korving committed Dec 2, 2016
2 parents 742d9a6 + 8997572 commit 484f622
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
17 changes: 14 additions & 3 deletions graylog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var zlib = require('zlib'),
crypto = require('crypto'),
dgram = require('dgram'),
util = require('util'),
EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter,
assert = require('assert');

/**
* Graylog instances emit errors. That means you really really should listen for them,
Expand All @@ -18,6 +19,10 @@ var graylog = function graylog(config) {
this.client = null;
this.hostname = config.hostname || require('os').hostname();
this.facility = config.facility || 'Node.js';
this.deflate = config.deflate || 'optimal';
assert(
this.deflate === 'optimal' || this.deflate === 'always' || this.deflate === 'never',
'deflate must be one of "optimal", "always", or "never". was "' + this.deflate + '"');

this._unsentMessages = 0;
this._unsentChunks = 0;
Expand Down Expand Up @@ -164,7 +169,7 @@ graylog.prototype._log = function log(short_message, full_message, additionalFie
// Compression
payload = new Buffer(JSON.stringify(message));

zlib.deflate(payload, function (err, buffer) {
function sendPayload(err, buffer) {
if (err) {
that._unsentMessages -= 1;
return that.emitError(err);
Expand Down Expand Up @@ -235,7 +240,13 @@ graylog.prototype._log = function log(short_message, full_message, additionalFie

send();
});
});
}

if (this.deflate === 'never' || (this.deflate === 'optimal' && payload.length <= this._bufferSize)) {
sendPayload(null, payload);
} else {
zlib.deflate(payload, sendPayload);
}
};

graylog.prototype.send = function (chunk, server, cb) {
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var graylog = require('./graylog'),
fs = require('fs'),
assert = require('assert'),
file,
data,
servers = [
Expand Down Expand Up @@ -49,7 +50,36 @@ client.log('ParametersTest - Short message and full message', 'Full message');
client.log('ParametersTest - Short Message with full message and json', 'Full message', {cool: 'beans'});
console.log('');

console.log('---------------------------------------------');
console.log('Sending without deflate');
console.log('---------------------------------------------');
client.deflate = 'never';
for (var i = 4; i <= 64; i *= 2) {
file = './data/' + i + '.dat';
data = fs.readFileSync(file);
console.log('sending', file);
client.critical('Test 4 ' + file, data.toString(), {datafile: i + '.dat'});
}
client.deflate = 'optimal';
console.log('');

client.close(function () {
console.log('Insertion complete. Please check', 'http://' + servers[0].host + ':3000', 'and verify that insertion was successfull');
console.log('');
});

console.log('---------------------------------------------');
console.log('Checking deflate assertion');
console.log('---------------------------------------------');
try {
new graylog.graylog({
servers: servers,
facility: 'Test logger / Node.JS Test Script',
deflate: 'not an option'
});
throw new Error('should not get here')
} catch (err) {
assert(
err.message === 'deflate must be one of "optimal", "always", or "never". was "not an option"',
'assertion msg was wrong: ' + err.message);
}

0 comments on commit 484f622

Please sign in to comment.