Skip to content

Commit

Permalink
Slight cleanup of the backend API by converting the flush interface to
Browse files Browse the repository at this point in the history
a top-level module export.
  • Loading branch information
mheffner committed Apr 26, 2012
1 parent 168a8af commit 4fa436a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
14 changes: 8 additions & 6 deletions backends/graphite.js
Expand Up @@ -16,6 +16,7 @@ var net = require('net'),
util = require('util');

var debug;
var flushInterval;
var graphiteHost;
var graphitePort;

Expand Down Expand Up @@ -44,7 +45,7 @@ var post_stats = function(statString) {
}
}

var flush_stats = function(ts, flushInterval, metrics) {
var flush_stats = function(ts, metrics) {
var statString = '';
var numStats = 0;
var key;
Expand Down Expand Up @@ -119,7 +120,7 @@ var flush_stats = function(ts, flushInterval, metrics) {
post_stats(statString);
};

var write_stats = function(writeCb) {
var backend_stats = function(writeCb) {
for (stat in graphiteStats) {
writeCb(null, stat, graphiteStats[stat]);
}
Expand All @@ -133,10 +134,11 @@ var init_backend = function(startup_time, config) {
graphiteStats.last_flush = startup_time;
graphiteStats.last_exception = startup_time;

return function(ts, flushInterval, metrics) {
flush_stats(ts, flushInterval, metrics);
};
flushInterval = config.flushInterval;

return true;
};

exports.init = init_backend;
exports.write_stats = write_stats;
exports.flush = flush_stats;
exports.stats = backend_stats;
13 changes: 7 additions & 6 deletions stats.js
Expand Up @@ -25,13 +25,12 @@ var loadBackend = function(config, name) {
util.log("Loading backend: " + name);
}

var flushCb = backendmod.init(startup_time, config);
if (!flushCb) {
var ret = backendmod.init(startup_time, config);
if (!ret) {
util.log("Failed to load backend: " + name);
process.exit(1);
}

backend.flushCb = flushCb;
backends.push(backend);
};

Expand All @@ -47,7 +46,7 @@ var flushMetrics = function() {
}

for (var i = 0; i < backends.length; i++) {
backends[i].flushCb(ts, flushInterval, metrics);
backends[i].mod.flush(ts, metrics);
}

// Clear the counters
Expand Down Expand Up @@ -176,7 +175,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {

// Retrieve stats from each backend
for (var i = 0; i < backends.length; i++) {
backends[i].mod.write_stats(function(err, stat, val) {
backends[i].mod.stats(function(err, stat, val) {
if (err) {
util.log("Failed to read stats for backend " +
backends[i].name + ": " + err);
Expand Down Expand Up @@ -250,6 +249,9 @@ config.configFile(process.argv[2], function (config, oldConfig) {
pctThreshold = [ pctThreshold ]; // listify percentiles so single values work the same
}

flushInterval = Number(config.flushInterval || 10000);
config.flushInterval = flushInterval;

if (config.backends) {
for (var i = 0; i < config.backends.length; i++) {
loadBackend(config, config.backends[i]);
Expand All @@ -260,7 +262,6 @@ config.configFile(process.argv[2], function (config, oldConfig) {
}

// Setup the flush timer
flushInterval = Number(config.flushInterval || 10000);
var flushInt = setInterval(flushMetrics, flushInterval);

if (keyFlushInterval > 0) {
Expand Down

0 comments on commit 4fa436a

Please sign in to comment.