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

Call callbacks in batch logging scenario once the request is sent or failed. #33

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions splunklogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ function _defaultEventFormatter(message, severity) {
* @property {object} config - Configuration settings for this <code>SplunkLogger</code> instance.
* @param {object} requestOptions - Options to pass to <code>{@link https://github.com/request/request#requestpost|request.post()}</code>.
* See the {@link http://github.com/request/request|request documentation} for all available options.
* @property {object[]} serializedContextQueue - Queue of serialized <code>context</code> objects to be sent to Splunk Enterprise or Splunk Cloud.
* @property {Object[]} serializedContextQueue - Queue of events and their callbacks to be sent to Splunk Enterprise or Splunk Cloud
* @property {Object} serializedContextQueue.event - Event definition
* @property {function} serializedContextQueue.callback - Callback to be invoked once event either is successfully sent, or failed to be sent
* @property {function} eventFormatter - Formats events, returning an event as a string, <code>function(message, severity)</code>.
* Can be overwritten, the default event formatter will display event and severity as properties in a JSON object.
* @property {function} error - A callback function for errors: <code>function(err, context)</code>.
Expand Down Expand Up @@ -416,16 +418,26 @@ SplunkLogger.prototype._post = function(requestOptions, callback) {
/**
* Sends events to Splunk Enterprise or Splunk Cloud, optionally with retries on non-Splunk errors.
*
* @param context
* @property {Object[]} eventList - List of events to send to Splunk Enterprise or Splunk Cloud
* @property {Object} eventList.event - Event definition to be sent
* @property {function} eventList.callback - Callback to be invoked once event either is successfully sent, or failed to be sent
* @param {function} callback - A callback function: <code>function(err, response, body)</code>
* @private
*/
SplunkLogger.prototype._sendEvents = function(context, callback) {
SplunkLogger.prototype._sendEvents = function(eventList, callback) {
callback = callback || /* istanbul ignore next*/ function(){};

// Initialize the config once more to avoid undefined vals below
this.config = this._initializeConfig(this.config);

var data = eventList.map(function(queueItem) {
return queueItem.event;
}).join("");

var context = {
message: data
};

// Makes a copy of the request options so we can set the body
var requestOptions = this._initializeRequestOptions(this.requestOptions);
requestOptions.body = this._validateMessage(context.message);
Expand Down Expand Up @@ -495,6 +507,13 @@ SplunkLogger.prototype._sendEvents = function(context, callback) {
that.error(requestError || splunkError, context);
}

// call callback for each event that was sent in the request
for (var i = 0; i < eventList.length; i++) {
if (eventList[i].callback) {
eventList[i].callback(requestError, _response, _body);
}
}

callback(requestError, _response, _body);
}
);
Expand Down Expand Up @@ -555,15 +574,15 @@ SplunkLogger.prototype.send = function(context, callback) {

// Store the context, and its estimated length
var currentEvent = JSON.stringify(this._makeBody(context));
this.serializedContextQueue.push(currentEvent);
this.serializedContextQueue.push({ event: currentEvent, callback: callback });
this.eventsBatchSize += Buffer.byteLength(currentEvent, "utf8");

var batchOverSize = this.eventsBatchSize > this.config.maxBatchSize && this.config.maxBatchSize > 0;
var batchOverCount = this.serializedContextQueue.length >= this.config.maxBatchCount && this.config.maxBatchCount > 0;

// Only flush if the queue's byte size is too large, or has too many events
if (batchOverSize || batchOverCount) {
this.flush(callback || function(){});
this.flush();
}
};

Expand All @@ -580,14 +599,8 @@ SplunkLogger.prototype.flush = function(callback) {
var queue = this.serializedContextQueue;
this.serializedContextQueue = [];
this.eventsBatchSize = 0;

// Send all queued events
var data = queue.join("");
var context = {
message: data
};

this._sendEvents(context, callback);
this._sendEvents(queue, callback);
};

module.exports = SplunkLogger;
4 changes: 2 additions & 2 deletions test/test_send.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,9 @@ describe("SplunkLogger send (integration tests)", function() {

// Wrap sendevents to ensure it gets called
var sendEvents = logger._sendEvents;
logger._sendEvents = function(cont, cb) {
logger._sendEvents = function(queue, cb) {
sent++;
sendEvents(cont, cb);
sendEvents(queue, cb);
};

logger.send(context);
Expand Down