From dd7f747754d44b04057d63a5b5b2fc50b8162ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Irmak?= Date: Mon, 27 May 2024 09:46:09 +0300 Subject: [PATCH] feature: keep track of and export total time observed by a timer (#772) --- metrics/influxdb/influxdb.go | 1 + metrics/influxdb/influxdbv2.go | 1 + metrics/timer.go | 22 ++++++++++++++++++---- metrics/timer_test.go | 15 +++++++++++++++ params/version.go | 2 +- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/metrics/influxdb/influxdb.go b/metrics/influxdb/influxdb.go index c562a281b794..25368a7bc641 100644 --- a/metrics/influxdb/influxdb.go +++ b/metrics/influxdb/influxdb.go @@ -223,6 +223,7 @@ func (r *reporter) send() error { "m5": ms.Rate5(), "m15": ms.Rate15(), "meanrate": ms.RateMean(), + "total": int64(ms.Total()), }, Time: now, }) diff --git a/metrics/influxdb/influxdbv2.go b/metrics/influxdb/influxdbv2.go index 24808791a98d..9996caa694d1 100644 --- a/metrics/influxdb/influxdbv2.go +++ b/metrics/influxdb/influxdbv2.go @@ -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) diff --git a/metrics/timer.go b/metrics/timer.go index 2e1a9be47295..a4dd8281e7ee 100644 --- a/metrics/timer.go +++ b/metrics/timer.go @@ -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 +} diff --git a/metrics/timer_test.go b/metrics/timer_test.go index 903e8e8d496e..162a37cdd019 100644 --- a/metrics/timer_test.go +++ b/metrics/timer_test.go @@ -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) + } +} diff --git a/params/version.go b/params/version.go index a9559904c1b9..145c4bf1b27f 100644 --- a/params/version.go +++ b/params/version.go @@ -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 )