metrics: fix info-gauge

This commit is contained in:
Martin Holst Swende 2024-11-26 21:14:30 +01:00
parent 3c3623d61e
commit 0d706d62fa
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
10 changed files with 24 additions and 44 deletions

View file

@ -196,7 +196,7 @@ func (exp *exp) syncToExpvar() {
exp.publishGauge(name, i.Snapshot()) exp.publishGauge(name, i.Snapshot())
case *metrics.GaugeFloat64: case *metrics.GaugeFloat64:
exp.publishGaugeFloat64(name, i.Snapshot()) exp.publishGaugeFloat64(name, i.Snapshot())
case metrics.GaugeInfo: case *metrics.GaugeInfo:
exp.publishGaugeInfo(name, i.Snapshot()) exp.publishGaugeInfo(name, i.Snapshot())
case metrics.Histogram: case metrics.Histogram:
exp.publishHistogram(name, i) exp.publishHistogram(name, i)

View file

@ -5,16 +5,6 @@ import (
"sync" "sync"
) )
type GaugeInfoSnapshot interface {
Value() GaugeInfoValue
}
// GaugeInfo holds a GaugeInfoValue value that can be set arbitrarily.
type GaugeInfo interface {
Update(GaugeInfoValue)
Snapshot() GaugeInfoSnapshot
}
// GaugeInfoValue is a mapping of keys to values // GaugeInfoValue is a mapping of keys to values
type GaugeInfoValue map[string]string type GaugeInfoValue map[string]string
@ -24,26 +14,23 @@ func (val GaugeInfoValue) String() string {
} }
// GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a // GetOrRegisterGaugeInfo returns an existing GaugeInfo or constructs and registers a
// new StandardGaugeInfo. // new GaugeInfo.
func GetOrRegisterGaugeInfo(name string, r Registry) GaugeInfo { func GetOrRegisterGaugeInfo(name string, r Registry) *GaugeInfo {
if nil == r { if nil == r {
r = DefaultRegistry r = DefaultRegistry
} }
return r.GetOrRegister(name, NewGaugeInfo()).(GaugeInfo) return r.GetOrRegister(name, NewGaugeInfo()).(*GaugeInfo)
} }
// NewGaugeInfo constructs a new StandardGaugeInfo. // NewGaugeInfo constructs a new GaugeInfo.
func NewGaugeInfo() GaugeInfo { func NewGaugeInfo() *GaugeInfo {
if !Enabled { return &GaugeInfo{
return NilGaugeInfo{}
}
return &StandardGaugeInfo{
value: GaugeInfoValue{}, value: GaugeInfoValue{},
} }
} }
// NewRegisteredGaugeInfo constructs and registers a new StandardGaugeInfo. // NewRegisteredGaugeInfo constructs and registers a new GaugeInfo.
func NewRegisteredGaugeInfo(name string, r Registry) GaugeInfo { func NewRegisteredGaugeInfo(name string, r Registry) *GaugeInfo {
c := NewGaugeInfo() c := NewGaugeInfo()
if nil == r { if nil == r {
r = DefaultRegistry r = DefaultRegistry
@ -53,31 +40,24 @@ func NewRegisteredGaugeInfo(name string, r Registry) GaugeInfo {
} }
// gaugeInfoSnapshot is a read-only copy of another GaugeInfo. // gaugeInfoSnapshot is a read-only copy of another GaugeInfo.
type gaugeInfoSnapshot GaugeInfoValue type GaugeInfoSnapshot GaugeInfoValue
// Value returns the value at the time the snapshot was taken. // Value returns the value at the time the snapshot was taken.
func (g gaugeInfoSnapshot) Value() GaugeInfoValue { return GaugeInfoValue(g) } func (g GaugeInfoSnapshot) Value() GaugeInfoValue { return GaugeInfoValue(g) }
type NilGaugeInfo struct{} // GaugeInfo maintains a set of key/value mappings.
type GaugeInfo struct {
func (NilGaugeInfo) Snapshot() GaugeInfoSnapshot { return NilGaugeInfo{} }
func (NilGaugeInfo) Update(v GaugeInfoValue) {}
func (NilGaugeInfo) Value() GaugeInfoValue { return GaugeInfoValue{} }
// StandardGaugeInfo is the standard implementation of a GaugeInfo and uses
// sync.Mutex to manage a single string value.
type StandardGaugeInfo struct {
mutex sync.Mutex mutex sync.Mutex
value GaugeInfoValue value GaugeInfoValue
} }
// Snapshot returns a read-only copy of the gauge. // Snapshot returns a read-only copy of the gauge.
func (g *StandardGaugeInfo) Snapshot() GaugeInfoSnapshot { func (g *GaugeInfo) Snapshot() GaugeInfoSnapshot {
return gaugeInfoSnapshot(g.value) return GaugeInfoSnapshot(g.value)
} }
// Update updates the gauge's value. // Update updates the gauge's value.
func (g *StandardGaugeInfo) Update(v GaugeInfoValue) { func (g *GaugeInfo) Update(v GaugeInfoValue) {
g.mutex.Lock() g.mutex.Lock()
defer g.mutex.Unlock() defer g.mutex.Unlock()
g.value = v g.value = v

View file

@ -73,7 +73,7 @@ func graphite(c *GraphiteConfig) error {
fmt.Fprintf(w, "%s.%s.value %d %d\n", c.Prefix, name, metric.Snapshot().Value(), now) fmt.Fprintf(w, "%s.%s.value %d %d\n", c.Prefix, name, metric.Snapshot().Value(), now)
case *GaugeFloat64: case *GaugeFloat64:
fmt.Fprintf(w, "%s.%s.value %f %d\n", c.Prefix, name, metric.Snapshot().Value(), now) fmt.Fprintf(w, "%s.%s.value %f %d\n", c.Prefix, name, metric.Snapshot().Value(), now)
case GaugeInfo: case *GaugeInfo:
fmt.Fprintf(w, "%s.%s.value %s %d\n", c.Prefix, name, metric.Snapshot().Value().String(), now) fmt.Fprintf(w, "%s.%s.value %s %d\n", c.Prefix, name, metric.Snapshot().Value().String(), now)
case Histogram: case Histogram:
h := metric.Snapshot() h := metric.Snapshot()

View file

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

View file

@ -33,7 +33,7 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
case *GaugeFloat64: case *GaugeFloat64:
l.Printf("gauge %s\n", name) l.Printf("gauge %s\n", name)
l.Printf(" value: %f\n", metric.Snapshot().Value()) l.Printf(" value: %f\n", metric.Snapshot().Value())
case GaugeInfo: case *GaugeInfo:
l.Printf("gauge %s\n", name) l.Printf("gauge %s\n", name)
l.Printf(" value: %s\n", metric.Snapshot().Value()) l.Printf(" value: %s\n", metric.Snapshot().Value())
case Healthcheck: case Healthcheck:

View file

@ -72,7 +72,7 @@ func (c *OpenTSDBConfig) writeRegistry(w io.Writer, now int64, shortHostname str
fmt.Fprintf(w, "put %s.%s.value %d %d host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname) fmt.Fprintf(w, "put %s.%s.value %d %d host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname)
case *GaugeFloat64: case *GaugeFloat64:
fmt.Fprintf(w, "put %s.%s.value %d %f host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname) fmt.Fprintf(w, "put %s.%s.value %d %f host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname)
case GaugeInfo: case *GaugeInfo:
fmt.Fprintf(w, "put %s.%s.value %d %s host=%s\n", c.Prefix, name, now, metric.Snapshot().Value().String(), shortHostname) fmt.Fprintf(w, "put %s.%s.value %d %s host=%s\n", c.Prefix, name, now, metric.Snapshot().Value().String(), shortHostname)
case Histogram: case Histogram:
h := metric.Snapshot() h := metric.Snapshot()

View file

@ -59,7 +59,7 @@ func (c *collector) Add(name string, i any) error {
c.addGauge(name, m.Snapshot()) c.addGauge(name, m.Snapshot())
case *metrics.GaugeFloat64: case *metrics.GaugeFloat64:
c.addGaugeFloat64(name, m.Snapshot()) c.addGaugeFloat64(name, m.Snapshot())
case metrics.GaugeInfo: case *metrics.GaugeInfo:
c.addGaugeInfo(name, m.Snapshot()) c.addGaugeInfo(name, m.Snapshot())
case metrics.Histogram: case metrics.Histogram:
c.addHistogram(name, m.Snapshot()) c.addHistogram(name, m.Snapshot())

View file

@ -214,7 +214,7 @@ func (r *StandardRegistry) Unregister(name string) {
func (r *StandardRegistry) loadOrRegister(name string, i interface{}) (interface{}, bool, bool) { func (r *StandardRegistry) loadOrRegister(name string, i interface{}) (interface{}, bool, bool) {
switch i.(type) { 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: default:
return nil, false, false return nil, false, false
} }

View file

@ -23,7 +23,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Snapshot().Value())) w.Info(fmt.Sprintf("gauge %s: value: %d", name, metric.Snapshot().Value()))
case *GaugeFloat64: case *GaugeFloat64:
w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Snapshot().Value())) w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Snapshot().Value()))
case GaugeInfo: case *GaugeInfo:
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value())) w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value()))
case Healthcheck: case Healthcheck:
metric.Check() metric.Check()

View file

@ -38,7 +38,7 @@ func WriteOnce(r Registry, w io.Writer) {
case *GaugeFloat64: case *GaugeFloat64:
fmt.Fprintf(w, "gauge %s\n", namedMetric.name) fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
fmt.Fprintf(w, " value: %f\n", metric.Snapshot().Value()) fmt.Fprintf(w, " value: %f\n", metric.Snapshot().Value())
case GaugeInfo: case *GaugeInfo:
fmt.Fprintf(w, "gauge %s\n", namedMetric.name) fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String()) fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String())
case Healthcheck: case Healthcheck: