Skip to content

Commit

Permalink
fix: avoid mutation bug in registry (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos authored and SimenB committed Jun 20, 2019
1 parent e291f4d commit 2c0020f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- fix: avoid mutation bug in registry

### Added

## [11.5.0] - 2019-06-04
Expand Down
6 changes: 6 additions & 0 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Registry {
let values = '';
for (const val of item.values || []) {
val.labels = val.labels || {};

if (defaultLabelNames.length > 0) {
// Make a copy before mutating
val.labels = Object.assign({}, val.labels);
}

for (const labelName of defaultLabelNames) {
val.labels[labelName] =
val.labels[labelName] || this._defaultLabels[labelName];
Expand Down
40 changes: 40 additions & 0 deletions test/registerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,46 @@ describe('register', () => {
});
});

describe('Registry with default labels', () => {
const Registry = require('../lib/registry');

it('should not throw with default labels', () => {
const r = new Registry();
r.setDefaultLabels({
env: 'development'
});

const hist = new Histogram({
name: 'my_histogram',
help: 'my histogram',
registers: [r],
labelNames: ['type']
});

const myHist = hist.labels('myType');

myHist.observe(1);

const metrics = r.metrics();
const lines = metrics.split('\n');
expect(
lines.indexOf(
'my_histogram_bucket{le="1",type="myType",env="development"} 1'
) >= 0
).toEqual(true);

myHist.observe(1);

const metrics2 = r.metrics();
const lines2 = metrics2.split('\n');
expect(
lines2.indexOf(
'my_histogram_bucket{le="1",type="myType",env="development"} 2'
) >= 0
).toEqual(true);
});
});

describe('merging', () => {
const Registry = require('../lib/registry');
let registryOne;
Expand Down

0 comments on commit 2c0020f

Please sign in to comment.