From bea923556b03a0aa03740fd36c930068712e25b0 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 26 Nov 2024 20:26:57 +0100 Subject: [PATCH] metrics: remove interfaces for float64-counter --- metrics/counter_float64.go | 95 +++++++-------------------------- metrics/exp/exp.go | 2 +- metrics/graphite.go | 2 +- metrics/influxdb/influxdb.go | 2 +- metrics/log.go | 2 +- metrics/opentsdb.go | 2 +- metrics/prometheus/collector.go | 2 +- metrics/registry.go | 4 +- metrics/syslog.go | 2 +- metrics/writer.go | 2 +- 10 files changed, 30 insertions(+), 85 deletions(-) diff --git a/metrics/counter_float64.go b/metrics/counter_float64.go index 15c81494ef..01ff143db8 100644 --- a/metrics/counter_float64.go +++ b/metrics/counter_float64.go @@ -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) { diff --git a/metrics/exp/exp.go b/metrics/exp/exp.go index b90cf2d2d2..294c43c362 100644 --- a/metrics/exp/exp.go +++ b/metrics/exp/exp.go @@ -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()) diff --git a/metrics/graphite.go b/metrics/graphite.go index 1ed6cae4ed..9f6637450d 100644 --- a/metrics/graphite.go +++ b/metrics/graphite.go @@ -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) diff --git a/metrics/influxdb/influxdb.go b/metrics/influxdb/influxdb.go index 0be36fade1..d702b59728 100644 --- a/metrics/influxdb/influxdb.go +++ b/metrics/influxdb/influxdb.go @@ -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(), diff --git a/metrics/log.go b/metrics/log.go index 296047e447..ebeab82dda 100644 --- a/metrics/log.go +++ b/metrics/log.go @@ -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: diff --git a/metrics/opentsdb.go b/metrics/opentsdb.go index 695a96b4f5..c56f363341 100644 --- a/metrics/opentsdb.go +++ b/metrics/opentsdb.go @@ -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) diff --git a/metrics/prometheus/collector.go b/metrics/prometheus/collector.go index a2af4f5e97..897d6feee4 100644 --- a/metrics/prometheus/collector.go +++ b/metrics/prometheus/collector.go @@ -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()) diff --git a/metrics/registry.go b/metrics/registry.go index 37b314bc78..74d8943862 100644 --- a/metrics/registry.go +++ b/metrics/registry.go @@ -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 } diff --git a/metrics/syslog.go b/metrics/syslog.go index 5110ea513d..991c39cb50 100644 --- a/metrics/syslog.go +++ b/metrics/syslog.go @@ -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())) diff --git a/metrics/writer.go b/metrics/writer.go index 56bcf1516f..c1dd1a087c 100644 --- a/metrics/writer.go +++ b/metrics/writer.go @@ -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: