Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to disable deflate #20

Merged
merged 5 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}