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

Make buffer option configurable and truncate events size over 1 MB

  • Loading branch information
Shwetajain148 committed Jan 6, 2017
commit 584da8503ed74db530ece021a469de081bc407e2
@@ -23,6 +23,17 @@ function stringify(msg) {
return payload;
}
//
// function to truncate message over 1 MB
//
function truncateLargeMessage(message) {
var MaximumBytesAllowedToLoggly = 1000 * 1000;

This comment has been minimized.

@mchaudhary

mchaudhary Jan 10, 2017

Ideally good not to hard code the MaximumBytesAllowedToLoggly. It will be good to have the size variable and can be passed as config. For now I am approving this. @Shwetajain148 please log JIRA to make the value define in config.

This comment has been minimized.

@Shwetajain148

Shwetajain148 Jan 11, 2017
Author

@mchaudhary, Actually I hard coded value of MaximumBytesAllowedToLoggly because we can send 1 MB per event i.e. maximum 1048576 bytes and in different encoding techniques like utf-8, utf-16 etc, individual character can take 1 to 6 bytes of memory space that increases the log message size each time. Due to this reason I took a safe side value less than actually accepted value so that if log message size increases due to different encoding techniques our final log message size would be less than 1 MB after truncation and will also reach to loggly successfully. I have also tested with different payload size and this logic was successfully truncating log message greater than 1 MB and logs were also reaching to loggly of less than 1 MB event size.
In that case we can not make MaximumBytesAllowedToLoggly configurable/dynamic. Anyways this logic is only applicable for truncating log message greater than 1 MB and not affecting any other code in library.
I have updated code to set value of MaximumBytesAllowedToLoggly using a constant variable.

This comment has been minimized.

@Shwetajain148

Shwetajain148 Jan 12, 2017
Author

@mchaudhary Please have a look at my comment above. Thanks

var bytesLengthOfLogMessage = Buffer.byteLength(message);
if(bytesLengthOfLogMessage > MaximumBytesAllowedToLoggly) {
message = message.slice(0, MaximumBytesAllowedToLoggly);
}
return message;
}
//
// function createClient (options)
// Creates a new instance of a Loggly client.
//
@@ -56,7 +67,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;
this.bufferOptions = options.bufferOptions || {size: 500, retriesInMilliSeconds: 30 * 1000};
//
// Set the tags on this instance.
//
@@ -91,6 +102,7 @@ util.inherits(Loggly, events.EventEmitter);
// - http://www.loggly.com/docs/api-sending-data/
//
Loggly.prototype.log = function (msg, tags, callback) {
msg.message = truncateLargeMessage(msg.message)
if (!callback && typeof tags === 'function') {
callback = tags;
tags = null;
@@ -120,7 +132,7 @@ Loggly.prototype.log = function (msg, tags, callback) {
body: msg,
proxy: this.proxy,
isBulk: this.isBulk,
numberOfLogtobebuffered: this.numberOfLogtobebuffered,
bufferOptions: this.bufferOptions,
headers: {
host: this.host,
accept: '*/*',
@@ -89,7 +89,7 @@ common.loggly = function () {
proxy,
isBulk,
uri,
numberOfLogtobebuffered;
bufferOptions;

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

var requestOptionsForBufferedLogs = JSON.parse(JSON.stringify(requestOptions))

if (auth) {
requestOptions.headers.authorization = 'Basic ' + new Buffer(auth.username + ':' + auth.password).toString('base64');
}
@@ -167,7 +169,6 @@ common.loggly = function () {
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode])));
}
sendBufferdLogstoLoggly();
success(res, body);
});
}
@@ -194,7 +195,6 @@ common.loggly = function () {
if (Object.keys(failCodes).indexOf(statusCode) !== -1) {
return onError((new Error('Loggly Error (' + statusCode + '): ' + failCodes[statusCode])));
}
sendBufferdLogstoLoggly();
success(res, body);
});
}
@@ -223,34 +223,35 @@ common.loggly = function () {
if (timerFunctionForBufferedLogs === null) {
timerFunctionForBufferedLogs = setInterval(function () {
if (arrBufferedMsg.length) sendBufferdLogstoLoggly();
}, 5 * 1000);
}, bufferOptions.retriesInMilliSeconds);
}


function sendBufferdLogstoLoggly() {
if (!arrBufferedMsg.length) return;
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);
}
});
}
arrayMessage = arrBufferedMsg.slice(0, logsInBunch);
requestOptionsForBufferedLogs.body = isBulk ? arrayMessage.join('\n') : arrayMessage[0];
request(requestOptionsForBufferedLogs, function (err, res, body) {
if(err) return;
var statusCode = res.statusCode.toString();
if(statusCode === "200") {
arrBufferedMsg.splice(0, logsInBunch);
sendBufferdLogstoLoggly();
}
});
requestOptionsForBufferedLogs.body = '';
}

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