-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Timer helpers #1854
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
base: main
Are you sure you want to change the base?
Timer helpers #1854
Conversation
Signed-off-by: Vladimir Buyanov <b.vladimir@clickadu.com>
1777cf3
to
eb20df1
Compare
Thanks for this proposal! BTW: Likely here or Slack is a better place for those discussions. I have some questions around benefit vs cons (code to maintain, 2 ways of doing same thing). Correct if I'm wrong, but I see you proposed: 1)
|
func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc { |
NewCounterTimer
etc. Questions is... should add typed versions OR try write generic ones (Timer and TimerVec only would work for observers, perhaps even for all metrics if we use ObserveDuration for adding and setting logic).If we want to make NewTimer work for counters/gauges it could be possible without braking changes with any
, so
func NewTimer(o Observer) *Timer {
into
func NewTimer(o any) *Timer {
.. but maybe it's too much -- maybe it's ok to have per metric type functions with corresponding opts, similar to *Func types
Hello. 1) Wrap methods.Yes, it does exactly that. But with a few caveats.
VS
I understand that the difference is not very big, but it is there.
Can be mistakenly converted to:
And although this is 100% the fault of the programmer who did this, but it seems to me that a good API should protect against errors, and not push towards them. 2) TimerHistogram typeYou are right, this type is made to maintain a uniform API and does not provide new functionality. 3) TimerHistogramVec typeYes, we can do it like this:
Do you like this? 4) TimerCounter typeYes, we can call the method AddDuration instead of Observe. 5) TimerContinuous typeI poked around for 10 minutes and couldn't figure out how to use CounterFunc to implement a ticking timer. I'll think about it some more, but if you have an idea of what it might look like, that would help. |
Signed-off-by: Vladimir Buyanov <b.vladimir@clickadu.com>
9287210
to
7afe36d
Compare
Hello.
Regarding changing the current Timer. There are several objections why we don't want to do this.
Therefore, I would leave it in its current state and add alternative types. |
Great discussion! Just curious, who is "we" you are referring to in this PR? (:
Is the above example really correct? What do you want to measure here? It looks like in this example this would measure the time since the creation of the handler (program start) to the last request, no? Probably more realistic example would be:
...which proves that
I believe there are ways to not break compatibility if we use type casting and extend the interface on timer to
Not sure I understand this argument. What do you mean take one measurement? Do you mean one start time? (you can observe duration as many times as you want). If that's what you meant then I don't see how your structures are different here. In fact making the constructor taking opts To sum up.... I think we need to make a few high level decisions first:
A)
A)
B) NewHistogramVecTimer(Opts, labels) etc... similar to CounterFunc (nothing like that for histograms): client_golang/prometheus/counter.go Line 351 in 9e567a7
Seems like you propose 1B and 2B. I think 1B is fair, given what we have so far in client_golang.. I would be keen on adding this. For 2B... as I wrote above, I don't see the usefulness of 2B, 2A is more useful, given you can reuse the same metric (it's not cheap to create and register new metric, it's cheap to create a new start time variable) for multiple timers uses. For 2B one could add |
Also for your HTTP request example, there are better ways than timer, Then if we choose 2A, I don't see the need for *Vec flavor too 🙈 Ideally to make some decisions, we need more example on practical cases where existing timer is not enough (counter is one fair example) |
Hello.
I mean us - me, you, other contributors ;)
Yes, example is correct.
Yes, that's exactly how it works in the current version.
We can create timer in module contructor and use in methods, like:
In this example, we will measure all the time spent on executing the DoWork() function. It can be called in many goroutines concurently.
I don't think we should use something like NewTimer(any) In the current version, I propose to use 4 types of timers:
To unify the API, they now accept a metric, something like this
The current version offers 2A. Reset() method won't help. Timer should be able to work with multiple threads. I updated my first post with a "prod like" example of using the new counters. |
Hello.
I'm proposing to add several helper functions to make working with timers more convenient.
I haven’t brought this up on the mailing list yet, but I’d like to hear your feedback.
The code is still a draft; if you like the approach, I’ll add more detailed comments, tests, and documentation.
Here are a few examples of how the new timers could be used: