From 32bffb270ed467f1d7489e7956a8890aff6c14e4 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Thu, 6 Mar 2025 08:22:22 +0100 Subject: [PATCH 1/4] fix(metrics): New constructors use GetOrRegister functions - Avoid conflicts for metrics used both in consumers and in libevm - See https://github.com/ava-labs/coreth/pull/848 --- metrics/counter.go | 14 ++------------ metrics/counter_float64.go | 14 ++------------ metrics/gauge.go | 7 +------ metrics/gauge_float64.go | 7 +------ metrics/gauge_info.go | 7 +------ metrics/histogram.go | 7 +------ metrics/resetting_timer.go | 7 +------ metrics/timer.go | 7 +------ 8 files changed, 10 insertions(+), 60 deletions(-) diff --git a/metrics/counter.go b/metrics/counter.go index dbe8e16a90d..1b670e266b7 100644 --- a/metrics/counter.go +++ b/metrics/counter.go @@ -52,12 +52,7 @@ func NewCounterForced() Counter { // NewRegisteredCounter constructs and registers a new StandardCounter. func NewRegisteredCounter(name string, r Registry) Counter { - c := NewCounter() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterCounter(name, r) } // NewRegisteredCounterForced constructs and registers a new StandardCounter @@ -65,12 +60,7 @@ func NewRegisteredCounter(name string, r Registry) Counter { // Be sure to unregister the counter from the registry once it is of no use to // allow for garbage collection. func NewRegisteredCounterForced(name string, r Registry) Counter { - c := NewCounterForced() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterCounterForced(name, r) } // counterSnapshot is a read-only copy of another Counter. diff --git a/metrics/counter_float64.go b/metrics/counter_float64.go index 15c81494efb..e5d90b27274 100644 --- a/metrics/counter_float64.go +++ b/metrics/counter_float64.go @@ -53,12 +53,7 @@ func NewCounterFloat64Forced() CounterFloat64 { // NewRegisteredCounterFloat64 constructs and registers a new StandardCounterFloat64. func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 { - c := NewCounterFloat64() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterCounterFloat64(name, r) } // NewRegisteredCounterFloat64Forced constructs and registers a new StandardCounterFloat64 @@ -66,12 +61,7 @@ func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 { // Be sure to unregister the counter from the registry once it is of no use to // allow for garbage collection. func NewRegisteredCounterFloat64Forced(name string, r Registry) CounterFloat64 { - c := NewCounterFloat64Forced() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterCounterFloat64Forced(name, r) } // counterFloat64Snapshot is a read-only copy of another CounterFloat64. diff --git a/metrics/gauge.go b/metrics/gauge.go index 5933df31078..86cf6e729f2 100644 --- a/metrics/gauge.go +++ b/metrics/gauge.go @@ -35,12 +35,7 @@ func NewGauge() Gauge { // NewRegisteredGauge constructs and registers a new StandardGauge. func NewRegisteredGauge(name string, r Registry) Gauge { - c := NewGauge() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterGauge(name, r) } // gaugeSnapshot is a read-only copy of another Gauge. diff --git a/metrics/gauge_float64.go b/metrics/gauge_float64.go index c1c3c6b6e6f..750d9aec299 100644 --- a/metrics/gauge_float64.go +++ b/metrics/gauge_float64.go @@ -34,12 +34,7 @@ func NewGaugeFloat64() GaugeFloat64 { // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64. func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 { - c := NewGaugeFloat64() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterGaugeFloat64(name, r) } // gaugeFloat64Snapshot is a read-only copy of another GaugeFloat64. diff --git a/metrics/gauge_info.go b/metrics/gauge_info.go index 0010edc3249..2f69b245783 100644 --- a/metrics/gauge_info.go +++ b/metrics/gauge_info.go @@ -44,12 +44,7 @@ func NewGaugeInfo() GaugeInfo { // NewRegisteredGaugeInfo constructs and registers a new StandardGaugeInfo. func NewRegisteredGaugeInfo(name string, r Registry) GaugeInfo { - c := NewGaugeInfo() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterGaugeInfo(name, r) } // gaugeInfoSnapshot is a read-only copy of another GaugeInfo. diff --git a/metrics/histogram.go b/metrics/histogram.go index 10259a24637..c4c05011159 100644 --- a/metrics/histogram.go +++ b/metrics/histogram.go @@ -40,12 +40,7 @@ func NewHistogram(s Sample) Histogram { // NewRegisteredHistogram constructs and registers a new StandardHistogram from // a Sample. func NewRegisteredHistogram(name string, r Registry, s Sample) Histogram { - c := NewHistogram(s) - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterHistogram(name, r, s) } // NilHistogram is a no-op Histogram. diff --git a/metrics/resetting_timer.go b/metrics/resetting_timer.go index 6802e3fcea9..e55e872feee 100644 --- a/metrics/resetting_timer.go +++ b/metrics/resetting_timer.go @@ -35,12 +35,7 @@ func GetOrRegisterResettingTimer(name string, r Registry) ResettingTimer { // NewRegisteredResettingTimer constructs and registers a new StandardResettingTimer. func NewRegisteredResettingTimer(name string, r Registry) ResettingTimer { - c := NewResettingTimer() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterResettingTimer(name, r) } // NewResettingTimer constructs a new StandardResettingTimer diff --git a/metrics/timer.go b/metrics/timer.go index bb8def82fb2..89c7817be39 100644 --- a/metrics/timer.go +++ b/metrics/timer.go @@ -46,12 +46,7 @@ func NewCustomTimer(h Histogram, m Meter) Timer { // Be sure to unregister the meter from the registry once it is of no use to // allow for garbage collection. func NewRegisteredTimer(name string, r Registry) Timer { - c := NewTimer() - if nil == r { - r = DefaultRegistry - } - r.Register(name, c) - return c + return GetOrRegisterTimer(name, r) } // NewTimer constructs a new StandardTimer using an exponentially-decaying From d1e374a7f932417ce31a3cd611b245511185d885 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Thu, 6 Mar 2025 09:14:29 +0100 Subject: [PATCH 2/4] `GetOrRegisterOrOverrideCounter` --- metrics/counter.libevm.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 metrics/counter.libevm.go diff --git a/metrics/counter.libevm.go b/metrics/counter.libevm.go new file mode 100644 index 00000000000..bec6730acad --- /dev/null +++ b/metrics/counter.libevm.go @@ -0,0 +1,38 @@ +// Copyright 2026 the libevm authors. +// +// The libevm additions to go-ethereum are free software: you can redistribute +// them and/or modify them under the terms of the GNU Lesser General Public License +// as published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The libevm additions are distributed in the hope that they will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see +// . + +package metrics + +// GetOrRegisterOrOverrideCounter searches for a metric already registered +// with the `name` given. If a metric is found and it is a [Counter], this one +// is returned. If a metric is found and it is not a [Counter], it is unregistered +// and replaced with a new registered [Counter]. If no metric is found, a new +// [Counter] is constructed and registered. +// This is especially useful for a metric defined in this project with a different +// type than a metric defined in a consumer of this project, for the same name. +func GetOrRegisterOrOverrideCounter(name string, r Registry) Counter { + if r == nil { + r = DefaultRegistry + } + counter, ok := r.GetOrRegister(name, NewCounter).(Counter) + if ok { + return counter + } + r.Unregister(name) + counter = NewCounter() + _ = r.Register(name, counter) + return counter +} From 60aaae1942daf519cebd37e62aaf75f2111f9965 Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Thu, 6 Mar 2025 11:47:39 +0100 Subject: [PATCH 3/4] Fix tests --- metrics/testdata/opentsb.want | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/testdata/opentsb.want b/metrics/testdata/opentsb.want index 43fe1b2ac27..4ed0fcbaa74 100644 --- a/metrics/testdata/opentsb.want +++ b/metrics/testdata/opentsb.want @@ -20,4 +20,4 @@ put pre.second.one-minute 978307200 0.00 host=hal9000 put pre.second.five-minute 978307200 0.00 host=hal9000 put pre.second.fifteen-minute 978307200 0.00 host=hal9000 put pre.second.mean-rate 978307200 0.00 host=hal9000 -put pre.tau.count 978307200 1.570000 host=hal9000 +put pre.tau.count 978307200 4.710000 host=hal9000 From 4869223a44173c438d950583229064b7c6126b3d Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Thu, 6 Mar 2025 14:41:07 +0100 Subject: [PATCH 4/4] Fix header year --- metrics/counter.libevm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics/counter.libevm.go b/metrics/counter.libevm.go index bec6730acad..bc2f512f440 100644 --- a/metrics/counter.libevm.go +++ b/metrics/counter.libevm.go @@ -1,4 +1,4 @@ -// Copyright 2026 the libevm authors. +// Copyright 2025 the libevm authors. // // The libevm additions to go-ethereum are free software: you can redistribute // them and/or modify them under the terms of the GNU Lesser General Public License