-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
lib/guard.js
Outdated
metric.labels.forEach((label) => { | ||
if (label && ( | ||
(label.name === null || typeof label.name === 'undefined') | ||
|| (label.value === null || typeof label.value === 'undefined') |
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
});
There was a problem hiding this comment.
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.
a54c52f
to
35cbdbf
Compare
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")) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 MetricLabel
s 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
There was a problem hiding this 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) => { |
There was a problem hiding this comment.
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.
This PR improves the guard by emitting
warn
messages when the labels or values areundefined
, 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