-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
231 lines (197 loc) · 5.57 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tally
import (
"math"
"sync"
"sync/atomic"
"time"
)
var (
capabilitiesNone = &capabilities{
reporting: false,
tagging: false,
}
capabilitiesReportingNoTagging = &capabilities{
reporting: true,
tagging: false,
}
capabilitiesReportingTagging = &capabilities{
reporting: true,
tagging: true,
}
)
type capabilities struct {
reporting bool
tagging bool
}
func (c *capabilities) Reporting() bool {
return c.reporting
}
func (c *capabilities) Tagging() bool {
return c.tagging
}
type counter struct {
prev int64
curr int64
cachedCount CachedCount
}
func newCounter(cachedCount CachedCount) *counter {
return &counter{cachedCount: cachedCount}
}
func (c *counter) Inc(v int64) {
atomic.AddInt64(&c.curr, v)
}
func (c *counter) value() int64 {
curr := atomic.LoadInt64(&c.curr)
prev := atomic.LoadInt64(&c.prev)
if prev == curr {
return 0
}
atomic.StoreInt64(&c.prev, curr)
return curr - prev
}
func (c *counter) report(name string, tags map[string]string, r StatsReporter) {
delta := c.value()
if delta == 0 {
return
}
r.ReportCounter(name, tags, delta)
}
func (c *counter) cachedReport() {
delta := c.value()
if delta == 0 {
return
}
c.cachedCount.ReportCount(delta)
}
func (c *counter) snapshot() int64 {
return atomic.LoadInt64(&c.curr) - atomic.LoadInt64(&c.prev)
}
type gauge struct {
updated uint64
curr uint64
cachedGauge CachedGauge
}
func newGauge(cachedGauge CachedGauge) *gauge {
return &gauge{cachedGauge: cachedGauge}
}
func (g *gauge) Update(v float64) {
atomic.StoreUint64(&g.curr, math.Float64bits(v))
atomic.StoreUint64(&g.updated, 1)
}
func (g *gauge) value() float64 {
return math.Float64frombits(atomic.LoadUint64(&g.curr))
}
func (g *gauge) report(name string, tags map[string]string, r StatsReporter) {
if atomic.SwapUint64(&g.updated, 0) == 1 {
r.ReportGauge(name, tags, g.value())
}
}
func (g *gauge) cachedReport() {
if atomic.SwapUint64(&g.updated, 0) == 1 {
g.cachedGauge.ReportGauge(g.value())
}
}
func (g *gauge) snapshot() float64 {
return math.Float64frombits(atomic.LoadUint64(&g.curr))
}
// NB(jra3): timers are a little special because they do no aggregate any data
// at the timer level. The reporter buffers may timer entries and periodically
// flushes.
type timer struct {
name string
tags map[string]string
reporter StatsReporter
cachedTimer CachedTimer
unreported timerValues
}
type timerValues struct {
sync.RWMutex
values []time.Duration
}
func newTimer(
name string,
tags map[string]string,
r StatsReporter,
cachedTimer CachedTimer,
) *timer {
t := &timer{
name: name,
tags: tags,
cachedTimer: cachedTimer,
reporter: r,
}
if r == nil {
t.reporter = &timerNoReporterSink{timer: t}
}
return t
}
func (t *timer) Start() Stopwatch {
return Stopwatch{start: globalClock.Now(), timer: t}
}
func (t *timer) Record(interval time.Duration) {
if t.cachedTimer != nil {
t.cachedTimer.ReportTimer(interval)
} else {
t.reporter.ReportTimer(t.name, t.tags, interval)
}
}
func (t *timer) snapshot() []time.Duration {
t.unreported.RLock()
snap := make([]time.Duration, len(t.unreported.values))
for i := range t.unreported.values {
snap[i] = t.unreported.values[i]
}
t.unreported.RUnlock()
return snap
}
type timerNoReporterSink struct {
sync.RWMutex
timer *timer
}
func (r *timerNoReporterSink) ReportCounter(name string, tags map[string]string, value int64) {
}
func (r *timerNoReporterSink) ReportGauge(name string, tags map[string]string, value float64) {
}
func (r *timerNoReporterSink) ReportTimer(name string, tags map[string]string, interval time.Duration) {
r.timer.unreported.Lock()
r.timer.unreported.values = append(r.timer.unreported.values, interval)
r.timer.unreported.Unlock()
}
func (r *timerNoReporterSink) Capabilities() Capabilities {
return capabilitiesReportingTagging
}
func (r *timerNoReporterSink) Flush() {
}
// NullStatsReporter is an implementatin of StatsReporter than simply does nothing.
var NullStatsReporter StatsReporter = nullStatsReporter{}
func (r nullStatsReporter) ReportCounter(name string, tags map[string]string, value int64) {
}
func (r nullStatsReporter) ReportGauge(name string, tags map[string]string, value float64) {
}
func (r nullStatsReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) {
}
func (r nullStatsReporter) Capabilities() Capabilities {
return capabilitiesReportingNoTagging
}
func (r nullStatsReporter) Flush() {
}
type nullStatsReporter struct{}