Richard Crowley 14 năm trước cách đây
mục cha
commit
10c63bdfe9
11 tập tin đã thay đổi với 76 bổ sung62 xóa
  1. 2 1
      cmd/metrics-bench/Makefile
  2. 0 0
      cmd/metrics-bench/main.go
  3. 2 1
      cmd/metrics-example/Makefile
  4. 12 8
      cmd/metrics-example/main.go
  5. 6 8
      healthcheck.go
  6. 1 1
      log.go
  7. 12 9
      meter.go
  8. 9 5
      registry.go
  9. 28 25
      sample.go
  10. 2 2
      syslog.go
  11. 2 2
      timer.go

+ 2 - 1
cmd/metrics-bench/Makefile

@@ -1,7 +1,8 @@
 include $(GOROOT)/src/Make.inc
 
 TARG=metrics-bench
-GOFILES=metrics-bench.go
+GOFILES=\
+	main.go\
 
 include $(GOROOT)/src/Make.cmd
 

+ 0 - 0
cmd/metrics-bench/metrics-bench.go → cmd/metrics-bench/main.go


+ 2 - 1
cmd/metrics-example/Makefile

@@ -1,7 +1,8 @@
 include $(GOROOT)/src/Make.inc
 
 TARG=metrics-example
-GOFILES=metrics-example.go
+GOFILES=\
+	main.go\
 
 include $(GOROOT)/src/Make.cmd
 

+ 12 - 8
cmd/metrics-example/metrics-example.go → cmd/metrics-example/main.go

@@ -1,11 +1,12 @@
 package main
 
 import (
-	"log"
+	"errors"
 	"github.com/rcrowley/go-metrics"
+	"log"
+	"math/rand"
 	"os"
-	"rand"
-//	"syslog"
+	// "syslog"
 	"time"
 )
 
@@ -53,13 +54,13 @@ func main() {
 		if 0 < rand.Intn(2) {
 			h.Healthy()
 		} else {
-			h.Unhealthy(os.NewError("baz"))
+			h.Unhealthy(errors.New("baz"))
 		}
 	})
 	r.Register("baz", hc)
 
 	s := metrics.NewExpDecaySample(1028, 0.015)
-//	s := metrics.NewUniformSample(1028)
+	//s := metrics.NewUniformSample(1028)
 	h := metrics.NewHistogram(s)
 	r.Register("bang", h)
 	for i := 0; i < fanout; i++ {
@@ -112,15 +113,18 @@ func main() {
 	metrics.RegisterRuntimeMemStats(r)
 	go func() {
 		t := time.NewTicker(5e9)
-		for 0 < <-t.C { metrics.CaptureRuntimeMemStats(r, true) }
+		for {
+			<-t.C
+			metrics.CaptureRuntimeMemStats(r, true)
+		}
 	}()
 
 	metrics.Log(r, 60, 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, 60, w)
-*/
+	*/
 
 }

+ 6 - 8
healthcheck.go

@@ -1,23 +1,21 @@
 package metrics
 
-import "os"
-
 // Healthchecks hold an os.Error value describing an arbitrary up/down status.
 //
 // This is an interface so as to encourage other structs to implement
 // the Healthcheck API as appropriate.
 type Healthcheck interface {
 	Check()
-	Error() os.Error
+	Error() error
 	Healthy()
-	Unhealthy(os.Error)
+	Unhealthy(error)
 }
 
 // The standard implementation of a Healthcheck stores the status and a
 // function to call to update the status.
 type StandardHealthcheck struct {
-	err os.Error
-	f func(Healthcheck)
+	err error
+	f   func(Healthcheck)
 }
 
 // Create a new healthcheck, which will use the given function to update
@@ -32,7 +30,7 @@ func (h *StandardHealthcheck) Check() {
 }
 
 // Return the healthcheck's status, which will be nil if it is healthy.
-func (h *StandardHealthcheck) Error() os.Error {
+func (h *StandardHealthcheck) Error() error {
 	return h.err
 }
 
@@ -42,6 +40,6 @@ func (h *StandardHealthcheck) Healthy() {
 }
 
 // Mark the healthcheck as unhealthy.  The error should provide details.
-func (h *StandardHealthcheck) Unhealthy(err os.Error) {
+func (h *StandardHealthcheck) Unhealthy(err error) {
 	h.err = err
 }

+ 1 - 1
log.go

@@ -60,6 +60,6 @@ func Log(r Registry, interval int, l *log.Logger) {
 				l.Printf("  mean rate:   %12.2f\n", m.RateMean())
 			}
 		})
-		time.Sleep(int64(1e9) * int64(interval))
+		time.Sleep(time.Duration(int64(1e9) * int64(interval)))
 	}
 }

+ 12 - 9
meter.go

@@ -20,15 +20,15 @@ type Meter interface {
 // its calculations and another goroutine (via time.Ticker) to produce
 // clock ticks.
 type StandardMeter struct {
-	in chan int64
-	out chan meterV
+	in     chan int64
+	out    chan meterV
 	ticker *time.Ticker
 }
 
 // A meterV contains all the values that would need to be passed back
 // from the synchronizing goroutine.
 type meterV struct {
-	count int64
+	count                          int64
 	rate1, rate5, rate15, rateMean float64
 }
 
@@ -82,16 +82,19 @@ func (m *StandardMeter) arbiter() {
 	a1 := NewEWMA1()
 	a5 := NewEWMA5()
 	a15 := NewEWMA15()
-	tsStart := time.Nanoseconds()
+	tsStart := time.Now()
 	for {
 		select {
 		case n := <-m.in:
 			mv.count += n
-			a1.Update(n); mv.rate1 = a1.Rate()
-			a5.Update(n); mv.rate5 = a5.Rate()
-			a15.Update(n); mv.rate15 = a15.Rate()
-			mv.rateMean = float64(1e9 * mv.count) / float64(
-				time.Nanoseconds() - tsStart)
+			a1.Update(n)
+			mv.rate1 = a1.Rate()
+			a5.Update(n)
+			mv.rate5 = a5.Rate()
+			a15.Update(n)
+			mv.rate15 = a15.Rate()
+			mv.rateMean = float64(1e9*mv.count) / float64(
+				time.Now().Sub(tsStart))
 		case m.out <- mv:
 		case <-m.ticker.C:
 			a1.Tick()

+ 9 - 5
registry.go

@@ -18,13 +18,13 @@ type Registry interface {
 // The standard implementation of a Registry is a set of mutex-protected
 // maps of names to metrics.
 type StandardRegistry struct {
-	mutex *sync.Mutex
+	mutex   *sync.Mutex
 	metrics map[string]interface{}
 }
 
 // Create a new registry.
 func NewRegistry() *StandardRegistry {
-	return &StandardRegistry {
+	return &StandardRegistry{
 		&sync.Mutex{},
 		make(map[string]interface{}),
 	}
@@ -33,7 +33,9 @@ func NewRegistry() *StandardRegistry {
 // Call the given function for each registered metric.
 func (r *StandardRegistry) Each(f func(string, interface{})) {
 	r.mutex.Lock()
-	for name, i := range r.metrics { f(name, i) }
+	for name, i := range r.metrics {
+		f(name, i)
+	}
 	r.mutex.Unlock()
 }
 
@@ -59,7 +61,9 @@ func (r *StandardRegistry) Register(name string, i interface{}) {
 func (r *StandardRegistry) RunHealthchecks() {
 	r.mutex.Lock()
 	for _, i := range r.metrics {
-		if h, ok := i.(Healthcheck); ok { h.Check() }
+		if h, ok := i.(Healthcheck); ok {
+			h.Check()
+		}
 	}
 	r.mutex.Unlock()
 }
@@ -67,6 +71,6 @@ func (r *StandardRegistry) RunHealthchecks() {
 // Unregister the metric with the given name.
 func (r *StandardRegistry) Unregister(name string) {
 	r.mutex.Lock()
-	r.metrics[name] = nil, false
+	delete(r.metrics, name)
 	r.mutex.Unlock()
 }

+ 28 - 25
sample.go

@@ -2,7 +2,7 @@ package metrics
 
 import (
 	"math"
-	"rand"
+	"math/rand"
 	"time"
 )
 
@@ -27,10 +27,10 @@ type Sample interface {
 // <http://www.research.att.com/people/Cormode_Graham/library/publications/CormodeShkapenyukSrivastavaXu09.pdf>
 type ExpDecaySample struct {
 	reservoirSize int
-	alpha float64
-	in chan int64
-	out chan []int64
-	reset chan bool
+	alpha         float64
+	in            chan int64
+	out           chan []int64
+	reset         chan bool
 }
 
 // Create a new exponentially-decaying sample with the given reservoir size
@@ -73,36 +73,37 @@ func (s *ExpDecaySample) Values() []int64 {
 func (s *ExpDecaySample) arbiter() {
 	count := 0
 	values := make(map[float64]int64)
-	tsStart := time.Seconds()
-	tsNext := time.Nanoseconds() + rescaleThreshold
+	start := time.Now()
+	next := time.Now().Add(rescaleThreshold)
 	var valuesCopy []int64
 	for {
 		select {
 		case v := <-s.in:
-			ts := time.Seconds()
-			k := math.Exp(float64(ts - tsStart) * s.alpha) / rand.Float64()
+			now := time.Now()
+			k := math.Exp(float64(now.Sub(start))*s.alpha) / rand.Float64()
 			count++
 			values[k] = v
 			if count > s.reservoirSize {
 				min := math.MaxFloat64
 				for k, _ := range values {
-					if k < min { min = k }
+					if k < min {
+						min = k
+					}
 				}
-				values[min] = 0, false
+				delete(values, min)
 				valuesCopy = make([]int64, s.reservoirSize)
 			} else {
 				valuesCopy = make([]int64, count)
 			}
-			tsNano := time.Nanoseconds()
-			if tsNano > tsNext {
-				tsOldStart := tsStart
-				tsStart = time.Seconds()
-				tsNext = tsNano + rescaleThreshold
+			if now.After(next) {
+				oldStart := start
+				start = time.Now()
+				next = now.Add(rescaleThreshold)
 				oldValues := values
 				values = make(map[float64]int64, len(oldValues))
 				for k, v := range oldValues {
-					values[k * math.Exp(-s.alpha * float64(
-						tsStart - tsOldStart))] = v
+					values[k*math.Exp(-s.alpha*float64(
+						start.Sub(oldStart)))] = v
 				}
 			}
 			i := 0
@@ -115,8 +116,8 @@ func (s *ExpDecaySample) arbiter() {
 			count = 0
 			values = make(map[float64]int64)
 			valuesCopy = make([]int64, 0)
-			tsStart = time.Seconds()
-			tsNext = tsStart + rescaleThreshold
+			start = time.Now()
+			next = start.Add(rescaleThreshold)
 		}
 	}
 }
@@ -126,9 +127,9 @@ func (s *ExpDecaySample) arbiter() {
 // <http://www.cs.umd.edu/~samir/498/vitter.pdf>
 type UniformSample struct {
 	reservoirSize int
-	in chan int64
-	out chan []int64
-	reset chan bool
+	in            chan int64
+	out           chan []int64
+	reset         chan bool
 }
 
 // Create a new uniform sample with the given reservoir size.
@@ -174,13 +175,15 @@ func (s *UniformSample) arbiter() {
 		case v := <-s.in:
 			count++
 			if count < s.reservoirSize {
-				values[count - 1] = v
+				values[count-1] = v
 				valuesCopy = make([]int64, count)
 			} else {
 				values[rand.Intn(s.reservoirSize)] = v
 				valuesCopy = make([]int64, len(values))
 			}
-			for i := 0; i < len(valuesCopy); i++ { valuesCopy[i] = values[i] }
+			for i := 0; i < len(valuesCopy); i++ {
+				valuesCopy[i] = values[i]
+			}
 		case s.out <- valuesCopy: // TODO Might need to make another copy here.
 		case <-s.reset:
 			count = 0

+ 2 - 2
syslog.go

@@ -2,7 +2,7 @@ package metrics
 
 import (
 	"fmt"
-	"syslog"
+	"log/syslog"
 	"time"
 )
 
@@ -67,6 +67,6 @@ func Syslog(r Registry, interval int, w *syslog.Writer) {
 				))
 			}
 		})
-		time.Sleep(int64(1e9) * int64(interval))
+		time.Sleep(time.Duration(int64(1e9) * int64(interval)))
 	}
 }

+ 2 - 2
timer.go

@@ -100,9 +100,9 @@ func (t *StandardTimer) StdDev() float64 {
 
 // Record the duration of the execution of the given function.
 func (t *StandardTimer) Time(f func()) {
-	ts := time.Nanoseconds()
+	ts := time.Now()
 	f()
-	t.Update(uint64(time.Nanoseconds() - ts))
+	t.Update(uint64(time.Now().Sub(ts)))
 }
 
 // Record the duration of an event.