diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 712729ec64..ec8ed678f5 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1973,7 +1973,7 @@ func SetupMetrics(cfg *metrics.Config) { if !cfg.Enabled { return } - metrics.Init(true) + metrics.Enable() log.Info("Enabling metrics collection") var ( enableExport = cfg.EnableInfluxDB diff --git a/metrics/influxdb/influxdb_test.go b/metrics/influxdb/influxdb_test.go index ba896510d2..547da138b8 100644 --- a/metrics/influxdb/influxdb_test.go +++ b/metrics/influxdb/influxdb_test.go @@ -33,7 +33,7 @@ import ( ) func TestMain(m *testing.M) { - metrics.Init(true) + metrics.Enable() os.Exit(m.Run()) } diff --git a/metrics/metrics.go b/metrics/metrics.go index 2245093501..6b8b9f9ec4 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -22,17 +22,12 @@ func Enabled() bool { return metricsEnabled } -// Init enables the metrics system. +// Enable enables the metrics system. // The Enabled-flag is expected to be set, once, during startup, but toggling off and on -// is not supported: YMMV. -// Init is not safe to call concurrently. It has no effect if it was already called. -func Init(enabled bool) { - metricsEnabled = enabled - if initRan { - return - } - initRan = true - // TODO: Maybe start the ticker for exp delays, and things like that. +// is not supported, +// Enable is not safe to call concurrently. It has no effect if it was already called. +func Enable() { + metricsEnabled = true } var threadCreateProfile = pprof.Lookup("threadcreate") diff --git a/metrics/prometheus/collector_test.go b/metrics/prometheus/collector_test.go index 6ddca67652..a8585d1226 100644 --- a/metrics/prometheus/collector_test.go +++ b/metrics/prometheus/collector_test.go @@ -27,7 +27,7 @@ import ( ) func TestMain(m *testing.M) { - metrics.Init(true) + metrics.Enable() os.Exit(m.Run()) } diff --git a/metrics/resetting_timer.go b/metrics/resetting_timer.go index f2236e2347..1b3e87bc3d 100644 --- a/metrics/resetting_timer.go +++ b/metrics/resetting_timer.go @@ -62,6 +62,9 @@ func (t *ResettingTimer) Time(f func()) { // Record the duration of an event. func (t *ResettingTimer) Update(d time.Duration) { + if !metricsEnabled { + return + } t.mutex.Lock() defer t.mutex.Unlock() t.values = append(t.values, int64(d))