...
1 package metrics
2
3 import "sync/atomic"
4
5
6 type Counter interface {
7 Clear()
8 Count() int64
9 Dec(int64)
10 Inc(int64)
11 Snapshot() Counter
12 }
13
14
15
16 func GetOrRegisterCounter(name string, r Registry) Counter {
17 if nil == r {
18 r = DefaultRegistry
19 }
20 return r.GetOrRegister(name, NewCounter).(Counter)
21 }
22
23
24 func NewCounter() Counter {
25 if UseNilMetrics {
26 return NilCounter{}
27 }
28 return &StandardCounter{0}
29 }
30
31
32 func NewRegisteredCounter(name string, r Registry) Counter {
33 c := NewCounter()
34 if nil == r {
35 r = DefaultRegistry
36 }
37 r.Register(name, c)
38 return c
39 }
40
41
42 type CounterSnapshot int64
43
44
45 func (CounterSnapshot) Clear() {
46 panic("Clear called on a CounterSnapshot")
47 }
48
49
50 func (c CounterSnapshot) Count() int64 { return int64(c) }
51
52
53 func (CounterSnapshot) Dec(int64) {
54 panic("Dec called on a CounterSnapshot")
55 }
56
57
58 func (CounterSnapshot) Inc(int64) {
59 panic("Inc called on a CounterSnapshot")
60 }
61
62
63 func (c CounterSnapshot) Snapshot() Counter { return c }
64
65
66 type NilCounter struct{}
67
68
69 func (NilCounter) Clear() {}
70
71
72 func (NilCounter) Count() int64 { return 0 }
73
74
75 func (NilCounter) Dec(i int64) {}
76
77
78 func (NilCounter) Inc(i int64) {}
79
80
81 func (NilCounter) Snapshot() Counter { return NilCounter{} }
82
83
84
85 type StandardCounter struct {
86 count int64
87 }
88
89
90 func (c *StandardCounter) Clear() {
91 atomic.StoreInt64(&c.count, 0)
92 }
93
94
95 func (c *StandardCounter) Count() int64 {
96 return atomic.LoadInt64(&c.count)
97 }
98
99
100 func (c *StandardCounter) Dec(i int64) {
101 atomic.AddInt64(&c.count, -i)
102 }
103
104
105 func (c *StandardCounter) Inc(i int64) {
106 atomic.AddInt64(&c.count, i)
107 }
108
109
110 func (c *StandardCounter) Snapshot() Counter {
111 return CounterSnapshot(c.Count())
112 }
113
View as plain text