a little cleanup and reduce the trim calls

This commit is contained in:
draco2003 2013-03-04 22:57:40 +00:00
Родитель 7d1ce9d963
Коммит eab7dd6e0d
1 изменённых файлов: 32 добавлений и 36 удалений

Просмотреть файл

@ -36,7 +36,7 @@ function loadBackend(config, name) {
l.log("Failed to load backend: " + name);
process.exit(1);
}
};
}
// global for conf
var conf;
@ -55,33 +55,33 @@ function flushMetrics() {
timer_data: timer_data,
pctThreshold: pctThreshold,
histogram: conf.histogram
}
};
// After all listeners, reset the stats
backendEvents.once('flush', function clear_metrics(ts, metrics) {
// Clear the counters
conf.deleteCounters = conf.deleteCounters || false;
for (key in metrics.counters) {
for (var counter_key in metrics.counters) {
if (conf.deleteCounters) {
if ((key.indexOf("packets_received") != -1) || (key.indexOf("bad_lines_seen") != -1)) {
metrics.counters[key] = 0;
if ((counter_key.indexOf("packets_received") != -1) || (counter_key.indexOf("bad_lines_seen") != -1)) {
metrics.counters[counter_key] = 0;
} else {
delete(metrics.counters[key]);
delete(metrics.counters[counter_key]);
}
} else {
metrics.counters[key] = 0;
metrics.counters[counter_key] = 0;
}
}
// Clear the timers
for (var key in metrics.timers) {
metrics.timers[key] = [];
metrics.timer_counters[key] = 0;
for (var timer_key in metrics.timers) {
metrics.timers[timer_key] = [];
metrics.timer_counters[timer_key] = 0;
}
// Clear the sets
for (var key in metrics.sets) {
metrics.sets[key] = new set.Set();
for (var set_key in metrics.sets) {
metrics.sets[set_key] = new set.Set();
}
});
@ -89,7 +89,7 @@ function flushMetrics() {
backendEvents.emit('flush', time_stamp, metrics);
});
};
}
var stats = {
messages: {
@ -160,7 +160,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
keyCounter[key] += 1;
}
if (bits.length == 0) {
if (bits.length === 0) {
bits.push("1");
}
@ -173,30 +173,31 @@ config.configFile(process.argv[2], function (config, oldConfig) {
} else {
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"; has invalid sample rate');
counters[bad_lines_seen]++;
stats['messages']['bad_lines_seen']++;
stats.messages.bad_lines_seen++;
continue;
}
}
if (fields[1] === undefined) {
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"');
counters[bad_lines_seen]++;
stats['messages']['bad_lines_seen']++;
stats.messages.bad_lines_seen++;
continue;
}
if (fields[1].trim() == "ms") {
var metric_type = fields[1].trim();
if (metric_type === "ms") {
if (! timers[key]) {
timers[key] = [];
timer_counters[key] = 0;
}
timers[key].push(Number(fields[0] || 0));
timer_counters[key] += (1 / sampleRate);
} else if (fields[1].trim() == "g") {
} else if (metric_type === "g") {
if (gauges[key] && fields[0].match(/^[-+]/)) {
gauges[key] += Number(fields[0] || 0);
} else {
gauges[key] = Number(fields[0] || 0);
}
} else if (fields[1].trim() == "s") {
} else if (metric_type === "s") {
if (! sets[key]) {
sets[key] = new set.Set();
}
@ -210,14 +211,14 @@ config.configFile(process.argv[2], function (config, oldConfig) {
}
}
stats['messages']['last_msg_seen'] = Math.round(new Date().getTime() / 1000);
stats.messages.last_msg_seen = Math.round(new Date().getTime() / 1000);
});
mgmtServer = net.createServer(function(stream) {
stream.setEncoding('ascii');
stream.on('error', function(err) {
l.log('Caught ' + err +', Moving on')
l.log('Caught ' + err +', Moving on');
});
stream.on('data', function(data) {
@ -287,25 +288,25 @@ config.configFile(process.argv[2], function (config, oldConfig) {
break;
case "delcounters":
for (var index in cmdline) {
delete counters[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
for (var counter_index in cmdline) {
delete counters[cmdline[counter_index]];
stream.write("deleted: " + cmdline[counter_index] + "\n");
}
stream.write("END\n\n");
break;
case "deltimers":
for (var index in cmdline) {
delete timers[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
for (var timer_index in cmdline) {
delete timers[cmdline[timer_index]];
stream.write("deleted: " + cmdline[timer_index] + "\n");
}
stream.write("END\n\n");
break;
case "delgauges":
for (var index in cmdline) {
delete gauges[cmdline[index]];
stream.write("deleted: " + cmdline[index] + "\n");
for (var gauge_index in cmdline) {
delete gauges[cmdline[gauge_index]];
stream.write("deleted: " + cmdline[gauge_index] + "\n");
}
stream.write("END\n\n");
break;
@ -352,7 +353,6 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var keyFlushLog = config.keyFlush && config.keyFlush.log;
keyFlushInt = setInterval(function () {
var key;
var sortedKeys = [];
for (var key in keyCounter) {
@ -381,9 +381,5 @@ config.configFile(process.argv[2], function (config, oldConfig) {
keyCounter = {};
}, keyFlushInterval);
}
;
}
})
});