Skip to content

Commit

Permalink
metrics: add force option for metric system (ethereum#17667)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Dec 16, 2024
1 parent 3c679c8 commit 48e0de0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
3 changes: 0 additions & 3 deletions metrics/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ type EWMA interface {

// NewEWMA constructs a new EWMA with the given alpha.
func NewEWMA(alpha float64) EWMA {
if !Enabled {
return NilEWMA{}
}
return &StandardEWMA{alpha: alpha}
}

Expand Down
43 changes: 41 additions & 2 deletions metrics/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ func GetOrRegisterMeter(name string, r Registry) Meter {
return r.GetOrRegister(name, NewMeter).(Meter)
}

// GetOrRegisterMeterForced returns an existing Meter or constructs and registers a
// new StandardMeter no matter the global switch is enabled or not.
// Be sure to unregister the meter from the registry once it is of no use to
// allow for garbage collection.
func GetOrRegisterMeterForced(name string, r Registry) Meter {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewMeterForced).(Meter)
}

// NewMeter constructs a new StandardMeter and launches a goroutine.
// Be sure to call Stop() once the meter is of no use to allow for garbage collection.
func NewMeter() Meter {
Expand All @@ -46,8 +57,23 @@ func NewMeter() Meter {
return m
}

// NewMeter constructs and registers a new StandardMeter and launches a
// goroutine.
// NewMeterForced constructs a new StandardMeter and launches a goroutine no matter
// the global switch is enabled or not.
// Be sure to call Stop() once the meter is of no use to allow for garbage collection.
func NewMeterForced() Meter {
m := newStandardMeter()
arbiter.Lock()
defer arbiter.Unlock()
arbiter.meters[m] = struct{}{}
if !arbiter.started {
arbiter.started = true
go arbiter.tick()
}
return m
}

// NewRegisteredMeter constructs and registers a new StandardMeter
// and launches a goroutine.
// Be sure to unregister the meter from the registry once it is of no use to
// allow for garbage collection.
func NewRegisteredMeter(name string, r Registry) Meter {
Expand All @@ -59,6 +85,19 @@ func NewRegisteredMeter(name string, r Registry) Meter {
return c
}

// NewRegisteredMeterForced constructs and registers a new StandardMeter
// and launches a goroutine no matter the global switch is enabled or not.
// Be sure to unregister the meter from the registry once it is of no use to
// allow for garbage collection.
func NewRegisteredMeterForced(name string, r Registry) Meter {
c := NewMeterForced()
if nil == r {
r = DefaultRegistry
}
r.Register(name, c)
return c
}

// MeterSnapshot is a read-only copy of another Meter.
type MeterSnapshot struct {
count int64
Expand Down

0 comments on commit 48e0de0

Please sign in to comment.