mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
metrics: split gaugeinfo into read/write
This commit is contained in:
parent
0abadecea1
commit
f4d0f94590
9 changed files with 30 additions and 128 deletions
|
|
@ -127,7 +127,7 @@ func (exp *exp) publishGaugeFloat64(name string, metric metrics.GaugeFloat64Snap
|
|||
exp.getFloat(name).Set(metric.Value())
|
||||
}
|
||||
|
||||
func (exp *exp) publishGaugeInfo(name string, metric metrics.GaugeInfo) {
|
||||
func (exp *exp) publishGaugeInfo(name string, metric metrics.GaugeInfoSnapshot) {
|
||||
exp.getInfo(name).Set(metric.Value().String())
|
||||
}
|
||||
|
||||
|
|
@ -197,7 +197,7 @@ func (exp *exp) syncToExpvar() {
|
|||
case metrics.GaugeFloat64:
|
||||
exp.publishGaugeFloat64(name, i.Snapshot())
|
||||
case metrics.GaugeInfo:
|
||||
exp.publishGaugeInfo(name, i)
|
||||
exp.publishGaugeInfo(name, i.Snapshot())
|
||||
case metrics.Histogram:
|
||||
exp.publishHistogram(name, i)
|
||||
case metrics.Meter:
|
||||
|
|
|
|||
|
|
@ -5,14 +5,17 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
// GaugeInfos hold a GaugeInfoValue value that can be set arbitrarily.
|
||||
type GaugeInfo interface {
|
||||
Snapshot() GaugeInfo
|
||||
Update(GaugeInfoValue)
|
||||
type GaugeInfoSnapshot interface {
|
||||
Value() GaugeInfoValue
|
||||
}
|
||||
|
||||
// GaugeInfoValue is a mappng of (string) keys to (string) values
|
||||
// GaugeInfos hold a GaugeInfoValue value that can be set arbitrarily.
|
||||
type GaugeInfo interface {
|
||||
Update(GaugeInfoValue)
|
||||
Snapshot() GaugeInfoSnapshot
|
||||
}
|
||||
|
||||
// GaugeInfoValue is a mapping of keys to values
|
||||
type GaugeInfoValue map[string]string
|
||||
|
||||
func (val GaugeInfoValue) String() string {
|
||||
|
|
@ -49,48 +52,16 @@ func NewRegisteredGaugeInfo(name string, r Registry) GaugeInfo {
|
|||
return c
|
||||
}
|
||||
|
||||
// NewFunctionalGauge constructs a new FunctionalGauge.
|
||||
func NewFunctionalGaugeInfo(f func() GaugeInfoValue) GaugeInfo {
|
||||
if !Enabled {
|
||||
return NilGaugeInfo{}
|
||||
}
|
||||
return &FunctionalGaugeInfo{value: f}
|
||||
}
|
||||
|
||||
// NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
|
||||
func NewRegisteredFunctionalGaugeInfo(name string, r Registry, f func() GaugeInfoValue) GaugeInfo {
|
||||
c := NewFunctionalGaugeInfo(f)
|
||||
if nil == r {
|
||||
r = DefaultRegistry
|
||||
}
|
||||
r.Register(name, c)
|
||||
return c
|
||||
}
|
||||
|
||||
// GaugeInfoSnapshot is a read-only copy of another GaugeInfo.
|
||||
type GaugeInfoSnapshot GaugeInfoValue
|
||||
|
||||
// Snapshot returns the snapshot.
|
||||
func (g GaugeInfoSnapshot) Snapshot() GaugeInfo { return g }
|
||||
|
||||
// Update panics.
|
||||
func (GaugeInfoSnapshot) Update(GaugeInfoValue) {
|
||||
panic("Update called on a GaugeInfoSnapshot")
|
||||
}
|
||||
// gaugeInfoSnapshot is a read-only copy of another GaugeInfo.
|
||||
type gaugeInfoSnapshot GaugeInfoValue
|
||||
|
||||
// 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) }
|
||||
|
||||
// NilGauge is a no-op Gauge.
|
||||
type NilGaugeInfo struct{}
|
||||
|
||||
// Snapshot is a no-op.
|
||||
func (NilGaugeInfo) Snapshot() GaugeInfo { return NilGaugeInfo{} }
|
||||
|
||||
// Update is a no-op.
|
||||
func (NilGaugeInfo) Snapshot() GaugeInfoSnapshot { return NilGaugeInfo{} }
|
||||
func (NilGaugeInfo) Update(v GaugeInfoValue) {}
|
||||
|
||||
// Value is a no-op.
|
||||
func (NilGaugeInfo) Value() GaugeInfoValue { return GaugeInfoValue{} }
|
||||
|
||||
// StandardGaugeInfo is the standard implementation of a GaugeInfo and uses
|
||||
|
|
@ -101,8 +72,8 @@ type StandardGaugeInfo struct {
|
|||
}
|
||||
|
||||
// Snapshot returns a read-only copy of the gauge.
|
||||
func (g *StandardGaugeInfo) Snapshot() GaugeInfo {
|
||||
return GaugeInfoSnapshot(g.Value())
|
||||
func (g *StandardGaugeInfo) Snapshot() GaugeInfoSnapshot {
|
||||
return gaugeInfoSnapshot(g.value)
|
||||
}
|
||||
|
||||
// Update updates the gauge's value.
|
||||
|
|
@ -111,34 +82,3 @@ func (g *StandardGaugeInfo) Update(v GaugeInfoValue) {
|
|||
defer g.mutex.Unlock()
|
||||
g.value = v
|
||||
}
|
||||
|
||||
// Value returns the gauge's current value.
|
||||
func (g *StandardGaugeInfo) Value() GaugeInfoValue {
|
||||
g.mutex.Lock()
|
||||
defer g.mutex.Unlock()
|
||||
return g.value
|
||||
}
|
||||
|
||||
// FunctionalGaugeInfo returns value from given function
|
||||
type FunctionalGaugeInfo struct {
|
||||
value func() GaugeInfoValue
|
||||
}
|
||||
|
||||
// Value returns the gauge's current value.
|
||||
func (g FunctionalGaugeInfo) Value() GaugeInfoValue {
|
||||
return g.value()
|
||||
}
|
||||
|
||||
// Value returns the gauge's current value in JSON string format
|
||||
func (g FunctionalGaugeInfo) ValueJsonString() string {
|
||||
data, _ := json.Marshal(g.value())
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// Snapshot returns the snapshot.
|
||||
func (g FunctionalGaugeInfo) Snapshot() GaugeInfo { return GaugeInfoSnapshot(g.Value()) }
|
||||
|
||||
// Update panics.
|
||||
func (FunctionalGaugeInfo) Update(GaugeInfoValue) {
|
||||
panic("Update called on a FunctionalGaugeInfo")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -14,62 +13,25 @@ func TestGaugeInfoJsonString(t *testing.T) {
|
|||
},
|
||||
)
|
||||
want := `{"anotherKey":"any_string_value","chain_id":"5","third_key":"anything"}`
|
||||
if have := g.Value().String(); have != want {
|
||||
t.Errorf("\nhave: %v\nwant: %v\n", have, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGaugeInfoSnapshot(t *testing.T) {
|
||||
g := NewGaugeInfo()
|
||||
g.Update(GaugeInfoValue{"value": "original"})
|
||||
snapshot := g.Snapshot() // Snapshot @chainid 5
|
||||
original := g.Snapshot()
|
||||
g.Update(GaugeInfoValue{"value": "updated"})
|
||||
// The 'g' should be updated
|
||||
if have, want := g.Value().String(), `{"value":"updated"}`; have != want {
|
||||
|
||||
if have := original.Value().String(); have != want {
|
||||
t.Errorf("\nhave: %v\nwant: %v\n", have, want)
|
||||
}
|
||||
// Snapshot should be unupdated
|
||||
if have, want := snapshot.Value().String(), `{"value":"original"}`; have != want {
|
||||
if have, want := g.Snapshot().Value().String(), `{"value":"updated"}`; have != want {
|
||||
t.Errorf("\nhave: %v\nwant: %v\n", have, want)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetOrRegisterGaugeInfo(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
NewRegisteredGaugeInfo("foo", r).Update(
|
||||
GaugeInfoValue{"chain_id": "5"})
|
||||
g := GetOrRegisterGaugeInfo("foo", r)
|
||||
g := GetOrRegisterGaugeInfo("foo", r).Snapshot()
|
||||
if have, want := g.Value().String(), `{"chain_id":"5"}`; have != want {
|
||||
t.Errorf("have\n%v\nwant\n%v\n", have, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFunctionalGaugeInfo(t *testing.T) {
|
||||
info := GaugeInfoValue{"chain_id": "0"}
|
||||
counter := 1
|
||||
// A "functional" gauge invokes the method to obtain the value
|
||||
fg := NewFunctionalGaugeInfo(func() GaugeInfoValue {
|
||||
info["chain_id"] = strconv.Itoa(counter)
|
||||
counter++
|
||||
return info
|
||||
})
|
||||
fg.Value()
|
||||
fg.Value()
|
||||
if have, want := info["chain_id"], "2"; have != want {
|
||||
t.Errorf("have %v want %v", have, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOrRegisterFunctionalGaugeInfo(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
NewRegisteredFunctionalGaugeInfo("foo", r, func() GaugeInfoValue {
|
||||
return GaugeInfoValue{
|
||||
"chain_id": "5",
|
||||
}
|
||||
})
|
||||
want := `{"chain_id":"5"}`
|
||||
have := GetOrRegisterGaugeInfo("foo", r).Value().String()
|
||||
if have != want {
|
||||
t.Errorf("have\n%v\nwant\n%v\n", have, want)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ func graphite(c *GraphiteConfig) error {
|
|||
case GaugeFloat64:
|
||||
fmt.Fprintf(w, "%s.%s.value %f %d\n", c.Prefix, name, metric.Snapshot().Value(), now)
|
||||
case GaugeInfo:
|
||||
fmt.Fprintf(w, "%s.%s.value %s %d\n", c.Prefix, name, metric.Value().String(), now)
|
||||
fmt.Fprintf(w, "%s.%s.value %s %d\n", c.Prefix, name, metric.Snapshot().Value().String(), now)
|
||||
case Histogram:
|
||||
h := metric.Snapshot()
|
||||
ps := h.Percentiles(c.Percentiles)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func LogScaled(r Registry, freq time.Duration, scale time.Duration, l Logger) {
|
|||
l.Printf(" value: %f\n", metric.Snapshot().Value())
|
||||
case GaugeInfo:
|
||||
l.Printf("gauge %s\n", name)
|
||||
l.Printf(" value: %s\n", metric.Value())
|
||||
l.Printf(" value: %s\n", metric.Snapshot().Value())
|
||||
case Healthcheck:
|
||||
metric.Check()
|
||||
l.Printf("healthcheck %s\n", name)
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ func (c *OpenTSDBConfig) writeRegistry(w io.Writer, now int64, shortHostname str
|
|||
case GaugeFloat64:
|
||||
fmt.Fprintf(w, "put %s.%s.value %d %f host=%s\n", c.Prefix, name, now, metric.Snapshot().Value(), shortHostname)
|
||||
case GaugeInfo:
|
||||
fmt.Fprintf(w, "put %s.%s.value %d %s host=%s\n", c.Prefix, name, now, metric.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:
|
||||
h := metric.Snapshot()
|
||||
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64Snapshot)
|
|||
c.writeGaugeCounter(name, m.Value())
|
||||
}
|
||||
|
||||
func (c *collector) addGaugeInfo(name string, m metrics.GaugeInfo) {
|
||||
func (c *collector) addGaugeInfo(name string, m metrics.GaugeInfoSnapshot) {
|
||||
c.writeGaugeInfo(name, m.Value())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ func Syslog(r Registry, d time.Duration, w *syslog.Writer) {
|
|||
case GaugeFloat64:
|
||||
w.Info(fmt.Sprintf("gauge %s: value: %f", name, metric.Snapshot().Value()))
|
||||
case GaugeInfo:
|
||||
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Value()))
|
||||
w.Info(fmt.Sprintf("gauge %s: value: %s", name, metric.Snapshot().Value()))
|
||||
case Healthcheck:
|
||||
metric.Check()
|
||||
w.Info(fmt.Sprintf("healthcheck %s: error: %v", name, metric.Error()))
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ func WriteOnce(r Registry, w io.Writer) {
|
|||
fmt.Fprintf(w, " value: %f\n", metric.Snapshot().Value())
|
||||
case GaugeInfo:
|
||||
fmt.Fprintf(w, "gauge %s\n", namedMetric.name)
|
||||
fmt.Fprintf(w, " value: %s\n", metric.Value().String())
|
||||
fmt.Fprintf(w, " value: %s\n", metric.Snapshot().Value().String())
|
||||
case Healthcheck:
|
||||
metric.Check()
|
||||
fmt.Fprintf(w, "healthcheck %s\n", namedMetric.name)
|
||||
|
|
|
|||
Loading…
Reference in a new issue