-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 CollectAndCount #753
Conversation
Now that we have also added CollectAndLint and GatherAndLint, I thought we should bring CollectAndCount in line. So: - Add GatherAndCount. - Add filtering by metric name. - Add tests. Minor wart: CollectAndCount should now return `(int, error)`, but that would be a breaking change as the current version just returns `int`. I decided to let the new version panic when it should return an error. An error is anyway very unlikely, so the biggest annoyance here is really just the inconsistency. Signed-off-by: beorn7 <[email protected]>
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 don't think this does what I was thinking about trying to do in a test the other week, but LGTM regardless.
You wanted to write a test if a certain metric shows up in your exposition. That's possible with the new tooling here because you can just put that metric name into the filter and count if there is at least one in the exposition. |
Isn't the pedantic registry kind of messing with that? I wanted to check if a list of metric names had been registered with an existing |
That's where the |
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.
Thanks @beorn7, but generally I don't see the real purpose of this. 🤔
We had an elegant function that was allowing us to just do testutil.Equals(t, 1, CollecAndCount(myMetricVar))
and now we want to make it more verbose and return some error/panic? 🤔
This is quite tragic as we use it in many places and it converts (some day in new version) to:
count, err := CollecAndCount(myMetricVar)
testutil.Ok(t, err)
testutil.Equals(t, 1, count)
This is clearly unnecessary verbose, and this function was done ONLY for testing.
..Anyone asked for that? 🤔 😄
That's why I am not a fan of this change... especially what's the improvement of this if the previous version was working just fine without error, allowing nice, not obtrusive use in test code. Also adding metricName
if we have collector passed already does not make any sense to me.. So we should use metric name or collector... or both?
So first question
Can we NOT change CollectAndCount
signature at all? And NOT use registering inside (because why?).
Secondly
TBH I would totally NOT recommend using GatherAndCount
at all, because the metric name can change anytime plus you can make an easy typo... I liked CollectAndCount
because I could do type-safe
validation (!). If you can use directly the metric defined in the code.. there's nothing better than that. And getting metric name from metric variable is non-trivial.
So... can we just NOT introduce GatherAndCount
? 😄 Is there any user of that? 🤔
Sounds like @cstyan already found a solution.
Alternatives
Since we have Must register
hidden in promauto.With
anyway. I might think panic is ok...? Or MustCollectAndCount
? But no one even asked for returning explicit error so it also feels weird that we add code that no one asks for.. 🤔 Also I could not see anyone asking for GatherAndCount
either...
@bwplotka You must have misunderstood. And as I tried to explain, we don't need to change it to return an error. This PR does not change it to return an error. The registration is needed to enable the filtering. Since the registration will never fail in practice, I wouldn't introduce a different code path for the case of no filtering. And yes, there is a need for And there are always people who ask for the option to handle an error instead of panicking. That's why all the other functions return an error. But as you personally do not seem to need |
Hm but the single collector has just one metric name, so what is this filtering 🤔 I guess historgrams / summaries which have more than one? Ok, then fine (:
I know @beorn7 but you say it's broken that it does not return error... so I am arguing with that. (: |
Cool, looks like what we need is |
How brutal would be to introduce breaking change? and mention this in CHANGELOG? This is particularly new function. |
That's not true. A With the filtering, you can easily test for presence of a particular metric in your home-grown
That's not true, either. (o: Histograms and summaries only have one metric name. (The "magic" metric names you see in Prometheus and in the text format are not "real" names.)
I'm not saying it's broken. It's just inconsistent with the four or five other functions we have in this package. I'd prefer to have them all panic or all return an error. But either change would be breaking.
Strictly speaking, yes, but I think in this case, it would be overkill. Personally, I'd be fine with all the functions panicking, but the error-return version is less controversial and more flexible (even if it makes the handling less elegant – but why are you using Go if you want elegant code? 🤯 ). We got the version with errors from the 1st time contributor, and when you contributed What we have in this PR now has just a tiny consistency wart.
Given the gigantic adoption of this module, I'd really bump the major version for any breaking change in a part that is not explicitly marked as experimental. But then I want to bundle a bunch of batched up breaking changes to not inflict the cost of a major version bump to change a single function signature. |
Hmm okay, yes I can see how |
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.
Thanks for the elaboration. I was wrong with collector behavior then. Filtering by metric name makes. (:
I'm not saying it's broken. It's just inconsistent with the four or five other functions we have in this package. I'd prefer to have them all panic or all return an error. But either change would be breaking.
Ok think I can agree with your friendliness towards users (: so let's not break compatibility.
But still, I don't fully agree with the comment that is "predicting" that panicking is wrong here (:
Sorry for begin pain (: Unless I am missing something, the only kind of errors that can happen in Collector
is someone creating an invalid metric name 🤔 Plus this is only testing package.
But I think I get that it would be just easier for library and more solid to return error explicitly...
Damn, we will need to create a test helper, that's it, 🤷♂️
LGTM (: Thanks for the discussion, makes sense.
There will certainly be a few people patronizing me at the next meetup (as it happened before, and of course only if we will have proper meetups again any time soon…). 🤓
Which can plausibly happen given that the helper function will often be used to test custom collectors. The collector could also collect a set of metrics that is not self-consistent.
Some people feel especially strong about not panicking in test code. In general, instead of trying to convince everyone to do the one right thing, I try to accommodate both schools of thought, at least if that's possible without a lot of overhead. |
Kind of I am. But I am sometimes also not decided xd 👍 |
Now that we have also added CollectAndLint and GatherAndLint, I
thought we should bring CollectAndCount in line. So:
Minor wart: CollectAndCount should now return
(int, error)
, but thatwould be a breaking change as the current version just returns
int
. I decided to let the new version panic when it should return anerror. An error is anyway very unlikely, so the biggest annoyance here
is really just the inconsistency.
@bwplotka you might like this.
@cstyan you needed this recently (but as already discussed, with
promauto
you won't, but your request still inspired this PR)