Skip to content

Commit

Permalink
Backend key name sanitization can be controlled with config flag
Browse files Browse the repository at this point in the history
This reverts commit 0eb2517.
  • Loading branch information
Patrick Koch committed Feb 6, 2015
1 parent 0c80706 commit 917a3e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions backends/graphite.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var prefixGauge;
var prefixSet;
var globalSuffix;
var prefixStats;
var globalKeySanitize = true;

// set up namespaces
var legacyNamespace = true;
Expand Down Expand Up @@ -99,9 +100,13 @@ var flush_stats = function graphite_flush(ts, metrics) {

// Sanitize key for graphite if not done globally
function sk(key) {
if (globalKeySanitize) {
return key;
} else {
return key.replace(/\s+/g, '_')
.replace(/\//g, '-')
.replace(/[^a-zA-Z_\-0-9\.]/g, '');
}
};

for (key in counters) {
Expand Down Expand Up @@ -248,6 +253,10 @@ exports.init = function graphite_init(startup_time, config, events, logger) {
graphiteStats.flush_time = 0;
graphiteStats.flush_length = 0;

if (config.keyNameSanitize !== undefined) {
globalKeySanitize = config.keyNameSanitize;
}

flushInterval = config.flushInterval;

flush_counts = typeof(config.flush_counts) === "undefined" ? true : config.flush_counts;
Expand Down
3 changes: 3 additions & 0 deletions exampleConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Optional Variables:
deleteCounters: don't send values to graphite for inactive counters, as opposed to sending 0 [default: false]
prefixStats: prefix to use for the statsd statistics data for this running instance of statsd [default: statsd]
applies to both legacy and new namespacing
keyNameSanitize: sanitize all stat names on ingress [default: true]
If disabled, it is up to the backends to sanitize keynames
as appropriate per their storage requirements.
console:
prettyprint: whether to prettyprint the console backend
Expand Down
16 changes: 15 additions & 1 deletion stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var backendEvents = new events.EventEmitter();
var healthStatus = config.healthStatus || 'up';
var old_timestamp = 0;
var timestamp_lag_namespace;
var keyNameSanitize = true;

// Load and init the backend from the backends/ directory.
function loadBackend(config, name) {
Expand Down Expand Up @@ -160,6 +161,16 @@ var stats = {
}
};

function sanitizeKeyName(key) {
if (keyNameSanitize) {
return key.replace(/\s+/g, '_')
.replace(/\//g, '-')
.replace(/[^a-zA-Z_\-0-9\.]/g, '');
} else {
return key;
}
}

// Global for the logger
var l;

Expand All @@ -184,6 +195,9 @@ config.configFile(process.argv[2], function (config) {
counters[packets_received] = 0;
counters[metrics_received] = 0;

if (config.keyNameSanitize !== undefined) {
keyNameSanitize = config.keyNameSanitize;
}
if (!serverLoaded) {
// key counting
var keyFlushInterval = Number((config.keyFlush && config.keyFlush.interval) || 0);
Expand All @@ -210,7 +224,7 @@ config.configFile(process.argv[2], function (config) {
l.log(metrics[midx].toString());
}
var bits = metrics[midx].toString().split(':');
var key = bits.shift();
var key = sanitizeKeyName(bits.shift());

if (keyFlushInterval > 0) {
if (! keyCounter[key]) {
Expand Down

0 comments on commit 917a3e4

Please sign in to comment.