Vendor update with github.com/ugorji/go and github.com/apache/thrift pinning (#1805)
This fix is an vendor update. Both ugorji and thrift have to be pinned to compile. The ugorji is from etcd and thrift is from zipkin. This fix fixes #1802. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
1e471a353e
commit
05a030e17b
10682 changed files with 37458 additions and 4048068 deletions
1
vendor/github.com/rcrowley/go-metrics/.travis.yml
generated
vendored
1
vendor/github.com/rcrowley/go-metrics/.travis.yml
generated
vendored
|
@ -1,7 +1,6 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- 1.2
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
|
|
1
vendor/github.com/rcrowley/go-metrics/README.md
generated
vendored
1
vendor/github.com/rcrowley/go-metrics/README.md
generated
vendored
|
@ -165,3 +165,4 @@ Clients are available for the following destinations:
|
|||
* DataDog - https://github.com/syntaqx/go-metrics-datadog
|
||||
* SignalFX - https://github.com/pascallouisperez/go-metrics-signalfx
|
||||
* Honeycomb - https://github.com/getspine/go-metrics-honeycomb
|
||||
* Wavefront - https://github.com/wavefrontHQ/go-metrics-wavefront
|
||||
|
|
20
vendor/github.com/rcrowley/go-metrics/cmd/metrics-bench/metrics-bench.go
generated
vendored
20
vendor/github.com/rcrowley/go-metrics/cmd/metrics-bench/metrics-bench.go
generated
vendored
|
@ -1,20 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rcrowley/go-metrics"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := metrics.NewRegistry()
|
||||
for i := 0; i < 10000; i++ {
|
||||
r.Register(fmt.Sprintf("counter-%d", i), metrics.NewCounter())
|
||||
r.Register(fmt.Sprintf("gauge-%d", i), metrics.NewGauge())
|
||||
r.Register(fmt.Sprintf("gaugefloat64-%d", i), metrics.NewGaugeFloat64())
|
||||
r.Register(fmt.Sprintf("histogram-uniform-%d", i), metrics.NewHistogram(metrics.NewUniformSample(1028)))
|
||||
r.Register(fmt.Sprintf("histogram-exp-%d", i), metrics.NewHistogram(metrics.NewExpDecaySample(1028, 0.015)))
|
||||
r.Register(fmt.Sprintf("meter-%d", i), metrics.NewMeter())
|
||||
}
|
||||
time.Sleep(600e9)
|
||||
}
|
154
vendor/github.com/rcrowley/go-metrics/cmd/metrics-example/metrics-example.go
generated
vendored
154
vendor/github.com/rcrowley/go-metrics/cmd/metrics-example/metrics-example.go
generated
vendored
|
@ -1,154 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/rcrowley/go-metrics"
|
||||
// "github.com/rcrowley/go-metrics/stathat"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
// "syslog"
|
||||
"time"
|
||||
)
|
||||
|
||||
const fanout = 10
|
||||
|
||||
func main() {
|
||||
|
||||
r := metrics.NewRegistry()
|
||||
|
||||
c := metrics.NewCounter()
|
||||
r.Register("foo", c)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
c.Dec(19)
|
||||
time.Sleep(300e6)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
c.Inc(47)
|
||||
time.Sleep(400e6)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
g := metrics.NewGauge()
|
||||
r.Register("bar", g)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
g.Update(19)
|
||||
time.Sleep(300e6)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
g.Update(47)
|
||||
time.Sleep(400e6)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
gf := metrics.NewGaugeFloat64()
|
||||
r.Register("barfloat64", gf)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
g.Update(19.0)
|
||||
time.Sleep(300e6)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
g.Update(47.0)
|
||||
time.Sleep(400e6)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
hc := metrics.NewHealthcheck(func(h metrics.Healthcheck) {
|
||||
if 0 < rand.Intn(2) {
|
||||
h.Healthy()
|
||||
} else {
|
||||
h.Unhealthy(errors.New("baz"))
|
||||
}
|
||||
})
|
||||
r.Register("baz", hc)
|
||||
|
||||
s := metrics.NewExpDecaySample(1028, 0.015)
|
||||
//s := metrics.NewUniformSample(1028)
|
||||
h := metrics.NewHistogram(s)
|
||||
r.Register("bang", h)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
h.Update(19)
|
||||
time.Sleep(300e6)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
h.Update(47)
|
||||
time.Sleep(400e6)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
m := metrics.NewMeter()
|
||||
r.Register("quux", m)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
m.Mark(19)
|
||||
time.Sleep(300e6)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
m.Mark(47)
|
||||
time.Sleep(400e6)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
t := metrics.NewTimer()
|
||||
r.Register("hooah", t)
|
||||
for i := 0; i < fanout; i++ {
|
||||
go func() {
|
||||
for {
|
||||
t.Time(func() { time.Sleep(300e6) })
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
for {
|
||||
t.Time(func() { time.Sleep(400e6) })
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
metrics.RegisterDebugGCStats(r)
|
||||
go metrics.CaptureDebugGCStats(r, 5e9)
|
||||
|
||||
metrics.RegisterRuntimeMemStats(r)
|
||||
go metrics.CaptureRuntimeMemStats(r, 5e9)
|
||||
|
||||
metrics.Log(r, 60e9, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
|
||||
|
||||
/*
|
||||
w, err := syslog.Dial("unixgram", "/dev/log", syslog.LOG_INFO, "metrics")
|
||||
if nil != err { log.Fatalln(err) }
|
||||
metrics.Syslog(r, 60e9, w)
|
||||
*/
|
||||
|
||||
/*
|
||||
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003")
|
||||
metrics.Graphite(r, 10e9, "metrics", addr)
|
||||
*/
|
||||
|
||||
/*
|
||||
stathat.Stathat(r, 10e9, "example@example.com")
|
||||
*/
|
||||
|
||||
}
|
22
vendor/github.com/rcrowley/go-metrics/cmd/never-read/never-read.go
generated
vendored
22
vendor/github.com/rcrowley/go-metrics/cmd/never-read/never-read.go
generated
vendored
|
@ -1,22 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:2003")
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if nil != err {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Println("listening", l.Addr())
|
||||
for {
|
||||
c, err := l.AcceptTCP()
|
||||
if nil != err {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
log.Println("accepted", c.RemoteAddr())
|
||||
}
|
||||
}
|
46
vendor/github.com/rcrowley/go-metrics/ewma.go
generated
vendored
46
vendor/github.com/rcrowley/go-metrics/ewma.go
generated
vendored
|
@ -79,16 +79,15 @@ func (NilEWMA) Update(n int64) {}
|
|||
type StandardEWMA struct {
|
||||
uncounted int64 // /!\ this should be the first member to ensure 64-bit alignment
|
||||
alpha float64
|
||||
rate float64
|
||||
init bool
|
||||
rate uint64
|
||||
init uint32
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
// Rate returns the moving average rate of events per second.
|
||||
func (a *StandardEWMA) Rate() float64 {
|
||||
a.mutex.Lock()
|
||||
defer a.mutex.Unlock()
|
||||
return a.rate * float64(1e9)
|
||||
currentRate := math.Float64frombits(atomic.LoadUint64(&a.rate)) * float64(1e9)
|
||||
return currentRate
|
||||
}
|
||||
|
||||
// Snapshot returns a read-only copy of the EWMA.
|
||||
|
@ -99,17 +98,38 @@ func (a *StandardEWMA) Snapshot() EWMA {
|
|||
// Tick ticks the clock to update the moving average. It assumes it is called
|
||||
// every five seconds.
|
||||
func (a *StandardEWMA) Tick() {
|
||||
// Optimization to avoid mutex locking in the hot-path.
|
||||
if atomic.LoadUint32(&a.init) == 1 {
|
||||
a.updateRate(a.fetchInstantRate())
|
||||
} else {
|
||||
// Slow-path: this is only needed on the first Tick() and preserves transactional updating
|
||||
// of init and rate in the else block. The first conditional is needed below because
|
||||
// a different thread could have set a.init = 1 between the time of the first atomic load and when
|
||||
// the lock was acquired.
|
||||
a.mutex.Lock()
|
||||
if atomic.LoadUint32(&a.init) == 1 {
|
||||
// The fetchInstantRate() uses atomic loading, which is unecessary in this critical section
|
||||
// but again, this section is only invoked on the first successful Tick() operation.
|
||||
a.updateRate(a.fetchInstantRate())
|
||||
} else {
|
||||
atomic.StoreUint32(&a.init, 1)
|
||||
atomic.StoreUint64(&a.rate, math.Float64bits(a.fetchInstantRate()))
|
||||
}
|
||||
a.mutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *StandardEWMA) fetchInstantRate() float64 {
|
||||
count := atomic.LoadInt64(&a.uncounted)
|
||||
atomic.AddInt64(&a.uncounted, -count)
|
||||
instantRate := float64(count) / float64(5e9)
|
||||
a.mutex.Lock()
|
||||
defer a.mutex.Unlock()
|
||||
if a.init {
|
||||
a.rate += a.alpha * (instantRate - a.rate)
|
||||
} else {
|
||||
a.init = true
|
||||
a.rate = instantRate
|
||||
}
|
||||
return instantRate
|
||||
}
|
||||
|
||||
func (a *StandardEWMA) updateRate(instantRate float64) {
|
||||
currentRate := math.Float64frombits(atomic.LoadUint64(&a.rate))
|
||||
currentRate += a.alpha * (instantRate - currentRate)
|
||||
atomic.StoreUint64(&a.rate, math.Float64bits(currentRate))
|
||||
}
|
||||
|
||||
// Update adds n uncounted events.
|
||||
|
|
35
vendor/github.com/rcrowley/go-metrics/ewma_test.go
generated
vendored
35
vendor/github.com/rcrowley/go-metrics/ewma_test.go
generated
vendored
|
@ -1,6 +1,11 @@
|
|||
package metrics
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func BenchmarkEWMA(b *testing.B) {
|
||||
a := NewEWMA1()
|
||||
|
@ -11,6 +16,34 @@ func BenchmarkEWMA(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkEWMAParallel(b *testing.B) {
|
||||
a := NewEWMA1()
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
a.Update(1)
|
||||
a.Tick()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// exercise race detector
|
||||
func TestEWMAConcurrency(t *testing.T) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
a := NewEWMA1()
|
||||
wg := &sync.WaitGroup{}
|
||||
reps := 100
|
||||
for i := 0; i < reps; i++ {
|
||||
wg.Add(1)
|
||||
go func(ewma EWMA, wg *sync.WaitGroup) {
|
||||
a.Update(rand.Int63())
|
||||
wg.Done()
|
||||
}(a, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestEWMA1(t *testing.T) {
|
||||
a := NewEWMA1()
|
||||
a.Update(3)
|
||||
|
|
156
vendor/github.com/rcrowley/go-metrics/exp/exp.go
generated
vendored
156
vendor/github.com/rcrowley/go-metrics/exp/exp.go
generated
vendored
|
@ -1,156 +0,0 @@
|
|||
// Hook go-metrics into expvar
|
||||
// on any /debug/metrics request, load all vars from the registry into expvar, and execute regular expvar handler
|
||||
package exp
|
||||
|
||||
import (
|
||||
"expvar"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/rcrowley/go-metrics"
|
||||
)
|
||||
|
||||
type exp struct {
|
||||
expvarLock sync.Mutex // expvar panics if you try to register the same var twice, so we must probe it safely
|
||||
registry metrics.Registry
|
||||
}
|
||||
|
||||
func (exp *exp) expHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// load our variables into expvar
|
||||
exp.syncToExpvar()
|
||||
|
||||
// now just run the official expvar handler code (which is not publicly callable, so pasted inline)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprintf(w, "{\n")
|
||||
first := true
|
||||
expvar.Do(func(kv expvar.KeyValue) {
|
||||
if !first {
|
||||
fmt.Fprintf(w, ",\n")
|
||||
}
|
||||
first = false
|
||||
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
|
||||
})
|
||||
fmt.Fprintf(w, "\n}\n")
|
||||
}
|
||||
|
||||
// Exp will register an expvar powered metrics handler with http.DefaultServeMux on "/debug/vars"
|
||||
func Exp(r metrics.Registry) {
|
||||
h := ExpHandler(r)
|
||||
// this would cause a panic:
|
||||
// panic: http: multiple registrations for /debug/vars
|
||||
// http.HandleFunc("/debug/vars", e.expHandler)
|
||||
// haven't found an elegant way, so just use a different endpoint
|
||||
http.Handle("/debug/metrics", h)
|
||||
}
|
||||
|
||||
// ExpHandler will return an expvar powered metrics handler.
|
||||
func ExpHandler(r metrics.Registry) http.Handler {
|
||||
e := exp{sync.Mutex{}, r}
|
||||
return http.HandlerFunc(e.expHandler)
|
||||
}
|
||||
|
||||
func (exp *exp) getInt(name string) *expvar.Int {
|
||||
var v *expvar.Int
|
||||
exp.expvarLock.Lock()
|
||||
p := expvar.Get(name)
|
||||
if p != nil {
|
||||
v = p.(*expvar.Int)
|
||||
} else {
|
||||
v = new(expvar.Int)
|
||||
expvar.Publish(name, v)
|
||||
}
|
||||
exp.expvarLock.Unlock()
|
||||
return v
|
||||
}
|
||||
|
||||
func (exp *exp) getFloat(name string) *expvar.Float {
|
||||
var v *expvar.Float
|
||||
exp.expvarLock.Lock()
|
||||
p := expvar.Get(name)
|
||||
if p != nil {
|
||||
v = p.(*expvar.Float)
|
||||
} else {
|
||||
v = new(expvar.Float)
|
||||
expvar.Publish(name, v)
|
||||
}
|
||||
exp.expvarLock.Unlock()
|
||||
return v
|
||||
}
|
||||
|
||||
func (exp *exp) publishCounter(name string, metric metrics.Counter) {
|
||||
v := exp.getInt(name)
|
||||
v.Set(metric.Count())
|
||||
}
|
||||
|
||||
func (exp *exp) publishGauge(name string, metric metrics.Gauge) {
|
||||
v := exp.getInt(name)
|
||||
v.Set(metric.Value())
|
||||
}
|
||||
func (exp *exp) publishGaugeFloat64(name string, metric metrics.GaugeFloat64) {
|
||||
exp.getFloat(name).Set(metric.Value())
|
||||
}
|
||||
|
||||
func (exp *exp) publishHistogram(name string, metric metrics.Histogram) {
|
||||
h := metric.Snapshot()
|
||||
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
||||
exp.getInt(name + ".count").Set(h.Count())
|
||||
exp.getFloat(name + ".min").Set(float64(h.Min()))
|
||||
exp.getFloat(name + ".max").Set(float64(h.Max()))
|
||||
exp.getFloat(name + ".mean").Set(float64(h.Mean()))
|
||||
exp.getFloat(name + ".std-dev").Set(float64(h.StdDev()))
|
||||
exp.getFloat(name + ".50-percentile").Set(float64(ps[0]))
|
||||
exp.getFloat(name + ".75-percentile").Set(float64(ps[1]))
|
||||
exp.getFloat(name + ".95-percentile").Set(float64(ps[2]))
|
||||
exp.getFloat(name + ".99-percentile").Set(float64(ps[3]))
|
||||
exp.getFloat(name + ".999-percentile").Set(float64(ps[4]))
|
||||
}
|
||||
|
||||
func (exp *exp) publishMeter(name string, metric metrics.Meter) {
|
||||
m := metric.Snapshot()
|
||||
exp.getInt(name + ".count").Set(m.Count())
|
||||
exp.getFloat(name + ".one-minute").Set(float64(m.Rate1()))
|
||||
exp.getFloat(name + ".five-minute").Set(float64(m.Rate5()))
|
||||
exp.getFloat(name + ".fifteen-minute").Set(float64((m.Rate15())))
|
||||
exp.getFloat(name + ".mean").Set(float64(m.RateMean()))
|
||||
}
|
||||
|
||||
func (exp *exp) publishTimer(name string, metric metrics.Timer) {
|
||||
t := metric.Snapshot()
|
||||
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
||||
exp.getInt(name + ".count").Set(t.Count())
|
||||
exp.getFloat(name + ".min").Set(float64(t.Min()))
|
||||
exp.getFloat(name + ".max").Set(float64(t.Max()))
|
||||
exp.getFloat(name + ".mean").Set(float64(t.Mean()))
|
||||
exp.getFloat(name + ".std-dev").Set(float64(t.StdDev()))
|
||||
exp.getFloat(name + ".50-percentile").Set(float64(ps[0]))
|
||||
exp.getFloat(name + ".75-percentile").Set(float64(ps[1]))
|
||||
exp.getFloat(name + ".95-percentile").Set(float64(ps[2]))
|
||||
exp.getFloat(name + ".99-percentile").Set(float64(ps[3]))
|
||||
exp.getFloat(name + ".999-percentile").Set(float64(ps[4]))
|
||||
exp.getFloat(name + ".one-minute").Set(float64(t.Rate1()))
|
||||
exp.getFloat(name + ".five-minute").Set(float64(t.Rate5()))
|
||||
exp.getFloat(name + ".fifteen-minute").Set(float64((t.Rate15())))
|
||||
exp.getFloat(name + ".mean-rate").Set(float64(t.RateMean()))
|
||||
}
|
||||
|
||||
func (exp *exp) syncToExpvar() {
|
||||
exp.registry.Each(func(name string, i interface{}) {
|
||||
switch i.(type) {
|
||||
case metrics.Counter:
|
||||
exp.publishCounter(name, i.(metrics.Counter))
|
||||
case metrics.Gauge:
|
||||
exp.publishGauge(name, i.(metrics.Gauge))
|
||||
case metrics.GaugeFloat64:
|
||||
exp.publishGaugeFloat64(name, i.(metrics.GaugeFloat64))
|
||||
case metrics.Histogram:
|
||||
exp.publishHistogram(name, i.(metrics.Histogram))
|
||||
case metrics.Meter:
|
||||
exp.publishMeter(name, i.(metrics.Meter))
|
||||
case metrics.Timer:
|
||||
exp.publishTimer(name, i.(metrics.Timer))
|
||||
default:
|
||||
panic(fmt.Sprintf("unsupported type for '%s': %T", name, i))
|
||||
}
|
||||
})
|
||||
}
|
16
vendor/github.com/rcrowley/go-metrics/gauge_float64.go
generated
vendored
16
vendor/github.com/rcrowley/go-metrics/gauge_float64.go
generated
vendored
|
@ -1,6 +1,9 @@
|
|||
package metrics
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"math"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// GaugeFloat64s hold a float64 value that can be set arbitrarily.
|
||||
type GaugeFloat64 interface {
|
||||
|
@ -85,8 +88,7 @@ func (NilGaugeFloat64) Value() float64 { return 0.0 }
|
|||
// StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
|
||||
// sync.Mutex to manage a single float64 value.
|
||||
type StandardGaugeFloat64 struct {
|
||||
mutex sync.Mutex
|
||||
value float64
|
||||
value uint64
|
||||
}
|
||||
|
||||
// Snapshot returns a read-only copy of the gauge.
|
||||
|
@ -96,16 +98,12 @@ func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {
|
|||
|
||||
// Update updates the gauge's value.
|
||||
func (g *StandardGaugeFloat64) Update(v float64) {
|
||||
g.mutex.Lock()
|
||||
defer g.mutex.Unlock()
|
||||
g.value = v
|
||||
atomic.StoreUint64(&g.value, math.Float64bits(v))
|
||||
}
|
||||
|
||||
// Value returns the gauge's current value.
|
||||
func (g *StandardGaugeFloat64) Value() float64 {
|
||||
g.mutex.Lock()
|
||||
defer g.mutex.Unlock()
|
||||
return g.value
|
||||
return math.Float64frombits(atomic.LoadUint64(&g.value))
|
||||
}
|
||||
|
||||
// FunctionalGaugeFloat64 returns value from given function
|
||||
|
|
10
vendor/github.com/rcrowley/go-metrics/gauge_float64_test.go
generated
vendored
10
vendor/github.com/rcrowley/go-metrics/gauge_float64_test.go
generated
vendored
|
@ -10,6 +10,16 @@ func BenchmarkGuageFloat64(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkGuageFloat64Parallel(b *testing.B) {
|
||||
g := NewGaugeFloat64()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
g.Update(float64(1))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGaugeFloat64(t *testing.T) {
|
||||
g := NewGaugeFloat64()
|
||||
g.Update(float64(47.0))
|
||||
|
|
19
vendor/github.com/rcrowley/go-metrics/gauge_test.go
generated
vendored
19
vendor/github.com/rcrowley/go-metrics/gauge_test.go
generated
vendored
|
@ -2,7 +2,10 @@ package metrics
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func BenchmarkGuage(b *testing.B) {
|
||||
|
@ -13,6 +16,22 @@ func BenchmarkGuage(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
// exercise race detector
|
||||
func TestGaugeConcurrency(t *testing.T) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
g := NewGauge()
|
||||
wg := &sync.WaitGroup{}
|
||||
reps := 100
|
||||
for i := 0; i < reps; i++ {
|
||||
wg.Add(1)
|
||||
go func(g Gauge, wg *sync.WaitGroup) {
|
||||
g.Update(rand.Int63())
|
||||
wg.Done()
|
||||
}(g, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestGauge(t *testing.T) {
|
||||
g := NewGauge()
|
||||
g.Update(int64(47))
|
||||
|
|
102
vendor/github.com/rcrowley/go-metrics/librato/client.go
generated
vendored
102
vendor/github.com/rcrowley/go-metrics/librato/client.go
generated
vendored
|
@ -1,102 +0,0 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const Operations = "operations"
|
||||
const OperationsShort = "ops"
|
||||
|
||||
type LibratoClient struct {
|
||||
Email, Token string
|
||||
}
|
||||
|
||||
// property strings
|
||||
const (
|
||||
// display attributes
|
||||
Color = "color"
|
||||
DisplayMax = "display_max"
|
||||
DisplayMin = "display_min"
|
||||
DisplayUnitsLong = "display_units_long"
|
||||
DisplayUnitsShort = "display_units_short"
|
||||
DisplayStacked = "display_stacked"
|
||||
DisplayTransform = "display_transform"
|
||||
// special gauge display attributes
|
||||
SummarizeFunction = "summarize_function"
|
||||
Aggregate = "aggregate"
|
||||
|
||||
// metric keys
|
||||
Name = "name"
|
||||
Period = "period"
|
||||
Description = "description"
|
||||
DisplayName = "display_name"
|
||||
Attributes = "attributes"
|
||||
|
||||
// measurement keys
|
||||
MeasureTime = "measure_time"
|
||||
Source = "source"
|
||||
Value = "value"
|
||||
|
||||
// special gauge keys
|
||||
Count = "count"
|
||||
Sum = "sum"
|
||||
Max = "max"
|
||||
Min = "min"
|
||||
SumSquares = "sum_squares"
|
||||
|
||||
// batch keys
|
||||
Counters = "counters"
|
||||
Gauges = "gauges"
|
||||
|
||||
MetricsPostUrl = "https://metrics-api.librato.com/v1/metrics"
|
||||
)
|
||||
|
||||
type Measurement map[string]interface{}
|
||||
type Metric map[string]interface{}
|
||||
|
||||
type Batch struct {
|
||||
Gauges []Measurement `json:"gauges,omitempty"`
|
||||
Counters []Measurement `json:"counters,omitempty"`
|
||||
MeasureTime int64 `json:"measure_time"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
func (self *LibratoClient) PostMetrics(batch Batch) (err error) {
|
||||
var (
|
||||
js []byte
|
||||
req *http.Request
|
||||
resp *http.Response
|
||||
)
|
||||
|
||||
if len(batch.Counters) == 0 && len(batch.Gauges) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if js, err = json.Marshal(batch); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.SetBasicAuth(self.Email, self.Token)
|
||||
|
||||
if resp, err = http.DefaultClient.Do(req); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var body []byte
|
||||
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
|
||||
}
|
||||
err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
|
||||
}
|
||||
return
|
||||
}
|
235
vendor/github.com/rcrowley/go-metrics/librato/librato.go
generated
vendored
235
vendor/github.com/rcrowley/go-metrics/librato/librato.go
generated
vendored
|
@ -1,235 +0,0 @@
|
|||
package librato
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/rcrowley/go-metrics"
|
||||
)
|
||||
|
||||
// a regexp for extracting the unit from time.Duration.String
|
||||
var unitRegexp = regexp.MustCompile("[^\\d]+$")
|
||||
|
||||
// a helper that turns a time.Duration into librato display attributes for timer metrics
|
||||
func translateTimerAttributes(d time.Duration) (attrs map[string]interface{}) {
|
||||
attrs = make(map[string]interface{})
|
||||
attrs[DisplayTransform] = fmt.Sprintf("x/%d", int64(d))
|
||||
attrs[DisplayUnitsShort] = string(unitRegexp.Find([]byte(d.String())))
|
||||
return
|
||||
}
|
||||
|
||||
type Reporter struct {
|
||||
Email, Token string
|
||||
Namespace string
|
||||
Source string
|
||||
Interval time.Duration
|
||||
Registry metrics.Registry
|
||||
Percentiles []float64 // percentiles to report on histogram metrics
|
||||
TimerAttributes map[string]interface{} // units in which timers will be displayed
|
||||
intervalSec int64
|
||||
}
|
||||
|
||||
func NewReporter(r metrics.Registry, d time.Duration, e string, t string, s string, p []float64, u time.Duration) *Reporter {
|
||||
return &Reporter{e, t, "", s, d, r, p, translateTimerAttributes(u), int64(d / time.Second)}
|
||||
}
|
||||
|
||||
func Librato(r metrics.Registry, d time.Duration, e string, t string, s string, p []float64, u time.Duration) {
|
||||
NewReporter(r, d, e, t, s, p, u).Run()
|
||||
}
|
||||
|
||||
func (self *Reporter) Run() {
|
||||
log.Printf("WARNING: This client has been DEPRECATED! It has been moved to https://github.com/mihasya/go-metrics-librato and will be removed from rcrowley/go-metrics on August 5th 2015")
|
||||
ticker := time.Tick(self.Interval)
|
||||
metricsApi := &LibratoClient{self.Email, self.Token}
|
||||
for now := range ticker {
|
||||
var metrics Batch
|
||||
var err error
|
||||
if metrics, err = self.BuildRequest(now, self.Registry); err != nil {
|
||||
log.Printf("ERROR constructing librato request body %s", err)
|
||||
continue
|
||||
}
|
||||
if err := metricsApi.PostMetrics(metrics); err != nil {
|
||||
log.Printf("ERROR sending metrics to librato %s", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// calculate sum of squares from data provided by metrics.Histogram
|
||||
// see http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
|
||||
func sumSquares(s metrics.Sample) float64 {
|
||||
count := float64(s.Count())
|
||||
sumSquared := math.Pow(count*s.Mean(), 2)
|
||||
sumSquares := math.Pow(count*s.StdDev(), 2) + sumSquared/count
|
||||
if math.IsNaN(sumSquares) {
|
||||
return 0.0
|
||||
}
|
||||
return sumSquares
|
||||
}
|
||||
func sumSquaresTimer(t metrics.Timer) float64 {
|
||||
count := float64(t.Count())
|
||||
sumSquared := math.Pow(count*t.Mean(), 2)
|
||||
sumSquares := math.Pow(count*t.StdDev(), 2) + sumSquared/count
|
||||
if math.IsNaN(sumSquares) {
|
||||
return 0.0
|
||||
}
|
||||
return sumSquares
|
||||
}
|
||||
|
||||
func (self *Reporter) BuildRequest(now time.Time, r metrics.Registry) (snapshot Batch, err error) {
|
||||
snapshot = Batch{
|
||||
// coerce timestamps to a stepping fn so that they line up in Librato graphs
|
||||
MeasureTime: (now.Unix() / self.intervalSec) * self.intervalSec,
|
||||
Source: self.Source,
|
||||
}
|
||||
snapshot.Gauges = make([]Measurement, 0)
|
||||
snapshot.Counters = make([]Measurement, 0)
|
||||
histogramGaugeCount := 1 + len(self.Percentiles)
|
||||
r.Each(func(name string, metric interface{}) {
|
||||
if self.Namespace != "" {
|
||||
name = fmt.Sprintf("%s.%s", self.Namespace, name)
|
||||
}
|
||||
measurement := Measurement{}
|
||||
measurement[Period] = self.Interval.Seconds()
|
||||
switch m := metric.(type) {
|
||||
case metrics.Counter:
|
||||
if m.Count() > 0 {
|
||||
measurement[Name] = fmt.Sprintf("%s.%s", name, "count")
|
||||
measurement[Value] = float64(m.Count())
|
||||
measurement[Attributes] = map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
}
|
||||
snapshot.Counters = append(snapshot.Counters, measurement)
|
||||
}
|
||||
case metrics.Gauge:
|
||||
measurement[Name] = name
|
||||
measurement[Value] = float64(m.Value())
|
||||
snapshot.Gauges = append(snapshot.Gauges, measurement)
|
||||
case metrics.GaugeFloat64:
|
||||
measurement[Name] = name
|
||||
measurement[Value] = float64(m.Value())
|
||||
snapshot.Gauges = append(snapshot.Gauges, measurement)
|
||||
case metrics.Histogram:
|
||||
if m.Count() > 0 {
|
||||
gauges := make([]Measurement, histogramGaugeCount, histogramGaugeCount)
|
||||
s := m.Sample()
|
||||
measurement[Name] = fmt.Sprintf("%s.%s", name, "hist")
|
||||
measurement[Count] = uint64(s.Count())
|
||||
measurement[Max] = float64(s.Max())
|
||||
measurement[Min] = float64(s.Min())
|
||||
measurement[Sum] = float64(s.Sum())
|
||||
measurement[SumSquares] = sumSquares(s)
|
||||
gauges[0] = measurement
|
||||
for i, p := range self.Percentiles {
|
||||
gauges[i+1] = Measurement{
|
||||
Name: fmt.Sprintf("%s.%.2f", measurement[Name], p),
|
||||
Value: s.Percentile(p),
|
||||
Period: measurement[Period],
|
||||
}
|
||||
}
|
||||
snapshot.Gauges = append(snapshot.Gauges, gauges...)
|
||||
}
|
||||
case metrics.Meter:
|
||||
measurement[Name] = name
|
||||
measurement[Value] = float64(m.Count())
|
||||
snapshot.Counters = append(snapshot.Counters, measurement)
|
||||
snapshot.Gauges = append(snapshot.Gauges,
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "1min"),
|
||||
Value: m.Rate1(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "5min"),
|
||||
Value: m.Rate5(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "15min"),
|
||||
Value: m.Rate15(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
)
|
||||
case metrics.Timer:
|
||||
measurement[Name] = name
|
||||
measurement[Value] = float64(m.Count())
|
||||
snapshot.Counters = append(snapshot.Counters, measurement)
|
||||
if m.Count() > 0 {
|
||||
libratoName := fmt.Sprintf("%s.%s", name, "timer.mean")
|
||||
gauges := make([]Measurement, histogramGaugeCount, histogramGaugeCount)
|
||||
gauges[0] = Measurement{
|
||||
Name: libratoName,
|
||||
Count: uint64(m.Count()),
|
||||
Sum: m.Mean() * float64(m.Count()),
|
||||
Max: float64(m.Max()),
|
||||
Min: float64(m.Min()),
|
||||
SumSquares: sumSquaresTimer(m),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: self.TimerAttributes,
|
||||
}
|
||||
for i, p := range self.Percentiles {
|
||||
gauges[i+1] = Measurement{
|
||||
Name: fmt.Sprintf("%s.timer.%2.0f", name, p*100),
|
||||
Value: m.Percentile(p),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: self.TimerAttributes,
|
||||
}
|
||||
}
|
||||
snapshot.Gauges = append(snapshot.Gauges, gauges...)
|
||||
snapshot.Gauges = append(snapshot.Gauges,
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "rate.1min"),
|
||||
Value: m.Rate1(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "rate.5min"),
|
||||
Value: m.Rate5(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
Measurement{
|
||||
Name: fmt.Sprintf("%s.%s", name, "rate.15min"),
|
||||
Value: m.Rate15(),
|
||||
Period: int64(self.Interval.Seconds()),
|
||||
Attributes: map[string]interface{}{
|
||||
DisplayUnitsLong: Operations,
|
||||
DisplayUnitsShort: OperationsShort,
|
||||
DisplayMin: "0",
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
83
vendor/github.com/rcrowley/go-metrics/meter.go
generated
vendored
83
vendor/github.com/rcrowley/go-metrics/meter.go
generated
vendored
|
@ -1,7 +1,9 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -62,7 +64,7 @@ func NewRegisteredMeter(name string, r Registry) Meter {
|
|||
// MeterSnapshot is a read-only copy of another Meter.
|
||||
type MeterSnapshot struct {
|
||||
count int64
|
||||
rate1, rate5, rate15, rateMean float64
|
||||
rate1, rate5, rate15, rateMean uint64
|
||||
}
|
||||
|
||||
// Count returns the count of events at the time the snapshot was taken.
|
||||
|
@ -75,19 +77,19 @@ func (*MeterSnapshot) Mark(n int64) {
|
|||
|
||||
// Rate1 returns the one-minute moving average rate of events per second at the
|
||||
// time the snapshot was taken.
|
||||
func (m *MeterSnapshot) Rate1() float64 { return m.rate1 }
|
||||
func (m *MeterSnapshot) Rate1() float64 { return math.Float64frombits(m.rate1) }
|
||||
|
||||
// Rate5 returns the five-minute moving average rate of events per second at
|
||||
// the time the snapshot was taken.
|
||||
func (m *MeterSnapshot) Rate5() float64 { return m.rate5 }
|
||||
func (m *MeterSnapshot) Rate5() float64 { return math.Float64frombits(m.rate5) }
|
||||
|
||||
// Rate15 returns the fifteen-minute moving average rate of events per second
|
||||
// at the time the snapshot was taken.
|
||||
func (m *MeterSnapshot) Rate15() float64 { return m.rate15 }
|
||||
func (m *MeterSnapshot) Rate15() float64 { return math.Float64frombits(m.rate15) }
|
||||
|
||||
// RateMean returns the meter's mean rate of events per second at the time the
|
||||
// snapshot was taken.
|
||||
func (m *MeterSnapshot) RateMean() float64 { return m.rateMean }
|
||||
func (m *MeterSnapshot) RateMean() float64 { return math.Float64frombits(m.rateMean) }
|
||||
|
||||
// Snapshot returns the snapshot.
|
||||
func (m *MeterSnapshot) Snapshot() Meter { return m }
|
||||
|
@ -124,11 +126,12 @@ func (NilMeter) Stop() {}
|
|||
|
||||
// StandardMeter is the standard implementation of a Meter.
|
||||
type StandardMeter struct {
|
||||
lock sync.RWMutex
|
||||
// Only used on stop.
|
||||
lock sync.Mutex
|
||||
snapshot *MeterSnapshot
|
||||
a1, a5, a15 EWMA
|
||||
startTime time.Time
|
||||
stopped bool
|
||||
stopped uint32
|
||||
}
|
||||
|
||||
func newStandardMeter() *StandardMeter {
|
||||
|
@ -145,9 +148,9 @@ func newStandardMeter() *StandardMeter {
|
|||
func (m *StandardMeter) Stop() {
|
||||
m.lock.Lock()
|
||||
stopped := m.stopped
|
||||
m.stopped = true
|
||||
m.stopped = 1
|
||||
m.lock.Unlock()
|
||||
if !stopped {
|
||||
if stopped != 1 {
|
||||
arbiter.Lock()
|
||||
delete(arbiter.meters, m)
|
||||
arbiter.Unlock()
|
||||
|
@ -156,20 +159,17 @@ func (m *StandardMeter) Stop() {
|
|||
|
||||
// Count returns the number of events recorded.
|
||||
func (m *StandardMeter) Count() int64 {
|
||||
m.lock.RLock()
|
||||
count := m.snapshot.count
|
||||
m.lock.RUnlock()
|
||||
return count
|
||||
return atomic.LoadInt64(&m.snapshot.count)
|
||||
}
|
||||
|
||||
// Mark records the occurance of n events.
|
||||
func (m *StandardMeter) Mark(n int64) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
if m.stopped {
|
||||
if atomic.LoadUint32(&m.stopped) == 1 {
|
||||
return
|
||||
}
|
||||
m.snapshot.count += n
|
||||
|
||||
atomic.AddInt64(&m.snapshot.count, n)
|
||||
|
||||
m.a1.Update(n)
|
||||
m.a5.Update(n)
|
||||
m.a15.Update(n)
|
||||
|
@ -178,56 +178,49 @@ func (m *StandardMeter) Mark(n int64) {
|
|||
|
||||
// Rate1 returns the one-minute moving average rate of events per second.
|
||||
func (m *StandardMeter) Rate1() float64 {
|
||||
m.lock.RLock()
|
||||
rate1 := m.snapshot.rate1
|
||||
m.lock.RUnlock()
|
||||
return rate1
|
||||
return math.Float64frombits(atomic.LoadUint64(&m.snapshot.rate1))
|
||||
}
|
||||
|
||||
// Rate5 returns the five-minute moving average rate of events per second.
|
||||
func (m *StandardMeter) Rate5() float64 {
|
||||
m.lock.RLock()
|
||||
rate5 := m.snapshot.rate5
|
||||
m.lock.RUnlock()
|
||||
return rate5
|
||||
return math.Float64frombits(atomic.LoadUint64(&m.snapshot.rate5))
|
||||
}
|
||||
|
||||
// Rate15 returns the fifteen-minute moving average rate of events per second.
|
||||
func (m *StandardMeter) Rate15() float64 {
|
||||
m.lock.RLock()
|
||||
rate15 := m.snapshot.rate15
|
||||
m.lock.RUnlock()
|
||||
return rate15
|
||||
return math.Float64frombits(atomic.LoadUint64(&m.snapshot.rate15))
|
||||
}
|
||||
|
||||
// RateMean returns the meter's mean rate of events per second.
|
||||
func (m *StandardMeter) RateMean() float64 {
|
||||
m.lock.RLock()
|
||||
rateMean := m.snapshot.rateMean
|
||||
m.lock.RUnlock()
|
||||
return rateMean
|
||||
return math.Float64frombits(atomic.LoadUint64(&m.snapshot.rateMean))
|
||||
}
|
||||
|
||||
// Snapshot returns a read-only copy of the meter.
|
||||
func (m *StandardMeter) Snapshot() Meter {
|
||||
m.lock.RLock()
|
||||
snapshot := *m.snapshot
|
||||
m.lock.RUnlock()
|
||||
return &snapshot
|
||||
copiedSnapshot := MeterSnapshot{
|
||||
count: atomic.LoadInt64(&m.snapshot.count),
|
||||
rate1: atomic.LoadUint64(&m.snapshot.rate1),
|
||||
rate5: atomic.LoadUint64(&m.snapshot.rate5),
|
||||
rate15: atomic.LoadUint64(&m.snapshot.rate15),
|
||||
rateMean: atomic.LoadUint64(&m.snapshot.rateMean),
|
||||
}
|
||||
return &copiedSnapshot
|
||||
}
|
||||
|
||||
func (m *StandardMeter) updateSnapshot() {
|
||||
// should run with write lock held on m.lock
|
||||
snapshot := m.snapshot
|
||||
snapshot.rate1 = m.a1.Rate()
|
||||
snapshot.rate5 = m.a5.Rate()
|
||||
snapshot.rate15 = m.a15.Rate()
|
||||
snapshot.rateMean = float64(snapshot.count) / time.Since(m.startTime).Seconds()
|
||||
rate1 := math.Float64bits(m.a1.Rate())
|
||||
rate5 := math.Float64bits(m.a5.Rate())
|
||||
rate15 := math.Float64bits(m.a15.Rate())
|
||||
rateMean := math.Float64bits(float64(m.Count()) / time.Since(m.startTime).Seconds())
|
||||
|
||||
atomic.StoreUint64(&m.snapshot.rate1, rate1)
|
||||
atomic.StoreUint64(&m.snapshot.rate5, rate5)
|
||||
atomic.StoreUint64(&m.snapshot.rate15, rate15)
|
||||
atomic.StoreUint64(&m.snapshot.rateMean, rateMean)
|
||||
}
|
||||
|
||||
func (m *StandardMeter) tick() {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
m.a1.Tick()
|
||||
m.a5.Tick()
|
||||
m.a15.Tick()
|
||||
|
|
34
vendor/github.com/rcrowley/go-metrics/meter_test.go
generated
vendored
34
vendor/github.com/rcrowley/go-metrics/meter_test.go
generated
vendored
|
@ -1,6 +1,8 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -13,6 +15,38 @@ func BenchmarkMeter(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkMeterParallel(b *testing.B) {
|
||||
m := NewMeter()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
m.Mark(1)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// exercise race detector
|
||||
func TestMeterConcurrency(t *testing.T) {
|
||||
rand.Seed(time.Now().Unix())
|
||||
ma := meterArbiter{
|
||||
ticker: time.NewTicker(time.Millisecond),
|
||||
meters: make(map[*StandardMeter]struct{}),
|
||||
}
|
||||
m := newStandardMeter()
|
||||
ma.meters[m] = struct{}{}
|
||||
go ma.tick()
|
||||
wg := &sync.WaitGroup{}
|
||||
reps := 100
|
||||
for i := 0; i < reps; i++ {
|
||||
wg.Add(1)
|
||||
go func(m Meter, wg *sync.WaitGroup) {
|
||||
m.Mark(1)
|
||||
wg.Done()
|
||||
}(m, wg)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestGetOrRegisterMeter(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
NewRegisteredMeter("foo", r).Mark(47)
|
||||
|
|
19
vendor/github.com/rcrowley/go-metrics/registry.go
generated
vendored
19
vendor/github.com/rcrowley/go-metrics/registry.go
generated
vendored
|
@ -54,7 +54,7 @@ type Registry interface {
|
|||
// of names to metrics.
|
||||
type StandardRegistry struct {
|
||||
metrics map[string]interface{}
|
||||
mutex sync.Mutex
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
// Create a new registry.
|
||||
|
@ -71,8 +71,8 @@ func (r *StandardRegistry) Each(f func(string, interface{})) {
|
|||
|
||||
// Get the metric by the given name or nil if none is registered.
|
||||
func (r *StandardRegistry) Get(name string) interface{} {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
return r.metrics[name]
|
||||
}
|
||||
|
||||
|
@ -81,6 +81,15 @@ func (r *StandardRegistry) Get(name string) interface{} {
|
|||
// The interface can be the metric to register if not found in registry,
|
||||
// or a function returning the metric for lazy instantiation.
|
||||
func (r *StandardRegistry) GetOrRegister(name string, i interface{}) interface{} {
|
||||
// access the read lock first which should be re-entrant
|
||||
r.mutex.RLock()
|
||||
metric, ok := r.metrics[name]
|
||||
r.mutex.RUnlock()
|
||||
if ok {
|
||||
return metric
|
||||
}
|
||||
|
||||
// only take the write lock if we'll be modifying the metrics map
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
if metric, ok := r.metrics[name]; ok {
|
||||
|
@ -103,8 +112,8 @@ func (r *StandardRegistry) Register(name string, i interface{}) error {
|
|||
|
||||
// Run all registered healthchecks.
|
||||
func (r *StandardRegistry) RunHealthchecks() {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
for _, i := range r.metrics {
|
||||
if h, ok := i.(Healthcheck); ok {
|
||||
h.Check()
|
||||
|
|
60
vendor/github.com/rcrowley/go-metrics/registry_test.go
generated
vendored
60
vendor/github.com/rcrowley/go-metrics/registry_test.go
generated
vendored
|
@ -1,6 +1,7 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -13,6 +14,16 @@ func BenchmarkRegistry(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkRegistryParallel(b *testing.B) {
|
||||
r := NewRegistry()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
r.GetOrRegister("foo", NewCounter())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegistry(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
r.Register("foo", NewCounter())
|
||||
|
@ -301,5 +312,52 @@ func TestWalkRegistries(t *testing.T) {
|
|||
if "prefix.prefix2." != prefix {
|
||||
t.Fatal(prefix)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConcurrentRegistryAccess(t *testing.T) {
|
||||
r := NewRegistry()
|
||||
|
||||
counter := NewCounter()
|
||||
|
||||
signalChan := make(chan struct{})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func(dowork chan struct{}) {
|
||||
defer wg.Done()
|
||||
iface := r.GetOrRegister("foo", counter)
|
||||
retCounter, ok := iface.(Counter)
|
||||
if !ok {
|
||||
t.Fatal("Expected a Counter type")
|
||||
}
|
||||
if retCounter != counter {
|
||||
t.Fatal("Counter references don't match")
|
||||
}
|
||||
}(signalChan)
|
||||
}
|
||||
|
||||
close(signalChan) // Closing will cause all go routines to execute at the same time
|
||||
wg.Wait() // Wait for all go routines to do their work
|
||||
|
||||
// At the end of the test we should still only have a single "foo" Counter
|
||||
i := 0
|
||||
r.Each(func(name string, iface interface{}) {
|
||||
i++
|
||||
if "foo" != name {
|
||||
t.Fatal(name)
|
||||
}
|
||||
if _, ok := iface.(Counter); !ok {
|
||||
t.Fatal(iface)
|
||||
}
|
||||
})
|
||||
if 1 != i {
|
||||
t.Fatal(i)
|
||||
}
|
||||
r.Unregister("foo")
|
||||
i = 0
|
||||
r.Each(func(string, interface{}) { i++ })
|
||||
if 0 != i {
|
||||
t.Fatal(i)
|
||||
}
|
||||
}
|
||||
|
|
69
vendor/github.com/rcrowley/go-metrics/stathat/stathat.go
generated
vendored
69
vendor/github.com/rcrowley/go-metrics/stathat/stathat.go
generated
vendored
|
@ -1,69 +0,0 @@
|
|||
// Metrics output to StatHat.
|
||||
package stathat
|
||||
|
||||
import (
|
||||
"github.com/rcrowley/go-metrics"
|
||||
"github.com/stathat/go"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Stathat(r metrics.Registry, d time.Duration, userkey string) {
|
||||
for {
|
||||
if err := sh(r, userkey); nil != err {
|
||||
log.Println(err)
|
||||
}
|
||||
time.Sleep(d)
|
||||
}
|
||||
}
|
||||
|
||||
func sh(r metrics.Registry, userkey string) error {
|
||||
r.Each(func(name string, i interface{}) {
|
||||
switch metric := i.(type) {
|
||||
case metrics.Counter:
|
||||
stathat.PostEZCount(name, userkey, int(metric.Count()))
|
||||
case metrics.Gauge:
|
||||
stathat.PostEZValue(name, userkey, float64(metric.Value()))
|
||||
case metrics.GaugeFloat64:
|
||||
stathat.PostEZValue(name, userkey, float64(metric.Value()))
|
||||
case metrics.Histogram:
|
||||
h := metric.Snapshot()
|
||||
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
||||
stathat.PostEZCount(name+".count", userkey, int(h.Count()))
|
||||
stathat.PostEZValue(name+".min", userkey, float64(h.Min()))
|
||||
stathat.PostEZValue(name+".max", userkey, float64(h.Max()))
|
||||
stathat.PostEZValue(name+".mean", userkey, float64(h.Mean()))
|
||||
stathat.PostEZValue(name+".std-dev", userkey, float64(h.StdDev()))
|
||||
stathat.PostEZValue(name+".50-percentile", userkey, float64(ps[0]))
|
||||
stathat.PostEZValue(name+".75-percentile", userkey, float64(ps[1]))
|
||||
stathat.PostEZValue(name+".95-percentile", userkey, float64(ps[2]))
|
||||
stathat.PostEZValue(name+".99-percentile", userkey, float64(ps[3]))
|
||||
stathat.PostEZValue(name+".999-percentile", userkey, float64(ps[4]))
|
||||
case metrics.Meter:
|
||||
m := metric.Snapshot()
|
||||
stathat.PostEZCount(name+".count", userkey, int(m.Count()))
|
||||
stathat.PostEZValue(name+".one-minute", userkey, float64(m.Rate1()))
|
||||
stathat.PostEZValue(name+".five-minute", userkey, float64(m.Rate5()))
|
||||
stathat.PostEZValue(name+".fifteen-minute", userkey, float64(m.Rate15()))
|
||||
stathat.PostEZValue(name+".mean", userkey, float64(m.RateMean()))
|
||||
case metrics.Timer:
|
||||
t := metric.Snapshot()
|
||||
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
||||
stathat.PostEZCount(name+".count", userkey, int(t.Count()))
|
||||
stathat.PostEZValue(name+".min", userkey, float64(t.Min()))
|
||||
stathat.PostEZValue(name+".max", userkey, float64(t.Max()))
|
||||
stathat.PostEZValue(name+".mean", userkey, float64(t.Mean()))
|
||||
stathat.PostEZValue(name+".std-dev", userkey, float64(t.StdDev()))
|
||||
stathat.PostEZValue(name+".50-percentile", userkey, float64(ps[0]))
|
||||
stathat.PostEZValue(name+".75-percentile", userkey, float64(ps[1]))
|
||||
stathat.PostEZValue(name+".95-percentile", userkey, float64(ps[2]))
|
||||
stathat.PostEZValue(name+".99-percentile", userkey, float64(ps[3]))
|
||||
stathat.PostEZValue(name+".999-percentile", userkey, float64(ps[4]))
|
||||
stathat.PostEZValue(name+".one-minute", userkey, float64(t.Rate1()))
|
||||
stathat.PostEZValue(name+".five-minute", userkey, float64(t.Rate5()))
|
||||
stathat.PostEZValue(name+".fifteen-minute", userkey, float64(t.Rate15()))
|
||||
stathat.PostEZValue(name+".mean-rate", userkey, float64(t.RateMean()))
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue