feature: keep track of and export total time observed by a timer (#772)

This commit is contained in:
Ömer Faruk Irmak 2024-05-27 09:46:09 +03:00 committed by GitHub
parent 9b4a779104
commit dd7f747754
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 36 additions and 5 deletions

View file

@ -223,6 +223,7 @@ func (r *reporter) send() error {
"m5": ms.Rate5(),
"m15": ms.Rate15(),
"meanrate": ms.RateMean(),
"total": int64(ms.Total()),
},
Time: now,
})

View file

@ -190,6 +190,7 @@ func (r *v2Reporter) send() {
"m5": ms.Rate5(),
"m15": ms.Rate15(),
"meanrate": ms.RateMean(),
"total": int64(ms.Total()),
}
pt := influxdb2.NewPoint(measurement, r.tags, fields, now)

View file

@ -25,6 +25,7 @@ type Timer interface {
Update(time.Duration)
UpdateSince(time.Time)
Variance() float64
Total() time.Duration
}
// GetOrRegisterTimer returns an existing Timer or constructs and registers a
@ -134,11 +135,14 @@ func (NilTimer) UpdateSince(time.Time) {}
// Variance is a no-op.
func (NilTimer) Variance() float64 { return 0.0 }
func (NilTimer) Total() time.Duration { return time.Duration(0) }
// StandardTimer is the standard implementation of a Timer and uses a Histogram
// and Meter.
type StandardTimer struct {
histogram Histogram
meter Meter
total time.Duration
mutex sync.Mutex
}
@ -200,6 +204,7 @@ func (t *StandardTimer) Snapshot() Timer {
return &TimerSnapshot{
histogram: t.histogram.Snapshot().(*HistogramSnapshot),
meter: t.meter.Snapshot().(*MeterSnapshot),
total: t.total,
}
}
@ -231,14 +236,12 @@ func (t *StandardTimer) Update(d time.Duration) {
defer t.mutex.Unlock()
t.histogram.Update(int64(d))
t.meter.Mark(1)
t.total += d
}
// Record the duration of an event that started at a time and ends now.
func (t *StandardTimer) UpdateSince(ts time.Time) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.histogram.Update(int64(time.Since(ts)))
t.meter.Mark(1)
t.Update(time.Since(ts))
}
// Variance returns the variance of the values in the sample.
@ -246,10 +249,16 @@ func (t *StandardTimer) Variance() float64 {
return t.histogram.Variance()
}
// Total returns the total time that events observed by this timer took
func (t *StandardTimer) Total() time.Duration {
return t.total
}
// TimerSnapshot is a read-only copy of another Timer.
type TimerSnapshot struct {
histogram *HistogramSnapshot
meter *MeterSnapshot
total time.Duration
}
// Count returns the number of events recorded at the time the snapshot was
@ -324,3 +333,8 @@ func (*TimerSnapshot) UpdateSince(time.Time) {
// Variance returns the variance of the values at the time the snapshot was
// taken.
func (t *TimerSnapshot) Variance() float64 { return t.histogram.Variance() }
// Total returns the total time that events observed by this timer took
func (t *TimerSnapshot) Total() time.Duration {
return t.total
}

View file

@ -112,3 +112,18 @@ func ExampleGetOrRegisterTimer() {
t.Update(47)
fmt.Println(t.Max()) // Output: 47
}
func TestTimerSum(t *testing.T) {
tm := GetOrRegisterTimer("test.timer.sum", nil)
times := 5000000
for i := 0; i < times; i++ {
tm.Update(time.Second)
}
ss := tm.Snapshot()
if total := tm.Total().Seconds(); total != float64(times) {
t.Errorf("tm.Total().Seconds(): 5000000.0 != %v\n", total)
}
if total := ss.Total().Seconds(); total != float64(times) {
t.Errorf("ss.Total().Seconds(): 5000000.0 != %v\n", total)
}
}

View file

@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 3 // Minor version component of the current release
VersionPatch = 22 // Patch version component of the current release
VersionPatch = 23 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)