Skip to content

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

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

Conversation

cl-bvl
Copy link

@cl-bvl cl-bvl commented Aug 7, 2025

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:

// Counter with SUM of all measurements
timerCounter := NewTimerCounter(Opts{Name: "timer_counter"})
go func() {
	defer timerCounter.Observe()()
	time.Sleep(2 * time.Second)
}()

// Histogram with all measurements
timerHistogram := NewTimerHistogram(HistogramOpts{Name: "timer_histogram", Buckets: []float64{1, 2, 3, 4, 5, 6}})
timerHistogram.Wrap(func() {
	time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
})

// Vector histogram. Most other types of timers also have a vector version.
timerHistogramVec := NewTimerHistogramVec(HistogramOpts{Name: "timer_histogram_vec", Buckets: []float64{1, 2, 3, 4, 5, 6}}, []string{"name"})
go func() {
	defer timerHistogramVec.Observe(map[string]string{"name": "timer1"})()
	time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
}()
timerHistogramVec.WrapLabelValues([]string{"timer2"}, func() {
	time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
})

// Counter with SUM of all measurements
// But the value also shows the sum of running timers. 
// It is well suited for long-running tasks - then the metric shows their running time until their completion.
timerContinuous := NewTimerContinuous(Opts{Name: "timer_continuous"}, 0)
timerContinuous.Wrap(func() {
	time.Sleep(5 * time.Minute)
})

cl-bvl added 2 commits August 8, 2025 02:40
Signed-off-by: Vladimir Buyanov <[email protected]>
Signed-off-by: Vladimir Buyanov <[email protected]>
@cl-bvl cl-bvl force-pushed the feature/timer_helpers branch from 1777cf3 to eb20df1 Compare August 7, 2025 23:40
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.

1 participant