Codebase list golang-github-go-kit-kit / 0c243515-0bf2-4b25-8735-9c1038fa7c1b/v0.5.0 metrics / timer.go
0c243515-0bf2-4b25-8735-9c1038fa7c1b/v0.5.0

Tree @0c243515-0bf2-4b25-8735-9c1038fa7c1b/v0.5.0 (Download .tar.gz)

timer.go @0c243515-0bf2-4b25-8735-9c1038fa7c1b/v0.5.0

d595679
 
 
 
 
 
 
 
 
 
 
1cca2e4
d595679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
	}
	t.h.Observe(d)
}