Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions metrics/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func (r *reporter) send() error {
"m5": ms.Rate5(),
"m15": ms.Rate15(),
"meanrate": ms.RateMean(),
"total": int64(ms.Total()),
},
Time: now,
})
Expand Down
1 change: 1 addition & 0 deletions metrics/influxdb/influxdbv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 18 additions & 4 deletions metrics/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -200,6 +204,7 @@ func (t *StandardTimer) Snapshot() Timer {
return &TimerSnapshot{
histogram: t.histogram.Snapshot().(*HistogramSnapshot),
meter: t.meter.Snapshot().(*MeterSnapshot),
total: t.total,
}
}

Expand Down Expand Up @@ -231,25 +236,29 @@ 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.
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
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions metrics/timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down