Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message on labels #633

Merged
merged 6 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
}

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(', ')}".`,

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v16.x and OS ubuntu-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v16.x and OS windows-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v16.x and OS macOS-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v18.x and OS ubuntu-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v18.x and OS windows-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v18.x and OS macOS-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v20.x and OS ubuntu-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v20.x and OS windows-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v20.x and OS macOS-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v21.x and OS ubuntu-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v21.x and OS windows-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`

Check failure on line 57 in lib/util.js

View workflow job for this annotation

GitHub Actions / Test on Node.js v21.x and OS macOS-latest

Replace `',·'` with `⏎↹↹↹↹',·',⏎↹↹↹`
);
}

const acc = {};
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/counterTest.js.snap
Original file line number Diff line number Diff line change
@@ -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"`;

Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/histogramTest.js.snap
Original file line number Diff line number Diff line change
@@ -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"`;

Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/summaryTest.js.snap
Original file line number Diff line number Diff line change
@@ -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' ]"`;
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' ]"`;
20 changes: 20 additions & 0 deletions test/utilTest.js
Original file line number Diff line number Diff line change
@@ -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".',
);
});
});
});
Loading