Add observerFunc to observe with a Gauge

This commit is contained in:
beorn7 2016-11-11 12:33:56 +01:00
parent 8496756f6d
commit e63845e3ce
1 changed files with 7 additions and 5 deletions

View File

@ -21,10 +21,15 @@ type Observer interface {
Observe(float64) Observe(float64)
} }
type observerFunc func(float64)
func (o observerFunc) Observe(value float64) {
o(value)
}
type Timer struct { type Timer struct {
begin time.Time begin time.Time
observer Observer observer Observer
gauge Gauge
} }
func StartTimer() *Timer { func StartTimer() *Timer {
@ -37,7 +42,7 @@ func (t *Timer) With(o Observer) *Timer {
} }
func (t *Timer) WithGauge(g Gauge) *Timer { func (t *Timer) WithGauge(g Gauge) *Timer {
t.gauge = g t.observer = observerFunc(g.Set)
return t return t
} }
@ -45,7 +50,4 @@ func (t *Timer) Stop() {
if t.observer != nil { if t.observer != nil {
t.observer.Observe(time.Since(t.begin).Seconds()) t.observer.Observe(time.Since(t.begin).Seconds())
} }
if t.gauge != nil {
t.gauge.Set(time.Since(t.begin).Seconds())
}
} }