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

parse metric name and tags with histograms support #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 25 additions & 14 deletions lib/librato.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,34 @@ var flushStats = function libratoFlush(ts, metrics) {
var parseAndSetTags = function(measureName, measure) {
// Valid format for parsing tags out: global-prefix.name#tag1=value,tag2=value
// NOTE: Name can include the source
// Histogram bins: global-prefix.name#tag1=val1,tag2=val2.bin_50 => name: global-prefix.name.bin_50

var metricName = '';

var vals = measureName.split('#');
if (vals.length > 1) {
// Found tags in the measureName. Parse them out and return the measureName without the tags.
measureName = vals.shift();
rawTags = vals.pop().split(',');
rawTags.forEach(function(rawTag) {
var name = rawTag.split('=').shift();
var value = rawTag.split('=').pop();
if (name.length && value.length) {
measure.tags[name] = value;
}
});
return measureName;
} else {
// No tags existed in the measureName
if ( vals.length < 2 ) {
return measureName;
}

// Found tags in the measureName. Parse them out and return the measureName without the tags.
metricName = vals.shift();
var tags = vals[0];

var pos = tags.lastIndexOf('.bin_');
if (pos > -1) {
metricName += tags.substr(pos);
tags = tags.substr(0, pos);
}

rawTags = tags.split(',');
rawTags.forEach( function(rawTag) {
var name = rawTag.split('=').shift();
var value = rawTag.split('=').pop();
if (name.length && value.length) {
measure.tags[name] = value;
}
} );
return metricName;
};
for (key in metrics.counters) {
if (skipInternalMetrics && key.match(internalStatsdRe) != null) {
Expand Down
16 changes: 16 additions & 0 deletions test/librato_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ module.exports = {
this.emitter.emit('flush', 123, metrics);
},

testValidMeasurementSingleTagFromHistogram: function(test) {
test.expect(4);
let metrics = {gauges: {'my_gauge#foo=bar.bin_50': 1}};
Copy link
Contributor

@bryanmikaelian bryanmikaelian Apr 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good test but we probably want to build a proper timer metric to fully test this code path: https://github.com/librato/statsd-librato-backend/blob/master/lib/librato.js#L373.

Something like https://github.com/librato/statsd-librato-backend/blob/master/test/librato_tests.js#L85-L94 but with a histogram property

this.apiServer.post('/v1/measurements')
.reply(200, (uri, requestBody) => {
let measurement = requestBody.measurements[0];
test.ok(requestBody);
test.equal(measurement.name, 'my_gauge.bin_50');
test.equal(measurement.value, 1);
test.deepEqual(measurement.tags, {foo: 'bar'});
test.done();
});

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

testValidMeasurementMultipleTags: function(test) {
test.expect(4);
let metrics = {gauges: {'my_gauge#foo=bar,biz=baz': 1}};
Expand Down