-
Notifications
You must be signed in to change notification settings - Fork 65
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
changes suggested by lint check #1072
changes suggested by lint check #1072
Conversation
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.
Looks good!
pkg/metrics/metrics_test.go
Outdated
@@ -27,7 +27,7 @@ func TestInitGauge(t *testing.T) { | |||
m.Set(22) | |||
|
|||
// then | |||
assert.Equal(t, float64(22), promtestutil.ToFloat64(m)) | |||
assert.InDelta(t, float64(22), promtestutil.ToFloat64(m), 0) |
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'm curios why linter doesn't like Equal()
here... :)
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.
Could you please revert the InDelta changes back to Equal?
pkg/metrics/metrics_test.go
Outdated
assert.Equal(t, float64(1), promtestutil.ToFloat64(m.WithLabelValues("member-1"))) | ||
assert.Equal(t, float64(2), promtestutil.ToFloat64(m.WithLabelValues("member-2"))) | ||
assert.InDelta(t, float64(1), promtestutil.ToFloat64(m.WithLabelValues("member-1")), 0) | ||
assert.InDelta(t, float64(2), promtestutil.ToFloat64(m.WithLabelValues("member-2")), 0) |
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.
This doesn't test the same - the value should be 2
, not something between 0
and 2
.
So I have the same question as Alexey - why is the Equal
so problematic? We use it all over the place. And why the Delta makes is better? I see it rather as a regression.
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 testifylint linter complains about the usage of Equal
for float numbers and suggests we use InDelta
or InEpsilon
instead: https://github.com/Antonboom/testifylint?tab=readme-ov-file#float-compare
The delta
arg is the "margin of error" of the value to check, not the lower-bound of a range (eg: [0,2]
)
InDelta asserts that the two numerals are within delta of each other.
We could also use this wrapper func, instead
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.
If I'm reading it right then from the technical perspective those two assertion are 100% identical:
assert.Equal(t, float64(1), promtestutil.ToFloat64(m.WithLabelValues("member-1")))
assert.InDelta(t, float64(1), promtestutil.ToFloat64(m.WithLabelValues("member-1")), 0)
First checks if two float64 numbers are equal. The second check if two float64 numbers are within a delta 0
which means exactly equal. So it's just a trick to make the linter happy.
If we wanted to avoid the potential floating point rounding issues we would need to use something like
assert.InDelta(t, float64(1), promtestutil.ToFloat64(m.WithLabelValues("member-1")), 0.0001)
I didn't look very closely but It looks like we shouldn't really have a rounding problem in this case. We use floats (because of how Prometheus metrics work I guess?) but they really always round to integers. So I would rather use Equal()
and suppress the linter so the code is easy to understand.
If for whatever reason we can expect the the actual metric can be slightly off (1.0001
instead of 1.0
) for example then I would use InDelta()
with some small delta. But I doubt we need it.
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.
So I would rather use Equal() and suppress the linter so the code is easy to understand.
I'd say using the wrapper function xavier suggested would keep the code easy to understand without suppressing the linter.
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.
@alexeykazakov I was thinking something similar when i wrote the delta to be 0 expecting that the two numbers would be exactly equal because of the rounding off.
On reading further I understand that the concern here is the way a float variable is represented in memory i.e even though we expect the value to be 2 - it could be stored in memory as 1.999999999 as the closest float to 2. ref- https://medium.com/pragmatic-programmers/testing-floating-point-numbers-in-go-9872fe6de17f
This explains:
- Why the linter expects to use a InDelta or Inepsilon for comparing floats.
- In the function InDelta/InEpsilon - the value of delta/epsilon should not be zero (it goes against the whole concept of using these separate functions for comparing float variable)
So looks like wrapper function would indeed be the best solution here?
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.
oh well, looks like using the wrapper won't be straightforward - because of this there is import cycling dependency - so will have to rearrange a few things.
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.
Let's maybe move the wrappers to common?
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.
yeah sure, do you want me to that in this same PR or maybe a separate one would be better because the wrapper is used in a lot of places.
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.
A separate PR would be easy to handle I think.
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.
here is the PR - #1073
once those are merged I will update the current PR as well accordingly.
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: alexeykazakov, MatousJobanek, ranakan19, xcoulon The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Quality Gate passedIssues Measures |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1072 +/- ##
=======================================
Coverage 85.08% 85.08%
=======================================
Files 55 55
Lines 5034 5034
=======================================
Hits 4283 4283
Misses 580 580
Partials 171 171 |
Separate PR with linter changes for easier review, otherwise #1057 would have been even bigger to review.
pre-req - #1073 to be merged