Skip to content

Commit 4466c7b

Browse files
holisticodenonsense
authored andcommitted
metrics: added NewCounterForced (#17919)
1 parent 2868acd commit 4466c7b

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

metrics/counter.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package metrics
22

3-
import "sync/atomic"
3+
import (
4+
"sync/atomic"
5+
)
46

57
// Counters hold an int64 value that can be incremented and decremented.
68
type Counter interface {
@@ -20,6 +22,17 @@ func GetOrRegisterCounter(name string, r Registry) Counter {
2022
return r.GetOrRegister(name, NewCounter).(Counter)
2123
}
2224

25+
// GetOrRegisterCounterForced returns an existing Counter or constructs and registers a
26+
// new Counter no matter the global switch is enabled or not.
27+
// Be sure to unregister the counter from the registry once it is of no use to
28+
// allow for garbage collection.
29+
func GetOrRegisterCounterForced(name string, r Registry) Counter {
30+
if nil == r {
31+
r = DefaultRegistry
32+
}
33+
return r.GetOrRegister(name, NewCounterForced).(Counter)
34+
}
35+
2336
// NewCounter constructs a new StandardCounter.
2437
func NewCounter() Counter {
2538
if !Enabled {
@@ -28,6 +41,12 @@ func NewCounter() Counter {
2841
return &StandardCounter{0}
2942
}
3043

44+
// NewCounterForced constructs a new StandardCounter and returns it no matter if
45+
// the global switch is enabled or not.
46+
func NewCounterForced() Counter {
47+
return &StandardCounter{0}
48+
}
49+
3150
// NewRegisteredCounter constructs and registers a new StandardCounter.
3251
func NewRegisteredCounter(name string, r Registry) Counter {
3352
c := NewCounter()
@@ -38,6 +57,19 @@ func NewRegisteredCounter(name string, r Registry) Counter {
3857
return c
3958
}
4059

60+
// NewRegisteredCounterForced constructs and registers a new StandardCounter
61+
// and launches a goroutine no matter the global switch is enabled or not.
62+
// Be sure to unregister the counter from the registry once it is of no use to
63+
// allow for garbage collection.
64+
func NewRegisteredCounterForced(name string, r Registry) Counter {
65+
c := NewCounterForced()
66+
if nil == r {
67+
r = DefaultRegistry
68+
}
69+
r.Register(name, c)
70+
return c
71+
}
72+
4173
// CounterSnapshot is a read-only copy of another Counter.
4274
type CounterSnapshot int64
4375

0 commit comments

Comments
 (0)