Skip to content

Commit bd45211

Browse files
authored
Improve error message on labels (#633)
1 parent 564e467 commit bd45211

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
### Changed
1313

14+
- Improve error message when number of registered labels mismatch with the number of labels provided
15+
1416
### Added
1517

1618
[unreleased]: https://github.com/siimon/prom-client/compare/v15.1.2...HEAD

lib/util.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ exports.getLabels = function (labelNames, args) {
5353
}
5454

5555
if (labelNames.length !== args.length) {
56-
throw new Error('Invalid number of arguments');
56+
throw new Error(
57+
`Invalid number of arguments (${args.length}): "${args.join(
58+
', ',
59+
)}" for label names (${labelNames.length}): "${labelNames.join(', ')}".`,
60+
);
5761
}
5862

5963
const acc = {};

test/__snapshots__/counterTest.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`counter with OpenMetrics registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;
3+
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"."`;
44

5-
exports[`counter with OpenMetrics registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;
5+
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"."`;
66

77
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"`;
88

99
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"`;
1010

11-
exports[`counter with Prometheus registry remove should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;
11+
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"."`;
1212

13-
exports[`counter with Prometheus registry with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;
13+
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"."`;
1414

1515
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"`;
1616

test/__snapshots__/histogramTest.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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"`;
3+
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"."`;
44

5-
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"`;
5+
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"."`;
66

77
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"`;
88

99
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"`;
1010

11-
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"`;
11+
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"."`;
1212

13-
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"`;
13+
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"."`;
1414

1515
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"`;
1616

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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"`;
3+
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"."`;
44

5-
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"`;
5+
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"."`;
66

77
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' ]"`;
88

9-
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"`;
9+
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"."`;
1010

11-
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"`;
11+
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"."`;
1212

13-
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' ]"`;
13+
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' ]"`;

test/utilTest.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
describe('utils', () => {
4+
describe('getLabels', () => {
5+
const getLabels = require('../lib/util').getLabels;
6+
7+
it('should not throw on missing argument', async () => {
8+
const labels = getLabels(['label1', 'label2'], ['arg1', 'arg2']);
9+
expect(labels).toEqual({ label1: 'arg1', label2: 'arg2' });
10+
});
11+
12+
it('should throw on missing argument', async () => {
13+
expect(() => {
14+
getLabels(['label1', 'label2'], ['arg1']);
15+
}).toThrowError(
16+
'Invalid number of arguments (1): "arg1" for label names (2): "label1, label2".',
17+
);
18+
});
19+
});
20+
});

0 commit comments

Comments
 (0)