Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Append the globalPrefix only after sourceRegex alterations to the mea… #90

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/librato.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,26 @@ var flushStats = function libratoFlush(ts, metrics) {
var addMeasure = function addMeasure(mType, measure, countStat) {
countStat = typeof countStat !== 'undefined' ? countStat : true;
var match;
var measureName = globalPrefix + measure.name;
var measureName = measure.name;
measure.tags = {};
measureName = parseAndSetTags(measureName, measure);
// Use first capturing group as source name.
// NOTE: Only legacy users will a) have a source and b) have a source set by regex
if (sourceRegex && (match = measureName.match(sourceRegex)) && match[1]) {
measure.source = sanitizeName(match[1]);
// Remove entire matching string from the measure name & add global prefix.
measure.name = sanitizeName(measureName.slice(0, match.index) + measureName.slice(match.index + match[0].length));
// Remove entire matching string from the measure name, add global prefix and sanitize the final measure name.
measure.name = sanitizeName(globalPrefix + measureName.slice(0, match.index) + measureName.slice(match.index + match[0].length));
// Create a measurement-level tag named source
measure.tags.source = measure.source;
} else {
measure.name = sanitizeName(measureName);
// add global prefix and sanitize the final measure name.
measure.name = sanitizeName(globalPrefix + measureName);
// Use the global config sourceName as a source tag, if it exists.
if (sourceName !== null) {
measure.tags.source = sourceName;
}
}

if (brokenMetrics[measure.name]) {
return;
}
Expand Down
27 changes: 27 additions & 0 deletions test/librato_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,31 @@ module.exports.legacy = {

this.emitter.emit('flush', 123, metrics);
},

testGlobalPrefix: function(test) {
config.librato.sourceRegex = /^(.*?)--/;
config.librato.globalPrefix = 'global.prefix';
librato.init(null, config, this.emitter);

test.expect(6);
let metrics = {gauges: {'rails-application--my_gauge': 1}};
this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name.startsWith('global.prefix'), true);
test.deepEqual(measurement.tags, {source: 'rails-application'});
});

this.apiServer.post('/v1/metrics')
.reply(200, (uri, requestBody) => {
let gauge = requestBody.gauges[0];
test.ok(requestBody);
test.equal(gauge.name.startsWith('global.prefix'), true);
test.equal(gauge.source, 'rails-application');
test.done();
});

this.emitter.emit('flush', 123, metrics);
},
};