package metrics import ( "sync/atomic" "time" ) // Timer type is used to store a duration. type Timer struct { val atomic.Int64 desc *desc } // Name returns the name of the timer. func (t *Timer) Name() string { return t.desc.Name } // Value atomically returns the value of the timer. func (t *Timer) Value() time.Duration { return time.Duration(t.val.Load()) } // Update sets the timer value to d. func (t *Timer) Update(d time.Duration) { t.val.Store(int64(d)) } // UpdateSince sets the timer value to the difference between since and the current time. func (t *Timer) UpdateSince(since time.Time) { t.Update(time.Since(since)) } // AddSince adds to the timer's current value the difference between since and the current time. func (t *Timer) AddSince(since time.Time) { t.val.Add(int64(time.Since(since))) } // String returns a string representation using the name and value of the timer. func (t *Timer) String() string { return t.desc.Name + ": " + time.Duration(t.val.Load()).String() } // Time updates the timer to the duration it takes to call f. func (t *Timer) Time(f func()) { s := time.Now() f() t.UpdateSince(s) }