Browse Source

(#25) Add GetOrRegister* for reach standard metric.

Richard Crowley 12 years ago
parent
commit
4e85b8b5ac
9 changed files with 73 additions and 8 deletions
  1. 8 8
      counter.go
  2. 8 0
      gauge.go
  3. 8 0
      gauge_test.go
  4. 8 0
      histogram.go
  5. 9 0
      histogram_test.go
  6. 8 0
      meter.go
  7. 8 0
      meter_test.go
  8. 8 0
      timer.go
  9. 8 0
      timer_test.go

+ 8 - 8
counter.go

@@ -13,6 +13,14 @@ type Counter interface {
 	Inc(int64)
 }
 
+// Get an existing or create and register a new Counter.
+func GetOrRegisterCounter(name string, r Registry) Counter {
+	if nil == r {
+		r = DefaultRegistry
+	}
+	return r.GetOrRegister(name, NewCounter()).(Counter)
+}
+
 // Create a new Counter.
 func NewCounter() Counter {
 	if UseNilMetrics {
@@ -31,14 +39,6 @@ func NewRegisteredCounter(name string, r Registry) Counter {
 	return c
 }
 
-// Get an existing or create and register a new Counter.
-func GetOrRegisterCounter(name string, r Registry) Counter {
-	if nil == r {
-		r = DefaultRegistry
-	}
-	return r.GetOrRegister(name, NewCounter()).(Counter)
-}
-
 // No-op Counter.
 type NilCounter struct{}
 

+ 8 - 0
gauge.go

@@ -11,6 +11,14 @@ type Gauge interface {
 	Value() int64
 }
 
+// Get an existing or create and register a new Gauge.
+func GetOrRegisterGauge(name string, r Registry) Gauge {
+	if nil == r {
+		r = DefaultRegistry
+	}
+	return r.GetOrRegister(name, NewGauge()).(Gauge)
+}
+
 // Create a new Gauge.
 func NewGauge() Gauge {
 	if UseNilMetrics {

+ 8 - 0
gauge_test.go

@@ -17,3 +17,11 @@ func TestGauge(t *testing.T) {
 		t.Errorf("g.Value(): 47 != %v\n", v)
 	}
 }
+
+func TestGetOrRegisterGauge(t *testing.T) {
+	r := NewRegistry()
+	GetOrRegisterGauge("foo", r).Update(47)
+	if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
+		t.Fatal(g)
+	}
+}

+ 8 - 0
histogram.go

@@ -24,6 +24,14 @@ type Histogram interface {
 	Variance() float64
 }
 
+// Get an existing or create and register a new Histogram.
+func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
+	if nil == r {
+		r = DefaultRegistry
+	}
+	return r.GetOrRegister(name, NewHistogram(s)).(Histogram)
+}
+
 // Create a new Histogram with the given Sample.  The initial values compare
 // so that the first value will be both min and max and the variance is flagged
 // for special treatment on its first iteration.

+ 9 - 0
histogram_test.go

@@ -10,6 +10,15 @@ func BenchmarkHistogram(b *testing.B) {
 	}
 }
 
+func TestGetOrRegisterHistogram(t *testing.T) {
+	r := NewRegistry()
+	s := NewUniformSample(100)
+	GetOrRegisterHistogram("foo", r, s).Update(47)
+	if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
+		t.Fatal(h)
+	}
+}
+
 func TestHistogram10000(t *testing.T) {
 	h := NewHistogram(NewUniformSample(100000))
 	for i := 1; i <= 10000; i++ {

+ 8 - 0
meter.go

@@ -16,6 +16,14 @@ type Meter interface {
 	RateMean() float64
 }
 
+// Get an existing or create and register a new Meter.
+func GetOrRegisterMeter(name string, r Registry) Meter {
+	if nil == r {
+		r = DefaultRegistry
+	}
+	return r.GetOrRegister(name, NewMeter()).(Meter)
+}
+
 // Create a new Meter.  Create the communication channels and start the
 // synchronizing goroutine.
 func NewMeter() Meter {

+ 8 - 0
meter_test.go

@@ -13,6 +13,14 @@ func BenchmarkMeter(b *testing.B) {
 	}
 }
 
+func TestGetOrRegisterMeter(t *testing.T) {
+	r := NewRegistry()
+	GetOrRegisterMeter("foo", r).Mark(47)
+	if m := GetOrRegisterMeter("foo", r); 47 != m.Count() {
+		t.Fatal(m)
+	}
+}
+
 func TestMeterDecay(t *testing.T) {
 	m := &StandardMeter{
 		make(chan int64),

+ 8 - 0
timer.go

@@ -23,6 +23,14 @@ type Timer interface {
 	UpdateSince(time.Time)
 }
 
+// Get an existing or create and register a new Timer.
+func GetOrRegisterTimer(name string, r Registry) Timer {
+	if nil == r {
+		r = DefaultRegistry
+	}
+	return r.GetOrRegister(name, NewTimer()).(Timer)
+}
+
 // Create a new timer with the given Histogram and Meter.
 func NewCustomTimer(h Histogram, m Meter) Timer {
 	if UseNilMetrics {

+ 8 - 0
timer_test.go

@@ -14,6 +14,14 @@ func BenchmarkTimer(b *testing.B) {
 	}
 }
 
+func TestGetOrRegisterTimer(t *testing.T) {
+	r := NewRegistry()
+	GetOrRegisterTimer("foo", r).Update(47)
+	if tm := GetOrRegisterTimer("foo", r); 1 != tm.Count() {
+		t.Fatal(tm)
+	}
+}
+
 func TestTimerExtremes(t *testing.T) {
 	tm := NewTimer()
 	tm.Update(math.MaxInt64)