From 5ee2d47bd0f7ef8c0137cad1e6c6f488537bf62f Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Sat, 23 Jan 2021 10:37:44 -0800 Subject: [PATCH] fix: summary.observe should validate labels correctly Fixes #262 --- CHANGELOG.md | 1 + lib/summary.js | 2 +- test/__snapshots__/summaryTest.js.snap | 2 ++ test/summaryTest.js | 12 ++++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8feed15..3b46805d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/summary.js b/lib/summary.js index e5afd1b8..788203d9 100644 --- a/lib/summary.js +++ b/lib/summary.js @@ -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)}`, diff --git a/test/__snapshots__/summaryTest.js.snap b/test/__snapshots__/summaryTest.js.snap index 998369b1..f9f18ce0 100644 --- a/test/__snapshots__/summaryTest.js.snap +++ b/test/__snapshots__/summaryTest.js.snap @@ -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' ]"`; diff --git a/test/summaryTest.js b/test/summaryTest.js index bd119dfd..d9c11fb0 100644 --- a/test/summaryTest.js +++ b/test/summaryTest.js @@ -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);