Browse files

Move stat key name sanitization to Graphite backend.

  • Loading branch information...
1 parent a11359d commit 44106cc36ef870b36551fbd3bc0e4b5db9320728 @mheffner committed Sep 24, 2012
Showing with 15 additions and 9 deletions.
  1. +14 −5 backends/graphite.js
  2. +1 −4 stats.js
View
19 backends/graphite.js
@@ -93,14 +93,22 @@ var flush_stats = function graphite_flush(ts, metrics) {
var timer_data = metrics.timer_data;
var statsd_metrics = metrics.statsd_metrics;
+ // Sanitize Key for graphite
+ function sk(key) {
+ return key.replace(/\s+/g, '_')
+ .replace(/\//g, '-')
+ .replace(/[^a-zA-Z_\-0-9\.]/g, '');
+ };
+
for (key in counters) {
- var namespace = counterNamespace.concat(key);
var value = counters[key];
var valuePerSecond = counter_rates[key]; // pre-calculated "per second" rate
+ var keyName = sk(key);
+ var namespace = counterNamespace.concat(keyName);
if (legacyNamespace === true) {
statString += namespace.join(".") + ' ' + valuePerSecond + ts_suffix;
- statString += 'stats_counts.' + key + ' ' + value + ts_suffix;
+ statString += 'stats_counts.' + keyName + ' ' + value + ts_suffix;
} else {
statString += namespace.concat('rate').join(".") + ' ' + valuePerSecond + ts_suffix;
statString += namespace.concat('count').join(".") + ' ' + value + ts_suffix;
@@ -110,8 +118,9 @@ var flush_stats = function graphite_flush(ts, metrics) {
}
for (key in timer_data) {
- var namespace = timerNamespace.concat(key);
+ var namespace = timerNamespace.concat(sk(key));
var the_key = namespace.join(".");
+
for (timer_data_key in timer_data[key]) {
if (typeof(timer_data[key][timer_data_key]) === 'number') {
statString += the_key + '.' + timer_data_key + ' ' + timer_data[key][timer_data_key] + ts_suffix;
@@ -129,13 +138,13 @@ var flush_stats = function graphite_flush(ts, metrics) {
}
for (key in gauges) {
- var namespace = gaugesNamespace.concat(key);
+ var namespace = gaugesNamespace.concat(sk(key));
statString += namespace.join(".") + ' ' + gauges[key] + ts_suffix;
numStats += 1;
}
for (key in sets) {
- var namespace = setsNamespace.concat(key);
+ var namespace = setsNamespace.concat(sk(key));
statString += namespace.join(".") + '.count ' + sets[key].values().length + ts_suffix;
numStats += 1;
}
View
5 stats.js
@@ -167,10 +167,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
l.log(metrics[midx].toString());
}
var bits = metrics[midx].toString().split(':');
- var key = bits.shift()
- .replace(/\s+/g, '_')
- .replace(/\//g, '-')
- .replace(/[^a-zA-Z_\-0-9\.]/g, '');
+ var key = bits.shift();
if (keyFlushInterval > 0) {
if (! keyCounter[key]) {

0 comments on commit 44106cc

Please sign in to comment.