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

Add Graphite backend configuration flag to connect only when there are metrics to send #345

Open
wants to merge 3 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 31 additions & 5 deletions backends/graphite.js
Expand Up @@ -21,6 +21,8 @@ var net = require('net'),
var l;

var debug;
var quiet = false;
var deleteIdleStats = false;
var flushInterval;
var graphiteHost;
var graphitePort;
Expand Down Expand Up @@ -98,6 +100,19 @@ var flush_stats = function graphite_flush(ts, metrics) {
var timer_data = metrics.timer_data;
var statsd_metrics = metrics.statsd_metrics;

if (quiet
&& deleteIdleStats
&& (counters === null || isEmptyObject(counters))
&& (timer_data === null || isEmptyObject(timer_data))
&& (gauges === null || isEmptyObject(gauges))
&& (sets === null || isEmptyObject(sets))
) {
if (debug) {
l.log("Not sending stats, no metrics to report");
}
return;
}

for (key in counters) {
var namespace = counterNamespace.concat(key);
var value = counters[key];
Expand Down Expand Up @@ -190,14 +205,18 @@ exports.init = function graphite_init(startup_time, config, events) {
prefixSet = config.graphite.prefixSet;
globalSuffix = config.graphite.globalSuffix;
legacyNamespace = config.graphite.legacyNamespace;
quiet = config.graphite.quiet;
deleteIdleStats = config.deleteIdleStats;

// set defaults for prefixes & suffix
globalPrefix = globalPrefix !== undefined ? globalPrefix : "stats";
prefixCounter = prefixCounter !== undefined ? prefixCounter : "counters";
prefixTimer = prefixTimer !== undefined ? prefixTimer : "timers";
prefixGauge = prefixGauge !== undefined ? prefixGauge : "gauges";
prefixSet = prefixSet !== undefined ? prefixSet : "sets";
globalPrefix = globalPrefix !== undefined ? globalPrefix : "stats";
prefixCounter = prefixCounter !== undefined ? prefixCounter : "counters";
prefixTimer = prefixTimer !== undefined ? prefixTimer : "timers";
prefixGauge = prefixGauge !== undefined ? prefixGauge : "gauges";
prefixSet = prefixSet !== undefined ? prefixSet : "sets";
legacyNamespace = legacyNamespace !== undefined ? legacyNamespace : true;
quiet = quiet !== undefined ? quiet : false;
deleteIdleStats = deleteIdleStats !== undefined ? deleteIdleStats : false;

// In order to unconditionally add this string, it either needs to be
// a single space if it was unset, OR surrounded by a . and a space if
Expand Down Expand Up @@ -247,3 +266,10 @@ exports.init = function graphite_init(startup_time, config, events) {

return true;
};

function isEmptyObject(obj) {
for (var name in obj) {
return false;
}
return true;
}
2 changes: 2 additions & 0 deletions exampleConfig.js
Expand Up @@ -64,6 +64,8 @@ Optional Variables:

graphite:
legacyNamespace: use the legacy namespace [default: true]
quiet: connect to the Graphite server only when there are metrics to report
AND deleteIdleStats is set to 'true' [default: false]
globalPrefix: global prefix to use for sending stats to graphite [default: "stats"]
prefixCounter: graphite prefix for counter metrics [default: "counters"]
prefixTimer: graphite prefix for timer metrics [default: "timers"]
Expand Down
3 changes: 2 additions & 1 deletion test/graphite_tests.js
Expand Up @@ -80,7 +80,8 @@ module.exports = {
, port: 8125\n\
, dumpMessages: false \n\
, debug: false\n\
, graphite: { legacyNamespace: false }\n\
, deleteIdleStats: true\n\
, graphite: { legacyNamespace: false, quiet: true }\n\
, graphitePort: " + this.testport + "\n\
, graphiteHost: \"127.0.0.1\"}";

Expand Down