metrics: Init -> Enable

This commit is contained in:
Martin Holst Swende 2024-11-27 05:18:53 +01:00
parent 9e22de7c47
commit df9fdbc58a
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
5 changed files with 11 additions and 13 deletions

View file

@ -1973,7 +1973,7 @@ func SetupMetrics(cfg *metrics.Config) {
if !cfg.Enabled { if !cfg.Enabled {
return return
} }
metrics.Init(true) metrics.Enable()
log.Info("Enabling metrics collection") log.Info("Enabling metrics collection")
var ( var (
enableExport = cfg.EnableInfluxDB enableExport = cfg.EnableInfluxDB

View file

@ -33,7 +33,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
metrics.Init(true) metrics.Enable()
os.Exit(m.Run()) os.Exit(m.Run())
} }

View file

@ -22,17 +22,12 @@ func Enabled() bool {
return metricsEnabled 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 // The Enabled-flag is expected to be set, once, during startup, but toggling off and on
// is not supported: YMMV. // is not supported,
// Init is not safe to call concurrently. It has no effect if it was already called. // Enable is not safe to call concurrently. It has no effect if it was already called.
func Init(enabled bool) { func Enable() {
metricsEnabled = enabled metricsEnabled = true
if initRan {
return
}
initRan = true
// TODO: Maybe start the ticker for exp delays, and things like that.
} }
var threadCreateProfile = pprof.Lookup("threadcreate") var threadCreateProfile = pprof.Lookup("threadcreate")

View file

@ -27,7 +27,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
metrics.Init(true) metrics.Enable()
os.Exit(m.Run()) os.Exit(m.Run())
} }

View file

@ -62,6 +62,9 @@ func (t *ResettingTimer) Time(f func()) {
// Record the duration of an event. // Record the duration of an event.
func (t *ResettingTimer) Update(d time.Duration) { func (t *ResettingTimer) Update(d time.Duration) {
if !metricsEnabled {
return
}
t.mutex.Lock() t.mutex.Lock()
defer t.mutex.Unlock() defer t.mutex.Unlock()
t.values = append(t.values, int64(d)) t.values = append(t.values, int64(d))