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

Added retry logic & batching settings #1

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fa8a3ef
Update tests to pass with a default token
Sep 30, 2015
ae08451
Add utils.whilst + tests, rename utils.chain tests
Oct 7, 2015
1a55810
Add utils.expBackoff for exponential backoff timeout
Oct 9, 2015
76e3a43
Add retry on error logic, via exponential backoff.
Oct 9, 2015
e2d9342
Merge branch 'develop' into feature/retry
Oct 9, 2015
2042e05
Add retry example & update readme
Oct 9, 2015
3f4f3bd
Update examples
Oct 9, 2015
d598fb0
Add support for automatic interval batching.
Oct 20, 2015
3870937
Clean up batch interval implementation
Oct 28, 2015
fc98ed9
Add another config test for autoflush
Oct 30, 2015
566c3cb
Update doc datatype for batch interval
Oct 30, 2015
fa7f3b6
Add configuration setting for batch size
Nov 3, 2015
e5ded8d
Moved complicated logic from _sendEvents() to flush()
Nov 5, 2015
2f4bb81
Optimize calculateBatchSize, add additional test coverage
Nov 6, 2015
806b220
Add configuration setting for batch count
Nov 9, 2015
bdc30ac
Add comments to timer initialization logic
Nov 10, 2015
9949cdc
API changes based on feedback from #1
Nov 13, 2015
d50f1bd
Add units for _enableTimer()
Nov 13, 2015
ae5d79b
Disable maxBatchSize and maxBatchCount by default.
Nov 13, 2015
331143c
Rename utils.validatePositiveInt() to validateNonNegativeInt()
Nov 14, 2015
974187f
Renamed batching example to manual_batching
Nov 14, 2015
d50f926
Add all batching settings example
Nov 14, 2015
0644497
remove middleware and all references to it
Nov 14, 2015
bd45dfa
Update tests for 100% coverage
Nov 16, 2015
1a0c5bb
Added new example for custom event format
Nov 16, 2015
d9ce2d6
Correct the payload comment in the basic example
Nov 16, 2015
e2f9881
Correct comment in all batching example
Nov 16, 2015
8fa15ab
Refactor contextQueue to serializedContextQueue
Nov 17, 2015
4faf827
Remove the config.autoFlush property.
Nov 17, 2015
dab006b
Update examples following refactor
Nov 17, 2015
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See the `examples` folder for more examples:
* `basic.js`: shows how to configure a logger and send a log message to Splunk.
* `batching.js`: shows how to queue log messages, and send them in batches.
* `middleware.js`: shows how to add an express-like middleware function to be called before sending log messages to Splunk.
* `retry.js`: shows how to configure retries on errors.

### Basic example

Expand Down
95 changes: 95 additions & 0 deletions examples/all_batching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2015 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/**
* This example shows how to batch events with the
* SplunkLogger with all available settings:
* batchInterval, maxBatchCount, & maxBatchSize.
*/

// Change to require("splunk-logging").Logger;
var SplunkLogger = require("../index").Logger;

/**
* Only the token property is required.
*
* Here, batchInterval is set to flush every 1 second, when
* 10 events are queued, or when the size of queued events totals
* more than 1kb.
*/
var config = {
token: "your-token",
url: "https://localhost:8088",
batchInterval: 1000,
maxBatchCount: 10,
maxBatchSize: 1024 // 1kb
};

// Create a new logger
var Logger = new SplunkLogger(config);

Logger.error = function(err, context) {
// Handle errors here
console.log("error", err, "context", context);
};

// Define the payload to send to Splunk's Event Collector
var payload = {
// Message can be anything, doesn't have to be an object
message: {
temperature: "70F",
chickenCount: 500
},
// Metadata is optional
metadata: {
source: "chicken coop",
sourcetype: "httpevent",
index: "main",
host: "farm.local",
},
// Severity is also optional
severity: "info"
};

console.log("Queuing payload", payload);
// Don't need a callback here
Logger.send(payload);

var payload2 = {
message: {
temperature: "75F",
chickenCount: 600,
note: "New chickens have arrived"
},
metadata: payload.metadata
};

console.log("Queuing second payload", payload2);
// Don't need a callback here
Logger.send(payload2);

/**
* Since we've configured batching, we don't need
* to do anything at this point. Events will
* will be sent to Splunk automatically based
* on the batching settings above.
*/

// Kill the process
setTimeout(function() {
console.log("Events should be in Splunk! Exiting...");
process.exit();
}, 2000);
42 changes: 29 additions & 13 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@ var SplunkLogger = require("../index").Logger;

/**
* Only the token property is required.
* Defaults are listed explicitly.
*
* Alternatively, specify config.url like so:
*
* "https://localhost:8088/services/collector/event/1.0"
*/
var config = {
token: "your-token-here",
host: "localhost",
path: "/services/collector/event/1.0",
protocol: "https",
port: 8088,
level: "info",
autoFlush: true
url: "https://localhost:8088"
};

// Create a new logger
Expand All @@ -49,7 +39,7 @@ Logger.error = function(err, context) {

// Define the payload to send to Splunk's Event Collector
var payload = {
// Message can be anything, doesn't have to be an object
// Message can be anything, it doesn't have to be an object
message: {
temperature: "70F",
chickenCount: 500
Expand All @@ -59,13 +49,39 @@ var payload = {
source: "chicken coop",
sourcetype: "httpevent",
index: "main",
host: "farm.local",
host: "farm.local"
},
// Severity is also optional
severity: "info"
};

console.log("Sending payload", payload);

/**
* Since maxBatchCount is set to 1 by default,
* calling send will immediately send the payload.
*
* The underlying HTTP POST request is made to
*
* https://localhost:8088/services/collector/event/1.0
*
* with the following body
*
* {
* "source": "chicken coop",
* "sourcetype": "httpevent",
* "index": "main",
* "host": "farm.local",
* "event": {
* "message": {
* "temperature": "70F",
* "chickenCount": 500
* },
* "severity": "info"
* }
* }
*
*/
Logger.send(payload, function(err, resp, body) {
// If successful, body will be { text: 'Success', code: 0 }
console.log("Response from Splunk", body);
Expand Down
114 changes: 114 additions & 0 deletions examples/custom_format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2015 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

/**
* This example shows how to use a custom event format for SplunkLogger.
*/

// Change to require("splunk-logging").Logger;
var SplunkLogger = require("../index").Logger;

/**
* Only the token property is required.
*/
var config = {
token: "your-token-here",
url: "https://localhost:8088",
maxBatchCount: 1 // Send events 1 at a time
};

// Create a new logger
var Logger = new SplunkLogger(config);

Logger.error = function(err, context) {
// Handle errors here
console.log("error", err, "context", context);
};

/**
* Override the default eventFormatter() function,
* which takes a message and severity, returning
* any type - string or object are recommended.
*
* The message parameter can be any type. It will
* be whatever was passed to Logger.send().
* Severity will always be a string.
*
* In this example, we're building up a string
* of key=value pairs if message is an object,
* otherwise the message value is as value for
* the message key.
* This string is prefixed with the event
* severity in square brackets.
*/
Logger.eventFormatter = function(message, severity) {
var event = "[" + severity + "]";

if (typeof message === "object") {
for (var key in message) {
event += key + "=" + message[key] + " ";
}
}
else {
event += "message=" + message;
}

return event;
};

// Define the payload to send to Splunk's Event Collector
var payload = {
// Message can be anything, it doesn't have to be an object
message: {
temperature: "70F",
chickenCount: 500
},
// Metadata is optional
metadata: {
source: "chicken coop",
sourcetype: "httpevent",
index: "main",
host: "farm.local"
},
// Severity is also optional
severity: "info"
};

console.log("Sending payload", payload);

/**
* Since maxBatchCount is set to 1 by default,
* calling send will immediately send the payload.
*
* The underlying HTTP POST request is made to
*
* https://localhost:8088/services/collector/event/1.0
*
* with the following body
*
* {
* "source": "chicken coop",
* "sourcetype": "httpevent",
* "index": "main",
* "host": "farm.local",
* "event": "[info]temperature=70F chickenCount=500 "
* }
*
*/
Logger.send(payload, function(err, resp, body) {
// If successful, body will be { text: 'Success', code: 0 }
console.log("Response from Splunk", body);
});
20 changes: 6 additions & 14 deletions examples/batching.js → examples/manual_batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
* This example shows how to batch events with the
* SplunkLogger by manually calling flush.
*
* By default autoFlush is enabled, this means
* an HTTP request is made each time send()
* is called.
*
* By disabling autoFlush, events will be queued
* By setting maxbatchCount=0, events will be queued
* until flush() is called.
*/

Expand All @@ -32,16 +28,12 @@ var SplunkLogger = require("../index").Logger;
/**
* Only the token property is required.
*
* Here, autoFlush is set to false
* Here, maxBatchCount is set to 0.
*/
var config = {
token: "your-token-here",
host: "localhost",
path: "/services/collector/event/1.0",
protocol: "https",
port: 8088,
level: "info",
autoFlush: false
url: "https://localhost:8088",
maxBatchCount: 0 // Manually flush events
};

// Create a new logger
Expand All @@ -64,7 +56,7 @@ var payload = {
source: "chicken coop",
sourcetype: "httpevent",
index: "main",
host: "farm.local",
host: "farm.local"
},
// Severity is also optional
severity: "info"
Expand All @@ -88,7 +80,7 @@ console.log("Queuing second payload", payload2);
Logger.send(payload2);

/**
* Since autoFlush is disabled, call flush manually.
* Call flush manually.
* This will send both payloads in a single
* HTTP request.
*
Expand Down
Loading