Skip to content

Commit

Permalink
Merge pull request #11 from SebastianIoanPop/fix-statsd-illegal-chara…
Browse files Browse the repository at this point in the history
…cter

Fixed illegal character problem
  • Loading branch information
Sebastian Ioan Pop authored Jul 17, 2017
2 parents 9252dad + 2d01911 commit 0e618da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';
let defaultInterval = 10;
let oneSecInMs = 1000;
let keyCountAppendName = 'keyCounter';
const defaultInterval = 10;
const oneSecInMs = 1000;
const keyCountAppendName = 'keyCounter';

const path = require('path');
const util = require('util');
const infoParser = require('./lib/info-parser');
const redisClientFactory = require('./lib/redis-client-factory');
const libUtils = require('./lib/utils');
let keyCounter = require('./lib/redis-key-counter');
const keyCounter = require('./lib/redis-key-counter');
var configDir = path.resolve(process.argv[2] || './');

const StatsD = require('statsd-client');
Expand Down Expand Up @@ -44,13 +45,14 @@ redisClients.forEach((c) => {
keyCounter.count(c, (err, stat) => {
var prefix = libUtils.getPrefix(c, keyCountAppendName);
var suffix = libUtils.getSuffix(c, keyCountAppendName);
let cleanKey = libUtils.cleanKey(stat.key);

if (err) {
util.log(`[${c.host}] ${err}`);
return;
}

statsdClient.gauge(`${prefix}${stat.key}${suffix}`, stat.count);
statsdClient.gauge(`${prefix}${cleanKey}${suffix}`, stat.count);
});

}, (statsConfig.interval || defaultInterval) * oneSecInMs);
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
const statsdIllegalCharacter = ':';
const defaultWordSeparatorCharacter = '-';

module.exports.getSuffix = (c, nodeName) => {
var suffix = '';
Expand All @@ -25,3 +27,7 @@ module.exports.getPrefix = (c, nodeName) => {

return pre;
};

module.exports.cleanKey = (keyName) => {
return keyName.replace(statsdIllegalCharacter, defaultWordSeparatorCharacter);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redis-statsd-reporter",
"version": "1.5.1",
"version": "1.5.2",
"description": "Pull info from redis and publish to statsd",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ describe('utils', () => {
result.should.equal(',host=foo,node=bar');
});
});

describe('cleanKey', () => {
it('should replace occurances of ":" with "-" in the given key', () => {
var result = utils.cleanKey('test:key');
result.should.equal('test-key');
});
});
});

0 comments on commit 0e618da

Please sign in to comment.