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

fix: emit warn when labels and values are out of sync #57

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

leftieFriele
Copy link
Contributor

This PR improves the guard by emitting warn messages when the labels or values are undefined, which should make for less errors in application when requests are terminated or similar things happens.

Previously this would result in the error mentioned in #53

Closes #53

lib/guard.js Outdated
metric.labels.forEach((label) => {
if (label && (
(label.name === null || typeof label.name === 'undefined')
|| (label.value === null || typeof label.value === 'undefined')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is one thing, but can the value not be null or undefined? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is what we want to warn against, as you will get the error about arguments and labels not matching if value if null of undefined. right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For labels yes, but I think a label's value can be falsy and not fail. I can write a test.

Copy link
Contributor

@wkillerud wkillerud Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test to histogramTest.js in prom-client and it passes:

it('should not crash on falsy values', () => {
  let i = new Histogram({
    name: 'histo',
    help: 'help',
    labelNames: ['code'],
  });
  i = i.labels(undefined);
  expect(true).toEqual(true);
});

This one fails because we get two values for the one configured label name:

it('should not crash on falsy values', () => {
  let i = new Histogram({
    name: 'histo',
    help: 'help',
    labelNames: ['code'],
  });
  i = i.labels(undefined, undefined);
  expect(true).toEqual(true);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll add a test for this here as well.

@wkillerud
Copy link
Contributor

wkillerud commented Jun 21, 2024

A bit off topic, but these inline messages from GitHub are not helping readability 🙈

lib/guard.js Outdated
// push all label permutations on the metric into the
// Set for the metric in the registry. the size of
// this Set is compared with the threshold
metric.labels.forEach((label) => {
entry.add(`${label.name}${SEPARATOR}${label.value}`);
if (label && (label.name === null || typeof label.name === "undefined")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just scan all object properties for null / undefined values instead of targeting specific properties?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like a good idea 👍

Copy link
Contributor Author

@leftieFriele leftieFriele Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the types for MetricLabel and there null is valid for values, so I guess we should only look for undefined then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at this some more, metric.labels is an array of MetricLabels and that means I shouldn't have to look for anything but the name and value should as @wkillerud said be allowed to be null / undefined

Copy link
Contributor

@wkillerud wkillerud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant with these unit tests in prom-client was that undefined and null should be allowed as a value.

@@ -425,3 +425,50 @@ tap.test(
});
},
);

tap.test("Guard() - do not add metric when a label value is undefined and emit warning", (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An undefined value should be allowed in my opinion, and not emit a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve guard to avoid invalid number of arguments error message
3 participants