metrics: test updates, remove deprecated 'graphite' reporter

This commit is contained in:
Martin Holst Swende 2024-11-27 03:20:47 +01:00
parent a6038c67e7
commit 9814769df7
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
7 changed files with 50 additions and 316 deletions

View file

@ -32,61 +32,35 @@ func BenchmarkCounterFloat64Parallel(b *testing.B) {
} }
} }
func TestCounterFloat64Clear(t *testing.T) { func TestCounterFloat64(t *testing.T) {
c := NewCounterFloat64() c := NewCounterFloat64()
c.Inc(1.0)
c.Clear()
if count := c.Snapshot().Count(); count != 0 { if count := c.Snapshot().Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
}
func TestCounterFloat64Dec1(t *testing.T) {
c := NewCounterFloat64()
c.Dec(1.0) c.Dec(1.0)
if count := c.Snapshot().Count(); count != -1.0 { if count := c.Snapshot().Count(); count != -1.0 {
t.Errorf("c.Count(): -1.0 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
}
func TestCounterFloat64Dec2(t *testing.T) {
c := NewCounterFloat64()
c.Dec(2.0)
if count := c.Snapshot().Count(); count != -2.0 {
t.Errorf("c.Count(): -2.0 != %v\n", count)
}
}
func TestCounterFloat64Inc1(t *testing.T) {
c := NewCounterFloat64()
c.Inc(1.0)
if count := c.Snapshot().Count(); count != 1.0 {
t.Errorf("c.Count(): 1.0 != %v\n", count)
}
}
func TestCounterFloat64Inc2(t *testing.T) {
c := NewCounterFloat64()
c.Inc(2.0)
if count := c.Snapshot().Count(); count != 2.0 {
t.Errorf("c.Count(): 2.0 != %v\n", count)
}
}
func TestCounterFloat64Snapshot(t *testing.T) {
c := NewCounterFloat64()
c.Inc(1.0)
snapshot := c.Snapshot() snapshot := c.Snapshot()
c.Inc(1.0) c.Dec(2.0)
if count := snapshot.Count(); count != 1.0 { if count := c.Snapshot().Count(); count != -3.0 {
t.Errorf("c.Count(): 1.0 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
} c.Inc(1.0)
if count := c.Snapshot().Count(); count != -2.0 {
func TestCounterFloat64Zero(t *testing.T) { t.Errorf("wrong count: %v", count)
c := NewCounterFloat64() }
if count := c.Snapshot().Count(); count != 0 { c.Inc(2.0)
t.Errorf("c.Count(): 0 != %v\n", count) if count := c.Snapshot().Count(); count != 0.0 {
t.Errorf("wrong count: %v", count)
}
if count := snapshot.Count(); count != -1.0 {
t.Errorf("snapshot count wrong: %v", count)
}
c.Inc(1.0)
c.Clear()
if count := c.Snapshot().Count(); count != 0.0 {
t.Errorf("wrong count: %v", count)
} }
} }

View file

@ -19,35 +19,26 @@ func TestCounterClear(t *testing.T) {
} }
} }
func TestCounterDec1(t *testing.T) { func TestCounter(t *testing.T) {
c := NewCounter() c := NewCounter()
if count := c.Snapshot().Count(); count != 0 {
t.Errorf("wrong count: %v", count)
}
c.Dec(1) c.Dec(1)
if count := c.Snapshot().Count(); count != -1 { if count := c.Snapshot().Count(); count != -1 {
t.Errorf("c.Count(): -1 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
}
func TestCounterDec2(t *testing.T) {
c := NewCounter()
c.Dec(2) c.Dec(2)
if count := c.Snapshot().Count(); count != -2 { if count := c.Snapshot().Count(); count != -3 {
t.Errorf("c.Count(): -2 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
}
func TestCounterInc1(t *testing.T) {
c := NewCounter()
c.Inc(1) c.Inc(1)
if count := c.Snapshot().Count(); count != 1 { if count := c.Snapshot().Count(); count != -2 {
t.Errorf("c.Count(): 1 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
}
func TestCounterInc2(t *testing.T) {
c := NewCounter()
c.Inc(2) c.Inc(2)
if count := c.Snapshot().Count(); count != 2 { if count := c.Snapshot().Count(); count != 0 {
t.Errorf("c.Count(): 2 != %v\n", count) t.Errorf("wrong count: %v", count)
} }
} }
@ -61,13 +52,6 @@ func TestCounterSnapshot(t *testing.T) {
} }
} }
func TestCounterZero(t *testing.T) {
c := NewCounter()
if count := c.Snapshot().Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}
func TestGetOrRegisterCounter(t *testing.T) { func TestGetOrRegisterCounter(t *testing.T) {
r := NewRegistry() r := NewRegistry()
NewRegisteredCounter("foo", r).Inc(47) NewRegisteredCounter("foo", r).Inc(47)

View file

@ -1,117 +0,0 @@
package metrics
import (
"bufio"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
)
// GraphiteConfig provides a container with configuration parameters for
// the Graphite exporter
type GraphiteConfig struct {
Addr *net.TCPAddr // Network address to connect to
Registry Registry // Registry to be exported
FlushInterval time.Duration // Flush interval
DurationUnit time.Duration // Time conversion unit for durations
Prefix string // Prefix to be prepended to metric names
Percentiles []float64 // Percentiles to export from timers and histograms
}
// Graphite is a blocking exporter function which reports metrics in r
// to a graphite server located at addr, flushing them every d duration
// and prepending metric names with prefix.
func Graphite(r Registry, d time.Duration, prefix string, addr *net.TCPAddr) {
GraphiteWithConfig(GraphiteConfig{
Addr: addr,
Registry: r,
FlushInterval: d,
DurationUnit: time.Nanosecond,
Prefix: prefix,
Percentiles: []float64{0.5, 0.75, 0.95, 0.99, 0.999},
})
}
// GraphiteWithConfig is a blocking exporter function just like Graphite,
// but it takes a GraphiteConfig instead.
func GraphiteWithConfig(c GraphiteConfig) {
log.Printf("WARNING: This go-metrics client has been DEPRECATED! It has been moved to https://github.com/cyberdelia/go-metrics-graphite and will be removed from rcrowley/go-metrics on August 12th 2015")
for range time.Tick(c.FlushInterval) {
if err := graphite(&c); nil != err {
log.Println(err)
}
}
}
// GraphiteOnce performs a single submission to Graphite, returning a
// non-nil error on failed connections. This can be used in a loop
// similar to GraphiteWithConfig for custom error handling.
func GraphiteOnce(c GraphiteConfig) error {
log.Printf("WARNING: This go-metrics client has been DEPRECATED! It has been moved to https://github.com/cyberdelia/go-metrics-graphite and will be removed from rcrowley/go-metrics on August 12th 2015")
return graphite(&c)
}
func graphite(c *GraphiteConfig) error {
now := time.Now().Unix()
du := float64(c.DurationUnit)
conn, err := net.DialTCP("tcp", nil, c.Addr)
if nil != err {
return err
}
defer conn.Close()
w := bufio.NewWriter(conn)
c.Registry.Each(func(name string, i interface{}) {
switch metric := i.(type) {
case *Counter:
fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, metric.Snapshot().Count(), now)
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)
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.Snapshot().Value().String(), now)
case Histogram:
h := metric.Snapshot()
ps := h.Percentiles(c.Percentiles)
fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, h.Count(), now)
fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, h.Min(), now)
fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, h.Max(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, h.Mean(), now)
fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, h.StdDev(), now)
for psIdx, psKey := range c.Percentiles {
key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx], now)
}
case *Meter:
m := metric.Snapshot()
fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, m.Count(), now)
fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, m.Rate1(), now)
fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, m.Rate5(), now)
fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, m.Rate15(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, m.RateMean(), now)
case *Timer:
t := metric.Snapshot()
ps := t.Percentiles(c.Percentiles)
fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, t.Count(), now)
fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, t.Min()/int64(du), now)
fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, t.Max()/int64(du), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, t.Mean()/du, now)
fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, t.StdDev()/du, now)
for psIdx, psKey := range c.Percentiles {
key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx], now)
}
fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, t.Rate1(), now)
fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, t.Rate5(), now)
fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, t.Rate15(), now)
fmt.Fprintf(w, "%s.%s.mean-rate %.2f %d\n", c.Prefix, name, t.RateMean(), now)
}
w.Flush()
})
return nil
}

View file

@ -1,22 +0,0 @@
package metrics
import (
"net"
"time"
)
func ExampleGraphite() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go Graphite(DefaultRegistry, 1*time.Second, "some.prefix", addr)
}
func ExampleGraphiteWithConfig() {
addr, _ := net.ResolveTCPAddr("net", ":2003")
go GraphiteWithConfig(GraphiteConfig{
Addr: addr,
Registry: DefaultRegistry,
FlushInterval: 1 * time.Second,
DurationUnit: time.Millisecond,
Percentiles: []float64{0.5, 0.75, 0.99, 0.999},
})
}

View file

@ -1,42 +0,0 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package metrics
// compile-time checks that interfaces are implemented.
var (
_ HistogramSnapshot = (*emptySnapshot)(nil)
)
type emptySnapshot struct{}
func (*emptySnapshot) Count() int64 { return 0 }
func (*emptySnapshot) Max() int64 { return 0 }
func (*emptySnapshot) Mean() float64 { return 0.0 }
func (*emptySnapshot) Min() int64 { return 0 }
func (*emptySnapshot) Percentile(p float64) float64 { return 0.0 }
func (*emptySnapshot) Percentiles(ps []float64) []float64 { return make([]float64, len(ps)) }
func (*emptySnapshot) Size() int { return 0 }
func (*emptySnapshot) StdDev() float64 { return 0.0 }
func (*emptySnapshot) Sum() int64 { return 0 }
func (*emptySnapshot) Values() []int64 { return []int64{} }
func (*emptySnapshot) Variance() float64 { return 0.0 }
func (*emptySnapshot) Value() int64 { return 0 }
func (*emptySnapshot) Rate() float64 { return 0.0 }
func (*emptySnapshot) Rate1() float64 { return 0.0 }
func (*emptySnapshot) Rate5() float64 { return 0.0 }
func (*emptySnapshot) Rate15() float64 { return 0.0 }
func (*emptySnapshot) RateMean() float64 { return 0.0 }

View file

@ -17,11 +17,11 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// Enabled is checked by the constructor functions for all of the // Enabled is checked by functions that are deemed 'expensive', e.g. if a
// standard metrics. If it is true, the metric returned is a stub. // meter-type does locking and/or non-trivial math operations during update.
// //
// This global kill-switch helps quantify the observer effect and makes // The Enabled-flag is expected to be set, once, during startup, but toggling off and on
// for less cluttered pprof profiles. // is not supported: YMMV.
var Enabled = false var Enabled = false
// enablerFlags is the CLI flag names to use to enable metrics collections. // enablerFlags is the CLI flag names to use to enable metrics collections.

View file

@ -7,8 +7,6 @@ import (
"time" "time"
) )
const FANOUT = 128
func TestReadRuntimeValues(t *testing.T) { func TestReadRuntimeValues(t *testing.T) {
var v runtimeStats var v runtimeStats
readRuntimeStats(&v) readRuntimeStats(&v)
@ -16,60 +14,23 @@ func TestReadRuntimeValues(t *testing.T) {
} }
func BenchmarkMetrics(b *testing.B) { func BenchmarkMetrics(b *testing.B) {
r := NewRegistry() var (
c := NewRegisteredCounter("counter", r) r = NewRegistry()
cf := NewRegisteredCounterFloat64("counterfloat64", r) c = NewRegisteredCounter("counter", r)
g := NewRegisteredGauge("gauge", r) cf = NewRegisteredCounterFloat64("counterfloat64", r)
gf := NewRegisteredGaugeFloat64("gaugefloat64", r) g = NewRegisteredGauge("gauge", r)
h := NewRegisteredHistogram("histogram", r, NewUniformSample(100)) gf = NewRegisteredGaugeFloat64("gaugefloat64", r)
m := NewRegisteredMeter("meter", r) h = NewRegisteredHistogram("histogram", r, NewUniformSample(100))
t := NewRegisteredTimer("timer", r) m = NewRegisteredMeter("meter", r)
t = NewRegisteredTimer("timer", r)
)
RegisterDebugGCStats(r) RegisterDebugGCStats(r)
b.ResetTimer() b.ResetTimer()
ch := make(chan bool) var wg sync.WaitGroup
wg.Add(128)
wgD := &sync.WaitGroup{} for i := 0; i < 128; i++ {
/*
wgD.Add(1)
go func() { go func() {
defer wgD.Done()
//log.Println("go CaptureDebugGCStats")
for {
select {
case <-ch:
//log.Println("done CaptureDebugGCStats")
return
default:
CaptureDebugGCStatsOnce(r)
}
}
}()
//*/
wgW := &sync.WaitGroup{}
/*
wgW.Add(1)
go func() {
defer wgW.Done()
//log.Println("go Write")
for {
select {
case <-ch:
//log.Println("done Write")
return
default:
WriteOnce(r, io.Discard)
}
}
}()
//*/
wg := &sync.WaitGroup{}
wg.Add(FANOUT)
for i := 0; i < FANOUT; i++ {
go func(i int) {
defer wg.Done() defer wg.Done()
//log.Println("go", i)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
c.Inc(1) c.Inc(1)
cf.Inc(1.0) cf.Inc(1.0)
@ -79,13 +40,9 @@ func BenchmarkMetrics(b *testing.B) {
m.Mark(1) m.Mark(1)
t.Update(1) t.Update(1)
} }
//log.Println("done", i) }()
}(i)
} }
wg.Wait() wg.Wait()
close(ch)
wgD.Wait()
wgW.Wait()
} }
func Example() { func Example() {