Codebase list golang-github-go-kit-kit / dff50435-7a33-4f0c-bbdc-f455eb10d80a/v0.4.0 metrics / timer.go

Tree @dff50435-7a33-4f0c-bbdc-f455eb10d80a/v0.4.0 (Download .tar.gz)

timer.go @dff50435-7a33-4f0c-bbdc-f455eb10d80a/v0.4.0raw · history · blame

package metrics

import "time"

// Timer acts as a stopwatch, sending observations to a wrapped histogram.
// It's a bit of helpful syntax sugar for h.Observe(time.Since(x)).
type Timer struct {
	h Histogram
	t time.Time

// NewTimer wraps the given histogram and records the current time.
func NewTimer(h Histogram) *Timer {
	return &Timer{
		h: h,
		t: time.Now(),

// ObserveDuration captures the number of seconds since the timer was
// constructed, and forwards that observation to the histogram.
func (t *Timer) ObserveDuration() {
	d := time.Since(t.t).Seconds()
	if d < 0 {
		d = 0