From f43ab84035cf39ff9471d5d48e3bb95af7fda788 Mon Sep 17 00:00:00 2001 From: Phillip Wong Date: Thu, 18 May 2017 06:07:18 -0700 Subject: [PATCH] Append the globalPrefix only after sourceRegex alterations to the measureName have been done, or if there is no sourceRegex configured. (#90) --- lib/librato.js | 10 ++++++---- test/librato_tests.js | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/librato.js b/lib/librato.js index c316fcb..98a5861 100644 --- a/lib/librato.js +++ b/lib/librato.js @@ -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; } diff --git a/test/librato_tests.js b/test/librato_tests.js index 5ceb49b..b31f9dc 100644 --- a/test/librato_tests.js +++ b/test/librato_tests.js @@ -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); + }, };