Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
14 changes: 2 additions & 12 deletions metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,15 @@ 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
// and launches a goroutine no matter the global switch is enabled or not.
// 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.
Expand Down
38 changes: 38 additions & 0 deletions metrics/counter.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2026 the libevm authors.

Check failure on line 1 in metrics/counter.libevm.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Pattern ((20\d\d\-2025)|(2025)) doesn't match. (goheader)
//
// 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
// <http://www.gnu.org/licenses/>.

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
}
14 changes: 2 additions & 12 deletions metrics/counter_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,15 @@ 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
// and launches a goroutine no matter the global switch is enabled or not.
// 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.
Expand Down
7 changes: 1 addition & 6 deletions metrics/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions metrics/gauge_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions metrics/gauge_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions metrics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions metrics/resetting_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions metrics/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading