metrics: remove interfaces for float64-counter

This commit is contained in:
Martin Holst Swende 2024-11-26 20:26:57 +01:00
parent d2a3d9a775
commit bea923556b
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
10 changed files with 30 additions and 85 deletions

View file

@ -5,114 +5,59 @@ import (
"sync/atomic"
)
type CounterFloat64Snapshot interface {
Count() float64
}
// CounterFloat64 holds a float64 value that can be incremented and decremented.
type CounterFloat64 interface {
Clear()
Dec(float64)
Inc(float64)
Snapshot() CounterFloat64Snapshot
}
// GetOrRegisterCounterFloat64 returns an existing CounterFloat64 or constructs and registers
// a new StandardCounterFloat64.
func GetOrRegisterCounterFloat64(name string, r Registry) CounterFloat64 {
// GetOrRegisterCounterFloat64 returns an existing *CounterFloat64 or constructs and registers
// a new CounterFloat64.
func GetOrRegisterCounterFloat64(name string, r Registry) *CounterFloat64 {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewCounterFloat64).(CounterFloat64)
return r.GetOrRegister(name, NewCounterFloat64).(*CounterFloat64)
}
// GetOrRegisterCounterFloat64Forced returns an existing CounterFloat64 or constructs and registers a
// new CounterFloat64 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 GetOrRegisterCounterFloat64Forced(name string, r Registry) CounterFloat64 {
if nil == r {
r = DefaultRegistry
}
return r.GetOrRegister(name, NewCounterFloat64Forced).(CounterFloat64)
// NewCounterFloat64 constructs a new CounterFloat64.
func NewCounterFloat64() *CounterFloat64 {
return new(CounterFloat64)
}
// NewCounterFloat64 constructs a new StandardCounterFloat64.
func NewCounterFloat64() CounterFloat64 {
if !Enabled {
return NilCounterFloat64{}
}
return &StandardCounterFloat64{}
}
// NewCounterFloat64Forced constructs a new StandardCounterFloat64 and returns it no matter if
// the global switch is enabled or not.
func NewCounterFloat64Forced() CounterFloat64 {
return &StandardCounterFloat64{}
}
// NewRegisteredCounterFloat64 constructs and registers a new StandardCounterFloat64.
func NewRegisteredCounterFloat64(name string, r Registry) CounterFloat64 {
// NewRegisteredCounterFloat64 constructs and registers a new CounterFloat64.
func NewRegisteredCounterFloat64(name string, r Registry) *CounterFloat64 {
c := NewCounterFloat64()
if nil == r {
if r == nil {
r = DefaultRegistry
}
r.Register(name, c)
return c
}
// 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
}
// counterFloat64Snapshot is a read-only copy of another CounterFloat64.
type counterFloat64Snapshot float64
// CounterFloat64Snapshot is a read-only copy of a float64 counter.
type CounterFloat64Snapshot float64
// Count returns the value at the time the snapshot was taken.
func (c counterFloat64Snapshot) Count() float64 { return float64(c) }
func (c CounterFloat64Snapshot) Count() float64 { return float64(c) }
type NilCounterFloat64 struct{}
func (NilCounterFloat64) Clear() {}
func (NilCounterFloat64) Count() float64 { return 0.0 }
func (NilCounterFloat64) Dec(i float64) {}
func (NilCounterFloat64) Inc(i float64) {}
func (NilCounterFloat64) Snapshot() CounterFloat64Snapshot { return NilCounterFloat64{} }
// StandardCounterFloat64 is the standard implementation of a CounterFloat64 and uses the
// atomic to manage a single float64 value.
type StandardCounterFloat64 struct {
// CounterFloat64 is the uses atomic to manage a single float64 value.
type CounterFloat64 struct {
floatBits atomic.Uint64
}
// Clear sets the counter to zero.
func (c *StandardCounterFloat64) Clear() {
func (c *CounterFloat64) Clear() {
c.floatBits.Store(0)
}
// Dec decrements the counter by the given amount.
func (c *StandardCounterFloat64) Dec(v float64) {
func (c *CounterFloat64) Dec(v float64) {
atomicAddFloat(&c.floatBits, -v)
}
// Inc increments the counter by the given amount.
func (c *StandardCounterFloat64) Inc(v float64) {
func (c *CounterFloat64) Inc(v float64) {
atomicAddFloat(&c.floatBits, v)
}
// Snapshot returns a read-only copy of the counter.
func (c *StandardCounterFloat64) Snapshot() CounterFloat64Snapshot {
v := math.Float64frombits(c.floatBits.Load())
return counterFloat64Snapshot(v)
func (c *CounterFloat64) Snapshot() CounterFloat64Snapshot {
return CounterFloat64Snapshot(math.Float64frombits(c.floatBits.Load()))
}
func atomicAddFloat(fbits *atomic.Uint64, v float64) {

View file

@ -190,7 +190,7 @@ func (exp *exp) syncToExpvar() {
switch i := i.(type) {
case *metrics.Counter:
exp.publishCounter(name, i.Snapshot())
case metrics.CounterFloat64:
case *metrics.CounterFloat64:
exp.publishCounterFloat64(name, i.Snapshot())
case metrics.Gauge:
exp.publishGauge(name, i.Snapshot())

View file

@ -67,7 +67,7 @@ func graphite(c *GraphiteConfig) error {
switch metric := i.(type) {
case *Counter:
fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, metric.Snapshot().Count(), now)
case CounterFloat64:
case *CounterFloat64:
fmt.Fprintf(w, "%s.%s.count %f %d\n", c.Prefix, name, metric.Snapshot().Count(), now)
case Gauge:
fmt.Fprintf(w, "%s.%s.value %d %d\n", c.Prefix, name, metric.Snapshot().Value(), now)

View file

@ -14,7 +14,7 @@ func readMeter(namespace, name string, i interface{}) (string, map[string]interf
"value": metric.Snapshot().Count(),
}
return measurement, fields
case metrics.CounterFloat64:
case *metrics.CounterFloat64:
measurement := fmt.Sprintf("%s%s.count", namespace, name)
fields := map[string]interface{}{
"value": metric.Snapshot().Count(),

View file

@ -24,7 +24,7 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
case *Counter:
l.Printf("counter %s\n", name)
l.Printf(" count: %9d\n", metric.Snapshot().Count())
case CounterFloat64:
case *CounterFloat64:
l.Printf("counter %s\n", name)
l.Printf(" count: %f\n", metric.Snapshot().Count())
case Gauge:

View file

@ -66,7 +66,7 @@ func (c *OpenTSDBConfig) writeRegistry(w io.Writer, now int64, shortHostname str
switch metric := i.(type) {
case *Counter:
fmt.Fprintf(w, "put %s.%s.count %d %d host=%s\n", c.Prefix, name, now, metric.Snapshot().Count(), shortHostname)
case CounterFloat64:
case *CounterFloat64:
fmt.Fprintf(w, "put %s.%s.count %d %f host=%s\n", c.Prefix, name, now, metric.Snapshot().Count(), shortHostname)
case Gauge:
fmt.Fprintf(w, "put %s.%s.value %d %d host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname)

View file

@ -53,7 +53,7 @@ func (c *collector) Add(name string, i any) error {
switch m := i.(type) {
case *metrics.Counter:
c.addCounter(name, m.Snapshot())
case metrics.CounterFloat64:
case *metrics.CounterFloat64:
c.addCounterFloat64(name, m.Snapshot())
case metrics.Gauge:
c.addGauge(name, m.Snapshot())

View file

@ -151,7 +151,7 @@ func (r *StandardRegistry) GetAll() map[string]map[string]interface{} {
switch metric := i.(type) {
case *Counter:
values["count"] = metric.Snapshot().Count()
case CounterFloat64:
case *CounterFloat64:
values["count"] = metric.Snapshot().Count()
case Gauge:
values["value"] = metric.Snapshot().Value()
@ -214,7 +214,7 @@ func (r *StandardRegistry) Unregister(name string) {
func (r *StandardRegistry) loadOrRegister(name string, i interface{}) (interface{}, bool, bool) {
switch i.(type) {
case *Counter, CounterFloat64, Gauge, GaugeFloat64, GaugeInfo, Healthcheck, Histogram, Meter, Timer, ResettingTimer:
case *Counter, *CounterFloat64, Gauge, GaugeFloat64, GaugeInfo, Healthcheck, Histogram, Meter, Timer, ResettingTimer:
default:
return nil, false, false
}

View file

@ -17,7 +17,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
switch metric := i.(type) {
case *Counter:
w.Info(fmt.Sprintf("counter %s: count: %d", name, metric.Snapshot().Count()))
case CounterFloat64:
case *CounterFloat64:
w.Info(fmt.Sprintf("counter %s: count: %f", name, metric.Snapshot().Count()))
case Gauge:
w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Snapshot().Value()))

View file

@ -29,7 +29,7 @@ func WriteOnce(r Registry, w io.Writer) {
case *Counter:
fmt.Fprintf(w, "counter %s\n", namedMetric.name)
fmt.Fprintf(w, " count: %9d\n", metric.Snapshot().Count())
case CounterFloat64:
case *CounterFloat64:
fmt.Fprintf(w, "counter %s\n", namedMetric.name)
fmt.Fprintf(w, " count: %f\n", metric.Snapshot().Count())
case Gauge: