From bd45211760b966dc0812364550545c034fb71d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Dall=C3=B8kken?= Date: Sat, 22 Jun 2024 09:51:43 +0200 Subject: [PATCH] Improve error message on labels (#633) --- CHANGELOG.md | 2 ++ lib/util.js | 6 +++++- test/__snapshots__/counterTest.js.snap | 8 ++++---- test/__snapshots__/histogramTest.js.snap | 8 ++++---- test/__snapshots__/summaryTest.js.snap | 10 +++++----- test/utilTest.js | 20 ++++++++++++++++++++ 6 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 test/utilTest.js 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 diff --git a/lib/util.js b/lib/util.js index 8054c1bb..fef7529e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -53,7 +53,11 @@ exports.getLabels = function (labelNames, args) { } if (labelNames.length !== args.length) { - throw new Error('Invalid number of arguments'); + throw new Error( + `Invalid number of arguments (${args.length}): "${args.join( + ', ', + )}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`, + ); } const acc = {}; diff --git a/test/__snapshots__/counterTest.js.snap b/test/__snapshots__/counterTest.js.snap index 1b59e42f..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"`; +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"`; +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"`; +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"`; +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 075148fd..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"`; +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"`; +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"`; +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"`; +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 9dade939..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"`; +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"`; +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"`; +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"`; +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' ]"`; \ 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..bb02d68f --- /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 (1): "arg1" for label names (2): "label1, label2".', + ); + }); + }); +});