Skip to content

Commit da61fc3

Browse files
committed
Extract buffer key generation logic and add some more tests
1 parent c215cf5 commit da61fc3

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/aggregators.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ function Aggregator(opts) {
88
this.buffer = {};
99
}
1010

11-
Aggregator.prototype.addPoint = function(Type, key, value, tags, host) {
12-
var bufferKey = key;
13-
if (tags) {
14-
bufferKey += tags.concat().sort().join();
15-
}
11+
Aggregator.prototype.makeBufferKey = function(key, tags) {
12+
tags = tags || [''];
13+
return key + '#' + tags.concat().sort().join('.');
14+
};
1615

16+
Aggregator.prototype.addPoint = function(Type, key, value, tags, host) {
17+
var bufferKey = this.makeBufferKey(key, tags);
1718
if (!this.buffer.hasOwnProperty(bufferKey)) {
1819
this.buffer[bufferKey] = new Type(key, tags, host);
1920
}

test/aggregators_tests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,24 @@ describe('Aggregator', function() {
5454
f[0].should.have.deep.property('points[0][1]', 2);
5555
f[1].should.have.deep.property('points[0][1]', 3);
5656
});
57+
58+
it('should treat all empty tags definitions the same', function() {
59+
var agg = new aggregators.Aggregator();
60+
agg.addPoint(metrics.Gauge, 'noTagsKey', 1, null, 'myhost');
61+
agg.addPoint(metrics.Gauge, 'noTagsKey', 2, undefined, 'myhost');
62+
agg.addPoint(metrics.Gauge, 'noTagsKey', 3, [], 'myhost');
63+
var f = agg.flush();
64+
f.should.have.length(1);
65+
f[0].should.have.deep.property('points[0][1]', 3);
66+
});
67+
68+
it('should normalize the tag order', function() {
69+
var agg = new aggregators.Aggregator();
70+
agg.addPoint(metrics.Gauge, 'mykey', 1, ['t1', 't2', 't3'], 'myhost');
71+
agg.addPoint(metrics.Gauge, 'mykey', 2, ['t3', 't2', 't1'], 'myhost');
72+
var f = agg.flush();
73+
f.should.have.length(1);
74+
f[0].should.have.deep.property('points[0][1]', 2);
75+
});
5776
});
5877

0 commit comments

Comments
 (0)