Skip to content

Commit

Permalink
fix: summary.observe should validate labels correctly
Browse files Browse the repository at this point in the history
Fixes #262
  • Loading branch information
zbjornson committed Jan 23, 2021
1 parent 3a86d05 commit 5ee2d47
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ project adheres to [Semantic Versioning](http://semver.org/).

- fix: push client attempting to write Promise
- types: improve type checking of labels
- fix: Summary#observe should throw when adding additional labels to labelset (fixes [#262](https://github.com/siimon/prom-client/issues/262))

### Added

Expand Down
2 changes: 1 addition & 1 deletion lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function observe(labels) {
return value => {
const labelValuePair = convertLabelsAndValues(labels, value);

validateLabel(this.labelNames, this.labels);
validateLabel(this.labelNames, labels);
if (!Number.isFinite(labelValuePair.value)) {
throw new TypeError(
`Value is not a valid number: ${util.format(labelValuePair.value)}`,
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/summaryTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports[`summary global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;

exports[`summary global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;

exports[`summary global registry with param as object should validate labels when observing 1`] = `"Added label \\"baz\\" is not included in initial labelset: [ 'foo' ]"`;
12 changes: 12 additions & 0 deletions test/summaryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe('summary', () => {
expect((await instance.get()).values[8].value).toEqual(1);
});

it('should validate labels when observing', async () => {
const summary = new Summary({
name: 'foobar',
help: 'Foo Bar',
labelNames: ['foo'],
});

expect(() => {
summary.observe({ foo: 'bar', baz: 'qaz' }, 10);
}).toThrowErrorMatchingSnapshot();
});

it('should correctly calculate percentiles when more values are added to the summary', async () => {
instance.observe(100);
instance.observe(100);
Expand Down

0 comments on commit 5ee2d47

Please sign in to comment.