Skip to content

Commit cd0a0c6

Browse files
committed
Merge branch 'akrylysov-aggregate-by-tags'
2 parents 0b6c388 + da61fc3 commit cd0a0c6

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/aggregators.js

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

11+
Aggregator.prototype.makeBufferKey = function(key, tags) {
12+
tags = tags || [''];
13+
return key + '#' + tags.concat().sort().join('.');
14+
};
15+
1116
Aggregator.prototype.addPoint = function(Type, key, value, tags, host) {
12-
if (!this.buffer.hasOwnProperty(key)) {
13-
this.buffer[key] = new Type(key, tags, host);
17+
var bufferKey = this.makeBufferKey(key, tags);
18+
if (!this.buffer.hasOwnProperty(bufferKey)) {
19+
this.buffer[bufferKey] = new Type(key, tags, host);
1420
}
1521

16-
this.buffer[key].addPoint(value);
22+
this.buffer[bufferKey].addPoint(value);
1723
};
1824

1925
Aggregator.prototype.flush = function() {

test/aggregators_tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,34 @@ describe('Aggregator', function() {
4444
f.should.have.length(1);
4545
f[0].should.have.deep.property('points[0][1]', 5);
4646
});
47+
48+
it('should aggregate by key + tag', function() {
49+
var agg = new aggregators.Aggregator();
50+
agg.addPoint(metrics.Counter, 'test.mykey', 2, ['mytag1'], 'myhost');
51+
agg.addPoint(metrics.Counter, 'test.mykey', 3, ['mytag2'], 'myhost');
52+
var f = agg.flush();
53+
f.should.have.length(2);
54+
f[0].should.have.deep.property('points[0][1]', 2);
55+
f[1].should.have.deep.property('points[0][1]', 3);
56+
});
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+
});
4776
});
4877

0 commit comments

Comments
 (0)