bunyan-loggly
Advanced tools
Comparing version 1.3.4 to 1.3.5
56
index.js
@@ -10,11 +10,16 @@ var loggly = require('node-loggly-bulk'); | ||
this.callback = callback || noop; | ||
this.bufferLength = bufferLength || 1; | ||
this.bufferTimeout = bufferTimeout || 30 * 1000; | ||
logglyConfig.json = true; | ||
logglyConfig.isBulk = true; | ||
logglyConfig.bufferOptions = { | ||
size: this.bufferLength, | ||
retriesInMilliSeconds: this.bufferTimeout, | ||
}; | ||
this.logglyClient = loggly.createClient(logglyConfig); | ||
this._buffer = []; | ||
this.bufferLength = bufferLength || 1; | ||
this.bufferTimeout = bufferTimeout; | ||
this.callback = callback || noop; | ||
} | ||
@@ -28,2 +33,3 @@ | ||
var data = originalData; | ||
var bunyan2Loggly = this; | ||
@@ -37,43 +43,7 @@ // loggly prefers timestamp over time | ||
this._buffer.push(data); | ||
this._checkBuffer(); | ||
}; | ||
Bunyan2Loggly.prototype._processBuffer = function () { | ||
var bunyan2Loggly = this; | ||
clearTimeout(bunyan2Loggly._timeoutId); | ||
var content = bunyan2Loggly._buffer.slice(); | ||
bunyan2Loggly._buffer = []; | ||
bunyan2Loggly.logglyClient.log(content, function (error, result) { | ||
bunyan2Loggly.callback(error, result, content); | ||
bunyan2Loggly.logglyClient.log(data, function (error, result) { | ||
bunyan2Loggly.callback(error, result, data); | ||
}); | ||
}; | ||
Bunyan2Loggly.prototype._checkBuffer = function () { | ||
var bunyan2Loggly = this; | ||
if (!this._buffer.length) { | ||
return; | ||
} | ||
if (this._buffer.length >= this.bufferLength) { | ||
return this._processBuffer(); | ||
} | ||
if (this.bufferTimeout) { | ||
clearTimeout(this._timeoutId); | ||
this._timeoutId = setTimeout( | ||
function () { | ||
bunyan2Loggly._processBuffer(); | ||
}, | ||
this.bufferTimeout | ||
); | ||
} | ||
}; | ||
module.exports = Bunyan2Loggly; |
{ | ||
"name": "bunyan-loggly", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"description": "A bunyan stream to transport logs to loggly", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -74,2 +74,11 @@ var test = require('tape'); | ||
test('Bunyan2Loggly sets default bufferTimeout', function (t) { | ||
t.plan(1); | ||
var Bunyan2Loggly = proxyquire('../', getBaseMocks()); | ||
var bunyan2Loggly = new Bunyan2Loggly(testConfig); | ||
t.equal(bunyan2Loggly.bufferTimeout, 30000, 'bufferTimeout defaulted correctly'); | ||
}); | ||
test('Bunyan2Loggly sets bufferTimeout if provided', function (t) { | ||
@@ -84,3 +93,3 @@ t.plan(1); | ||
test('Bunyan2Loggly throws if write alled with non raw stream', function (t) { | ||
test('Bunyan2Loggly throws if write called with non raw stream', function (t) { | ||
t.plan(2); | ||
@@ -96,34 +105,21 @@ | ||
test('Bunyan2Loggly adds data to buffer and calls check buffer', function (t) { | ||
t.plan(4); | ||
test('Bunyan2Loggly changes time to timestamp', function (t) { | ||
t.plan(1); | ||
var Bunyan2Loggly = proxyquire('../', getBaseMocks()); | ||
var bunyan2Loggly = new Bunyan2Loggly(testConfig); | ||
var testData = { foo: 'bar' }; | ||
var mocks = getBaseMocks(); | ||
var Bunyan2Loggly = proxyquire('../', mocks); | ||
var testData = { foo: 'bar', time: 'nao' }; | ||
var responseData = { foo: 'bar', timestamp: 'nao' }; | ||
bunyan2Loggly._checkBuffer = function () { | ||
t.pass('checkbuffer called'); | ||
mocks['node-loggly-bulk'].createClient = function () { | ||
return { | ||
log: function (data) { | ||
t.deepEqual(data, responseData, 'data sent to loggly'); | ||
}, | ||
}; | ||
}; | ||
t.equal(bunyan2Loggly._buffer.length, 0, 'started with empty buffer'); | ||
bunyan2Loggly.write(testData); | ||
t.equal(bunyan2Loggly._buffer.length, 1, 'something was added to buffer'); | ||
t.deepEqual(bunyan2Loggly._buffer[0], testData, 'data was added to buffer'); | ||
}); | ||
test('Bunyan2Loggly changes time to timestamp', function (t) { | ||
t.plan(1); | ||
var Bunyan2Loggly = proxyquire('../', getBaseMocks()); | ||
var bunyan2Loggly = new Bunyan2Loggly(testConfig); | ||
var testData = { foo: 'bar', time: 'nao' }; | ||
bunyan2Loggly._checkBuffer = function () {}; | ||
bunyan2Loggly.write(testData); | ||
t.deepEqual(bunyan2Loggly._buffer[0], { foo: 'bar', timestamp: 'nao' }, 'time changed to timestamp'); | ||
}); | ||
@@ -141,3 +137,3 @@ | ||
log: function (data) { | ||
t.deepEqual(data, [testData], 'data sent to loggly'); | ||
t.deepEqual(data, testData, 'data sent to loggly'); | ||
}, | ||
@@ -164,3 +160,3 @@ }; | ||
t.equal(result, testResult, 'correct result'); | ||
t.deepEqual(content, [testData], 'correct content'); | ||
t.deepEqual(content, testData, 'correct content'); | ||
} | ||
@@ -194,3 +190,3 @@ | ||
t.notEqual(data, testData, 'original data was not mutated'); | ||
t.deepEqual(data, [{ timestamp: 'nao' }], 'changed to timestamp'); | ||
t.deepEqual(data, { timestamp: 'nao' }, 'changed to timestamp'); | ||
}, | ||
@@ -204,55 +200,1 @@ }; | ||
}); | ||
test('Bunyan2Loggly sends data to loggly once buffer limit is reached', function (t) { | ||
t.plan(1); | ||
var mocks = getBaseMocks(); | ||
var Bunyan2Loggly = proxyquire('../', mocks); | ||
var testData = { foo: 'bar' }; | ||
var sent = 0; | ||
mocks['node-loggly-bulk'].createClient = function () { | ||
return { | ||
log: function (data) { | ||
if (!sent) { | ||
t.fail('should not have sent until buffer limit reached'); | ||
} | ||
t.deepEqual(data, [testData, testData], 'data sent to loggly'); | ||
}, | ||
}; | ||
}; | ||
var bunyan2Loggly = new Bunyan2Loggly(testConfig, 2); | ||
bunyan2Loggly.write(testData); | ||
sent++; | ||
bunyan2Loggly.write(testData); | ||
}); | ||
test('Bunyan2Loggly sends data to loggly after bufferTimeout even if not reached bufferLimit', function (t) { | ||
t.plan(1); | ||
var mocks = getBaseMocks(); | ||
var Bunyan2Loggly = proxyquire('../', mocks); | ||
var testData = { foo: 'bar' }; | ||
var waitedABit = false; | ||
mocks['node-loggly-bulk'].createClient = function () { | ||
return { | ||
log: function (data) { | ||
if (!waitedABit) { | ||
t.fail('should not have sent until buffer limit reached'); | ||
} | ||
t.deepEqual(data, [testData], 'data sent to loggly'); | ||
}, | ||
}; | ||
}; | ||
var bunyan2Loggly = new Bunyan2Loggly(testConfig, 2, 500); | ||
bunyan2Loggly.write(testData); | ||
setTimeout(function () { | ||
waitedABit = true; | ||
}, 200); | ||
}); |
13191
190