Skip to content

Commit

Permalink
create accurate timer for metric flush
Browse files Browse the repository at this point in the history
clamp timestamp to a precise interval statsd#459
  • Loading branch information
Thomas Mühlschlegel authored and Patrick Koch committed Apr 11, 2016
1 parent ff845eb commit 54e6478
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ var conf;

// Flush metrics to each backend.
function flushMetrics() {
setTimeout(flushMetrics, getFlushTimeout(flushInterval));

var time_stamp = Math.round(new Date().getTime() / 1000);
if (old_timestamp > 0) {
gauges[timestamp_lag_namespace] = (time_stamp - old_timestamp - (Number(conf.flushInterval)/1000));
Expand Down Expand Up @@ -139,7 +141,7 @@ function flushMetrics() {
}
}

// normally gauges are not reset. so if we don't delete them, continue to persist previous value
// normally gauges are not reset. so if we don't delete them, continue to persist previous value
conf.deleteGauges = conf.deleteGauges || false;
if (conf.deleteGauges) {
for (var gauge_key in metrics.gauges) {
Expand Down Expand Up @@ -171,6 +173,10 @@ function sanitizeKeyName(key) {
}
}

function getFlushTimeout(interval) {
return interval - (new Date().getTime() - startup_time * 1000) % flushInterval
}

// Global for the logger
var l;

Expand Down Expand Up @@ -417,7 +423,7 @@ config.configFile(process.argv[2], function (config) {
}

// Setup the flush timer
var flushInt = setInterval(flushMetrics, flushInterval);
var flushInt = setTimeout(flushMetrics, getFlushTimeout(flushInterval));

if (keyFlushInterval > 0) {
var keyFlushPercent = Number((config.keyFlush && config.keyFlush.percent) || 100);
Expand Down

0 comments on commit 54e6478

Please sign in to comment.