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

LB-872 Improve Node with retry logic and queue #5

Merged
merged 3 commits into from Jan 17, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

queue support in case of error during sending logs to loggly

-make buffer size configurable
-handle 403 error(bad token)
-retries to send buffered logs to loggly in every 5 sec
-queue support for bulk and input mode
  • Loading branch information
Shwetajain148 committed Jan 4, 2017
commit fdba23a7eb370edaa314d7fd6bc84a87bc8aed18
@@ -56,6 +56,7 @@ var Loggly = exports.Loggly = function (options) {
this.userAgent = 'node-loggly ' + loggly.version;
this.useTagHeader = 'useTagHeader' in options ? options.useTagHeader : true;
this.isBulk = options.isBulk || false;
this.numberOfLogtobebuffered = options.numberOfLogtobebuffered || 1000;
//
// Set the tags on this instance.
//
@@ -119,6 +120,7 @@ Loggly.prototype.log = function (msg, tags, callback) {
body: msg,
proxy: this.proxy,
isBulk: this.isBulk,
numberOfLogtobebuffered: this.numberOfLogtobebuffered,
headers: {
host: this.host,
accept: '*/*',
@@ -13,6 +13,17 @@ var arrSize = 100,
arrMsg = [],
timerFunction = null;

//
// Variables for buffer array
//
var arrBufferedMsg = [],
timerFunctionForBufferedLogs = null;

//
// flag variable to validate authToken
//
var isValidToken = true;

var https = require('https'),
util = require('util'),
request = require('request'),
@@ -77,7 +88,8 @@ common.loggly = function () {
auth,
proxy,
isBulk,
uri;
uri,
numberOfLogtobebuffered;

//
// Now that we've popped off the two callbacks
@@ -99,6 +111,7 @@ common.loggly = function () {
isBulk = args[0].isBulk;
headers = args[0].headers;
proxy = args[0].proxy;
numberOfLogtobebuffered = args[0].numberOfLogtobebuffered;
}
}
else if (args.length === 2) {
@@ -113,6 +126,18 @@ common.loggly = function () {
}

function onError(err) {
if(!isValidToken){
console.log(err);
return;
}
var arrayLogs = [];
if(isBulk) {
arrayLogs = requestOptions.body.split('\n');
} else {
arrayLogs.push(requestOptions.body);
}
storeLogs(arrayLogs);

if (!responded) {
responded = true;
if (callback) { callback(err) }
@@ -138,9 +163,11 @@ common.loggly = function () {
return onError(err);
}
var statusCode = res.statusCode.toString();
if(statusCode === '403') isValidToken = false;
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode])));
}
}
sendBufferdLogstoLoggly();
success(res, body);
});
}
@@ -156,29 +183,26 @@ common.loggly = function () {
// Join Array Message with new line ('\n') character
//
requestOptions.body = arrMsg.join('\n');
arrMsg.length = 0;
try {
request(requestOptions, function (err, res, body) {
if (err) {
return onError(err);
}
var statusCode = res.statusCode.toString();
if(statusCode === '403') isValidToken = false;
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode])));
}
sendBufferdLogstoLoggly();
success(res, body);
});
}
catch (ex) {
onError(ex);
}
finally {
//
// Empty the array
//
arrMsg.length = 0;
}
}
if (isBulk === true) {
if (isBulk && isValidToken) {
if (timerFunction === null) {
timerFunction = setInterval(function () {
sendBulkLogs();
@@ -189,9 +213,47 @@ common.loggly = function () {
sendBulkLogs();
}
}
else {
else if(isValidToken) {
sendLogs();
}

//
// retries to send buffered logs to loggly in every 5 sec
//
if (timerFunctionForBufferedLogs === null) {
timerFunctionForBufferedLogs = setInterval(function () {
if (arrBufferedMsg.length) sendBufferdLogstoLoggly();
}, 5 * 1000);
}


function sendBufferdLogstoLoggly() {
var arrayMessage = [];
var bulkModeBunch = arrSize;
var inputModeBunch = 1;
var logsInBunch = isBulk ? bulkModeBunch : inputModeBunch;
for(var startIndex = 0; startIndex < arrBufferedMsg.length; startIndex = startIndex + logsInBunch) {
arrayMessage = arrBufferedMsg.slice(startIndex, startIndex + logsInBunch);
requestOptions.body = isBulk ? arrayMessage.join('\n') : arrayMessage[0];
request(requestOptions, function (err, res, body) {
if(err) return;
var statusCode = res.statusCode.toString();
if(statusCode === "200"){
arrBufferedMsg.splice(0, logsInBunch);
}
});
}
}

//
// This function will store logs into buffer
//
function storeLogs(logs) {
if (!logs.length) return;
var numberOfLogsToBeRemoved = (arrBufferedMsg.length + logs.length) - numberOfLogtobebuffered;
if (numberOfLogsToBeRemoved > 0) arrBufferedMsg = arrBufferedMsg.splice(numberOfLogsToBeRemoved);
arrBufferedMsg = arrBufferedMsg.concat(logs);
}
};
//
// ### function serialize (obj, key)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.