From efa32674993dfeecdc55a042e96bdbc934b963cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?espen=20dall=C3=B8kken?= Date: Fri, 21 Jun 2024 10:43:36 +0200 Subject: [PATCH 1/6] fix: improving error message in getLabels --- lib/util.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 8054c1bb..36cf2976 100644 --- a/lib/util.js +++ b/lib/util.js @@ -53,7 +53,9 @@ exports.getLabels = function (labelNames, args) { } if (labelNames.length !== args.length) { - throw new Error('Invalid number of arguments'); + throw new Error( + `Invalid number of arguments. Got ${args.join()}. Expected ${labelNames.join()}.`, + ); } const acc = {}; From 0417f9138472974bf47956879b01ac7420ade389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?espen=20dall=C3=B8kken?= Date: Fri, 21 Jun 2024 11:31:19 +0200 Subject: [PATCH 2/6] fix: improve error message in utils getLabels --- lib/util.js | 2 +- test/__snapshots__/counterTest.js.snap | 8 ++++---- test/__snapshots__/histogramTest.js.snap | 8 ++++---- test/__snapshots__/summaryTest.js.snap | 10 +++++----- test/utilTest.js | 20 ++++++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 test/utilTest.js diff --git a/lib/util.js b/lib/util.js index 36cf2976..077a769f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -54,7 +54,7 @@ exports.getLabels = function (labelNames, args) { if (labelNames.length !== args.length) { throw new Error( - `Invalid number of arguments. Got ${args.join()}. Expected ${labelNames.join()}.`, + `Invalid number of arguments: "${args.join()}" for label names: "${labelNames.join()}".`, ); } diff --git a/test/__snapshots__/counterTest.js.snap b/test/__snapshots__/counterTest.js.snap index 1b59e42f..58e6e64d 100644 --- a/test/__snapshots__/counterTest.js.snap +++ b/test/__snapshots__/counterTest.js.snap @@ -1,16 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`counter with OpenMetrics registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`counter with OpenMetrics registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; -exports[`counter with OpenMetrics registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`counter with OpenMetrics registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; exports[`counter with OpenMetrics registry with params as object should not be possible to decrease a counter 1`] = `"It is not possible to decrease a counter"`; exports[`counter with OpenMetrics registry with params as object should throw an error when the value is not a number 1`] = `"Value is not a valid number: 3ms"`; -exports[`counter with Prometheus registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`counter with Prometheus registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; -exports[`counter with Prometheus registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`counter with Prometheus registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; exports[`counter with Prometheus registry with params as object should not be possible to decrease a counter 1`] = `"It is not possible to decrease a counter"`; diff --git a/test/__snapshots__/histogramTest.js.snap b/test/__snapshots__/histogramTest.js.snap index 075148fd..98b9e972 100644 --- a/test/__snapshots__/histogramTest.js.snap +++ b/test/__snapshots__/histogramTest.js.snap @@ -1,16 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`histogram with OpenMetrics registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments"`; +exports[`histogram with OpenMetrics registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments: "get,500" for label names: "method"."`; -exports[`histogram with OpenMetrics registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`histogram with OpenMetrics registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET,/foo" for label names: "method"."`; exports[`histogram with OpenMetrics registry with object as params with global registry should not allow le as a custom label 1`] = `"le is a reserved label keyword"`; exports[`histogram with OpenMetrics registry with object as params with global registry should not allow non numbers 1`] = `"Value is not a valid number: asd"`; -exports[`histogram with Prometheus registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments"`; +exports[`histogram with Prometheus registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments: "get,500" for label names: "method"."`; -exports[`histogram with Prometheus registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`histogram with Prometheus registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET,/foo" for label names: "method"."`; exports[`histogram with Prometheus registry with object as params with global registry should not allow le as a custom label 1`] = `"le is a reserved label keyword"`; diff --git a/test/__snapshots__/summaryTest.js.snap b/test/__snapshots__/summaryTest.js.snap index 9dade939..2793063e 100644 --- a/test/__snapshots__/summaryTest.js.snap +++ b/test/__snapshots__/summaryTest.js.snap @@ -1,13 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`summary with OpenMetrics registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`summary with OpenMetrics registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; -exports[`summary with OpenMetrics registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`summary with OpenMetrics registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; exports[`summary with OpenMetrics registry global registry with param as object should validate labels when observing 1`] = `"Added label "baz" is not included in initial labelset: [ 'foo' ]"`; -exports[`summary with Prometheus registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`summary with Prometheus registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; -exports[`summary with Prometheus registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`; +exports[`summary with Prometheus registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; -exports[`summary with Prometheus registry global registry with param as object should validate labels when observing 1`] = `"Added label "baz" is not included in initial labelset: [ 'foo' ]"`; \ No newline at end of file +exports[`summary with Prometheus registry 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/utilTest.js b/test/utilTest.js new file mode 100644 index 00000000..9a60f139 --- /dev/null +++ b/test/utilTest.js @@ -0,0 +1,20 @@ +'use strict'; + +describe('utils', () => { + describe('getLabels', () => { + const getLabels = require('../lib/util').getLabels; + + it('should not throw on missing argument', async () => { + const labels = getLabels(['label1', 'label2'], ['arg1', 'arg2']); + expect(labels).toEqual({ label1: 'arg1', label2: 'arg2' }); + }); + + it('should throw on missing argument', async () => { + expect(() => { + getLabels(['label1', 'label2'], ['arg1']); + }).toThrowError( + 'Invalid number of arguments: "arg1" for label names: "label1,label2".', + ); + }); + }); +}); From 5a95912f2ff6ee88908f5b44b04cacd0bfa027ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Dall=C3=B8kken?= Date: Sat, 22 Jun 2024 09:29:08 +0200 Subject: [PATCH 3/6] Improved error message Co-authored-by: Simen Bekkhus --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 077a769f..b760319c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -54,7 +54,7 @@ exports.getLabels = function (labelNames, args) { if (labelNames.length !== args.length) { throw new Error( - `Invalid number of arguments: "${args.join()}" for label names: "${labelNames.join()}".`, + `Invalid number of arguments (${args.length}): "${args.join(', ')}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`, ); } From 021b919952bc1374ccd8fb217ab7a57feb659713 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 22 Jun 2024 09:44:53 +0200 Subject: [PATCH 4/6] update tests --- test/__snapshots__/counterTest.js.snap | 8 ++++---- test/__snapshots__/histogramTest.js.snap | 8 ++++---- test/__snapshots__/summaryTest.js.snap | 8 ++++---- test/utilTest.js | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/test/__snapshots__/counterTest.js.snap b/test/__snapshots__/counterTest.js.snap index 58e6e64d..dad9d0d3 100644 --- a/test/__snapshots__/counterTest.js.snap +++ b/test/__snapshots__/counterTest.js.snap @@ -1,16 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`counter with OpenMetrics registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`counter with OpenMetrics registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; -exports[`counter with OpenMetrics registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`counter with OpenMetrics registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; exports[`counter with OpenMetrics registry with params as object should not be possible to decrease a counter 1`] = `"It is not possible to decrease a counter"`; exports[`counter with OpenMetrics registry with params as object should throw an error when the value is not a number 1`] = `"Value is not a valid number: 3ms"`; -exports[`counter with Prometheus registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`counter with Prometheus registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; -exports[`counter with Prometheus registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`counter with Prometheus registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; exports[`counter with Prometheus registry with params as object should not be possible to decrease a counter 1`] = `"It is not possible to decrease a counter"`; diff --git a/test/__snapshots__/histogramTest.js.snap b/test/__snapshots__/histogramTest.js.snap index 98b9e972..bd5d6184 100644 --- a/test/__snapshots__/histogramTest.js.snap +++ b/test/__snapshots__/histogramTest.js.snap @@ -1,16 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`histogram with OpenMetrics registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments: "get,500" for label names: "method"."`; +exports[`histogram with OpenMetrics registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments (2): "get, 500" for label names (1): "method"."`; -exports[`histogram with OpenMetrics registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET,/foo" for label names: "method"."`; +exports[`histogram with OpenMetrics registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (2): "GET, /foo" for label names (1): "method"."`; exports[`histogram with OpenMetrics registry with object as params with global registry should not allow le as a custom label 1`] = `"le is a reserved label keyword"`; exports[`histogram with OpenMetrics registry with object as params with global registry should not allow non numbers 1`] = `"Value is not a valid number: asd"`; -exports[`histogram with Prometheus registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments: "get,500" for label names: "method"."`; +exports[`histogram with Prometheus registry with object as params with global registry labels should not allow different number of labels 1`] = `"Invalid number of arguments (2): "get, 500" for label names (1): "method"."`; -exports[`histogram with Prometheus registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET,/foo" for label names: "method"."`; +exports[`histogram with Prometheus registry with object as params with global registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (2): "GET, /foo" for label names (1): "method"."`; exports[`histogram with Prometheus registry with object as params with global registry should not allow le as a custom label 1`] = `"le is a reserved label keyword"`; diff --git a/test/__snapshots__/summaryTest.js.snap b/test/__snapshots__/summaryTest.js.snap index 2793063e..d2fba35d 100644 --- a/test/__snapshots__/summaryTest.js.snap +++ b/test/__snapshots__/summaryTest.js.snap @@ -1,13 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`summary with OpenMetrics registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`summary with OpenMetrics registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; -exports[`summary with OpenMetrics registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`summary with OpenMetrics registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; exports[`summary with OpenMetrics registry global registry with param as object should validate labels when observing 1`] = `"Added label "baz" is not included in initial labelset: [ 'foo' ]"`; -exports[`summary with Prometheus registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`summary with Prometheus registry global registry with param as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; -exports[`summary with Prometheus registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments: "GET" for label names: "method,endpoint"."`; +exports[`summary with Prometheus registry global registry with param as object remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments (1): "GET" for label names (2): "method, endpoint"."`; exports[`summary with Prometheus registry 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/utilTest.js b/test/utilTest.js index 9a60f139..bb02d68f 100644 --- a/test/utilTest.js +++ b/test/utilTest.js @@ -13,7 +13,7 @@ describe('utils', () => { expect(() => { getLabels(['label1', 'label2'], ['arg1']); }).toThrowError( - 'Invalid number of arguments: "arg1" for label names: "label1,label2".', + 'Invalid number of arguments (1): "arg1" for label names (2): "label1, label2".', ); }); }); From 89298ed107681dc2af3c49e267478099f8eea5f8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 22 Jun 2024 09:45:36 +0200 Subject: [PATCH 5/6] changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96fcb5cb..0da423e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Improve error message when number of registered labels mismatch with the number of labels provided + ### Added [unreleased]: https://github.com/siimon/prom-client/compare/v15.1.2...HEAD From ab415e5d8d60784d2d82ba453106e697cf39df49 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 22 Jun 2024 09:46:17 +0200 Subject: [PATCH 6/6] prettier --- lib/util.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index b760319c..fef7529e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -54,7 +54,9 @@ exports.getLabels = function (labelNames, args) { if (labelNames.length !== args.length) { throw new Error( - `Invalid number of arguments (${args.length}): "${args.join(', ')}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`, + `Invalid number of arguments (${args.length}): "${args.join( + ', ', + )}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`, ); }