Skip to content

Commit

Permalink
New GetOrRegisterCounter API.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrowley committed Oct 30, 2013
1 parent 569c4a2 commit 4be0647
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ func NewCounter() Counter {
return &StandardCounter{0}
}

// Create and register a new Counter.
func NewRegisteredCounter(name string, r Registry) Counter {
c := NewCounter()
if nil == r {
r = DefaultRegistry
}
r.Register(name, c)
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{}

Expand Down
8 changes: 8 additions & 0 deletions counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ func TestCounterZero(t *testing.T) {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestGetOrRegisterCounter(t *testing.T) {
r := NewRegistry()
GetOrRegisterCounter("foo", r).Inc(47)
if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
t.Fatal(c)
}
}

0 comments on commit 4be0647

Please sign in to comment.