-
Notifications
You must be signed in to change notification settings - Fork 18
/
stats.go
630 lines (514 loc) · 17.2 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package stats
import (
"context"
"strconv"
"sync"
"sync/atomic"
"time"
tagspkg "github.com/lyft/gostats/internal/tags"
)
// A Store holds statistics.
// There are two options when creating a new store:
//
// create a store backed by a tcp_sink to statsd
// s := stats.NewDefaultStore()
// create a store with a user provided Sink
// s := stats.NewStore(sink, true)
//
// Currently that only backing store supported is statsd via a TCP sink, https://github.com/lyft/gostats/blob/master/tcp_sink.go.
// However, implementing other Sinks (https://github.com/lyft/gostats/blob/master/sink.go) should be simple.
//
// A store holds Counters, Gauges, and Timers. You can add unscoped Counters, Gauges, and Timers to the store
// with:
//
// s := stats.NewDefaultStore()
// c := s.New[Counter|Gauge|Timer]("name")
type Store interface {
// Flush Counters and Gauges to the Sink attached to the Store.
// To flush the store at a regular interval call the
// Start(*time.Ticker)
// method on it.
//
// The store will flush either at the regular interval, or whenever
// Flush()
// is called. Whenever the store is flushed,
// the store will call
// GenerateStats()
// on all of its stat generators,
// and flush all the Counters and Gauges registered with it.
Flush()
// Start a timer for periodic stat flushes. This is a blocking
// call and should be called in a goroutine.
Start(*time.Ticker)
// StartContext starts a timer for periodic stat flushes. This is
// a blocking call and should be called in a goroutine.
//
// If the passed-in context is cancelled, then this call
// exits. Flush will be called on exit.
StartContext(context.Context, *time.Ticker)
// Add a StatGenerator to the Store that programatically generates stats.
AddStatGenerator(StatGenerator)
Scope
}
// A Scope namespaces Statistics.
//
// store := stats.NewDefaultStore()
// scope := stats.Scope("service")
// // the following counter will be emitted at the stats tree rooted at `service`.
// c := scope.NewCounter("success")
//
// Additionally you can create subscopes:
//
// store := stats.NewDefaultStore()
// scope := stats.Scope("service")
// networkScope := scope.Scope("network")
// // the following counter will be emitted at the stats tree rooted at service.network.
// c := networkScope.NewCounter("requests")
type Scope interface {
// Scope creates a subscope.
Scope(name string) Scope
// ScopeWithTags creates a subscope with Tags to a store or scope. All child scopes and metrics
// will inherit these tags by default.
ScopeWithTags(name string, tags map[string]string) Scope
// Store returns the Scope's backing Store.
Store() Store
// NewCounter adds a Counter to a store, or a scope.
NewCounter(name string) Counter
// NewCounterWithTags adds a Counter with Tags to a store, or a scope.
NewCounterWithTags(name string, tags map[string]string) Counter
// NewPerInstanceCounter adds a Per instance Counter with optional Tags to a store, or a scope.
NewPerInstanceCounter(name string, tags map[string]string) Counter
// NewGauge adds a Gauge to a store, or a scope.
NewGauge(name string) Gauge
// NewGaugeWithTags adds a Gauge with Tags to a store, or a scope.
NewGaugeWithTags(name string, tags map[string]string) Gauge
// NewPerInstanceGauge adds a Per instance Gauge with optional Tags to a store, or a scope.
NewPerInstanceGauge(name string, tags map[string]string) Gauge
// NewTimer adds a Timer to a store, or a scope that uses microseconds as its unit.
NewTimer(name string) Timer
// NewTimerWithTags adds a Timer with Tags to a store, or a scope with Tags that uses microseconds as its unit.
NewTimerWithTags(name string, tags map[string]string) Timer
// NewPerInstanceTimer adds a Per instance Timer with optional Tags to a store, or a scope that uses microseconds as its unit.
NewPerInstanceTimer(name string, tags map[string]string) Timer
// NewMilliTimer adds a Timer to a store, or a scope that uses milliseconds as its unit.
NewMilliTimer(name string) Timer
// NewMilliTimerWithTags adds a Timer with Tags to a store, or a scope with Tags that uses milliseconds as its unit.
NewMilliTimerWithTags(name string, tags map[string]string) Timer
// NewPerInstanceMilliTimer adds a Per instance Timer with optional Tags to a store, or a scope that uses milliseconds as its unit.
NewPerInstanceMilliTimer(name string, tags map[string]string) Timer
}
// A Counter is an always incrementing stat.
type Counter interface {
// Add increments the Counter by the argument's value.
Add(uint64)
// Inc increments the Counter by 1.
Inc()
// Set sets an internal counter value which will be written in the next flush.
// Its use is discouraged as it may break the counter's "always incrementing" semantics.
Set(uint64)
// String returns the current value of the Counter as a string.
String() string
// Value returns the current value of the Counter as a uint64.
Value() uint64
}
// A Gauge is a stat that can increment and decrement.
type Gauge interface {
// Add increments the Gauge by the argument's value.
Add(uint64)
// Sub decrements the Gauge by the argument's value.
Sub(uint64)
// Inc increments the Gauge by 1.
Inc()
// Dec decrements the Gauge by 1.
Dec()
// Set sets the Gauge to a value.
Set(uint64)
// String returns the current value of the Gauge as a string.
String() string
// Value returns the current value of the Gauge as a uint64.
Value() uint64
}
// A Timer is used to flush timing statistics.
type Timer interface {
// AddValue flushs the timer with the argument's value.
AddValue(float64)
// AddDuration emits the duration as a timing measurement.
AddDuration(time.Duration)
// AllocateSpan allocates a Timespan.
AllocateSpan() Timespan
}
// A Timespan is used to measure spans of time.
// They measure time from the time they are allocated by a Timer with
//
// AllocateSpan()
//
// until they call
//
// Complete()
//
// or
//
// CompleteWithDuration(time.Duration)
//
// When either function is called the timespan is flushed.
// When Complete is called the timespan is flushed.
//
// A Timespan can be flushed at function
// return by calling Complete with golang's defer statement.
type Timespan interface {
// End the Timespan and flush it.
Complete() time.Duration
// End the Timespan and flush it. Adds additional time.Duration to the measured time
CompleteWithDuration(time.Duration)
}
// A StatGenerator can be used to programatically generate stats.
// StatGenerators are added to a store via
//
// AddStatGenerator(StatGenerator)
//
// An example is https://github.com/lyft/gostats/blob/master/runtime.go.
type StatGenerator interface {
// Runs the StatGenerator to generate Stats.
GenerateStats()
}
// NewStore returns an Empty store that flushes to Sink passed as an argument.
// Note: the export argument is unused.
func NewStore(sink Sink, _ bool) Store {
return &statStore{sink: sink}
}
// NewDefaultStore returns a Store with a TCP statsd sink, and a running flush timer.
func NewDefaultStore() Store {
var newStore Store
settings := GetSettings()
if !settings.UseStatsd {
if settings.LoggingSinkDisabled {
newStore = NewStore(NewNullSink(), false)
} else {
newStore = NewStore(NewLoggingSink(), false)
}
go newStore.Start(time.NewTicker(10 * time.Second))
} else {
newStore = NewStore(NewTCPStatsdSink(), false)
go newStore.Start(time.NewTicker(time.Duration(settings.FlushIntervalS) * time.Second))
}
return newStore
}
type counter struct {
currentValue uint64
lastSentValue uint64
}
func (c *counter) Add(delta uint64) {
atomic.AddUint64(&c.currentValue, delta)
}
func (c *counter) Set(value uint64) {
atomic.StoreUint64(&c.currentValue, value)
}
func (c *counter) Inc() {
c.Add(1)
}
func (c *counter) Value() uint64 {
return atomic.LoadUint64(&c.currentValue)
}
func (c *counter) String() string {
return strconv.FormatUint(c.Value(), 10)
}
func (c *counter) latch() uint64 {
value := c.Value()
lastSent := atomic.SwapUint64(&c.lastSentValue, value)
return value - lastSent
}
type gauge struct {
value uint64
}
func (c *gauge) String() string {
return strconv.FormatUint(c.Value(), 10)
}
func (c *gauge) Add(value uint64) {
atomic.AddUint64(&c.value, value)
}
func (c *gauge) Sub(value uint64) {
atomic.AddUint64(&c.value, ^(value - 1))
}
func (c *gauge) Inc() {
c.Add(1)
}
func (c *gauge) Dec() {
c.Sub(1)
}
func (c *gauge) Set(value uint64) {
atomic.StoreUint64(&c.value, value)
}
func (c *gauge) Value() uint64 {
return atomic.LoadUint64(&c.value)
}
type timer struct {
base time.Duration
name string
sink Sink
}
func (t *timer) time(dur time.Duration) {
t.AddDuration(dur)
}
func (t *timer) AddDuration(dur time.Duration) {
t.AddValue(float64(dur / t.base))
}
func (t *timer) AddValue(value float64) {
t.sink.FlushTimer(t.name, value)
}
func (t *timer) AllocateSpan() Timespan {
return ×pan{timer: t, start: time.Now()}
}
type timespan struct {
timer *timer
start time.Time
}
func (ts *timespan) Complete() time.Duration {
d := time.Since(ts.start)
ts.timer.time(d)
return d
}
func (ts *timespan) CompleteWithDuration(value time.Duration) {
ts.timer.time(value)
}
type statStore struct {
counters sync.Map
gauges sync.Map
timers sync.Map
mu sync.RWMutex
statGenerators []StatGenerator
sink Sink
}
var ReservedTagWords = map[string]bool{"asg": true, "az": true, "backend": true, "canary": true, "host": true, "period": true, "region": true, "shard": true, "window": true, "source": true, "project": true, "facet": true, "envoyservice": true}
func (s *statStore) validateTags(tags map[string]string) {
for k := range tags {
if _, ok := ReservedTagWords[k]; ok {
// Keep track of how many times a reserved tag is used
s.NewCounter("reserved_tag").Inc()
}
}
}
func (s *statStore) StartContext(ctx context.Context, ticker *time.Ticker) {
for {
select {
case <-ctx.Done():
s.Flush()
return
case <-ticker.C:
s.Flush()
}
}
}
func (s *statStore) Start(ticker *time.Ticker) {
s.StartContext(context.Background(), ticker)
}
func (s *statStore) Flush() {
s.mu.RLock()
for _, g := range s.statGenerators {
g.GenerateStats()
}
s.mu.RUnlock()
s.counters.Range(func(key, v interface{}) bool {
// do not flush counters that are set to zero
if value := v.(*counter).latch(); value != 0 {
s.sink.FlushCounter(key.(string), value)
}
return true
})
s.gauges.Range(func(key, v interface{}) bool {
s.sink.FlushGauge(key.(string), v.(*gauge).Value())
return true
})
flushableSink, ok := s.sink.(FlushableSink)
if ok {
flushableSink.Flush()
}
}
func (s *statStore) AddStatGenerator(statGenerator StatGenerator) {
s.mu.Lock()
defer s.mu.Unlock()
s.statGenerators = append(s.statGenerators, statGenerator)
}
func (s *statStore) Store() Store {
return s
}
func (s *statStore) Scope(name string) Scope {
return newSubScope(s, name, nil)
}
func (s *statStore) ScopeWithTags(name string, tags map[string]string) Scope {
s.validateTags(tags)
return newSubScope(s, name, tags)
}
func (s *statStore) newCounter(serializedName string) *counter {
if v, ok := s.counters.Load(serializedName); ok {
return v.(*counter)
}
c := new(counter)
if v, loaded := s.counters.LoadOrStore(serializedName, c); loaded {
return v.(*counter)
}
return c
}
func (s *statStore) NewCounter(name string) Counter {
return s.newCounter(name)
}
func (s *statStore) NewCounterWithTags(name string, tags map[string]string) Counter {
s.validateTags(tags)
return s.newCounter(tagspkg.SerializeTags(name, tags))
}
func (s *statStore) newCounterWithTagSet(name string, tags tagspkg.TagSet) Counter {
return s.newCounter(tags.Serialize(name))
}
var emptyPerInstanceTags = map[string]string{"_f": "i"}
func (s *statStore) NewPerInstanceCounter(name string, tags map[string]string) Counter {
if len(tags) == 0 {
return s.NewCounterWithTags(name, emptyPerInstanceTags)
}
if _, found := tags["_f"]; found {
return s.NewCounterWithTags(name, tags)
}
s.validateTags(tags)
return s.newCounterWithTagSet(name, tagspkg.TagSet(nil).MergePerInstanceTags(tags))
}
func (s *statStore) newGauge(serializedName string) *gauge {
if v, ok := s.gauges.Load(serializedName); ok {
return v.(*gauge)
}
g := new(gauge)
if v, loaded := s.gauges.LoadOrStore(serializedName, g); loaded {
return v.(*gauge)
}
return g
}
func (s *statStore) NewGauge(name string) Gauge {
return s.newGauge(name)
}
func (s *statStore) NewGaugeWithTags(name string, tags map[string]string) Gauge {
s.validateTags(tags)
return s.newGauge(tagspkg.SerializeTags(name, tags))
}
func (s *statStore) newGaugeWithTagSet(name string, tags tagspkg.TagSet) Gauge {
return s.newGauge(tags.Serialize(name))
}
func (s *statStore) NewPerInstanceGauge(name string, tags map[string]string) Gauge {
if len(tags) == 0 {
return s.NewGaugeWithTags(name, emptyPerInstanceTags)
}
if _, found := tags["_f"]; found {
return s.NewGaugeWithTags(name, tags)
}
s.validateTags(tags)
return s.newGaugeWithTagSet(name, tagspkg.TagSet(nil).MergePerInstanceTags(tags))
}
func (s *statStore) newTimer(serializedName string, base time.Duration) *timer {
if v, ok := s.timers.Load(serializedName); ok {
return v.(*timer)
}
t := &timer{name: serializedName, sink: s.sink, base: base}
if v, loaded := s.timers.LoadOrStore(serializedName, t); loaded {
return v.(*timer)
}
return t
}
func (s *statStore) NewMilliTimer(name string) Timer {
return s.newTimer(name, time.Millisecond)
}
func (s *statStore) NewMilliTimerWithTags(name string, tags map[string]string) Timer {
s.validateTags(tags)
return s.newTimer(tagspkg.SerializeTags(name, tags), time.Millisecond)
}
func (s *statStore) NewTimer(name string) Timer {
return s.newTimer(name, time.Microsecond)
}
func (s *statStore) NewTimerWithTags(name string, tags map[string]string) Timer {
s.validateTags(tags)
return s.newTimer(tagspkg.SerializeTags(name, tags), time.Microsecond)
}
func (s *statStore) newTimerWithTagSet(name string, tags tagspkg.TagSet, base time.Duration) Timer {
return s.newTimer(tags.Serialize(name), base)
}
func (s *statStore) NewPerInstanceTimer(name string, tags map[string]string) Timer {
if len(tags) == 0 {
return s.NewTimerWithTags(name, emptyPerInstanceTags)
}
if _, found := tags["_f"]; found {
return s.NewTimerWithTags(name, tags)
}
s.validateTags(tags)
return s.newTimerWithTagSet(name, tagspkg.TagSet(nil).MergePerInstanceTags(tags), time.Microsecond)
}
func (s *statStore) NewPerInstanceMilliTimer(name string, tags map[string]string) Timer {
if len(tags) == 0 {
return s.NewMilliTimerWithTags(name, emptyPerInstanceTags)
}
if _, found := tags["_f"]; found {
return s.NewMilliTimerWithTags(name, tags)
}
s.validateTags(tags)
return s.newTimerWithTagSet(name, tagspkg.TagSet(nil).MergePerInstanceTags(tags), time.Millisecond)
}
type subScope struct {
registry *statStore
name string
tags tagspkg.TagSet // read-only and may be shared by multiple subScopes
}
func newSubScope(registry *statStore, name string, tags map[string]string) *subScope {
return &subScope{registry: registry, name: name, tags: tagspkg.NewTagSet(tags)}
}
func (s *subScope) Scope(name string) Scope {
return s.ScopeWithTags(name, nil)
}
func (s *subScope) ScopeWithTags(name string, tags map[string]string) Scope {
s.registry.validateTags(tags)
return &subScope{
registry: s.registry,
name: joinScopes(s.name, name),
tags: s.tags.MergeTags(tags),
}
}
func (s *subScope) Store() Store {
return s.registry
}
func (s *subScope) NewCounter(name string) Counter {
return s.NewCounterWithTags(name, nil)
}
func (s *subScope) NewCounterWithTags(name string, tags map[string]string) Counter {
return s.registry.newCounterWithTagSet(joinScopes(s.name, name), s.tags.MergeTags(tags))
}
func (s *subScope) NewPerInstanceCounter(name string, tags map[string]string) Counter {
return s.registry.newCounterWithTagSet(joinScopes(s.name, name),
s.tags.MergePerInstanceTags(tags))
}
func (s *subScope) NewGauge(name string) Gauge {
return s.NewGaugeWithTags(name, nil)
}
func (s *subScope) NewGaugeWithTags(name string, tags map[string]string) Gauge {
return s.registry.newGaugeWithTagSet(joinScopes(s.name, name), s.tags.MergeTags(tags))
}
func (s *subScope) NewPerInstanceGauge(name string, tags map[string]string) Gauge {
return s.registry.newGaugeWithTagSet(joinScopes(s.name, name),
s.tags.MergePerInstanceTags(tags))
}
func (s *subScope) NewTimer(name string) Timer {
return s.NewTimerWithTags(name, nil)
}
func (s *subScope) NewTimerWithTags(name string, tags map[string]string) Timer {
return s.registry.newTimerWithTagSet(joinScopes(s.name, name), s.tags.MergeTags(tags), time.Microsecond)
}
func (s *subScope) NewPerInstanceTimer(name string, tags map[string]string) Timer {
return s.registry.newTimerWithTagSet(joinScopes(s.name, name),
s.tags.MergePerInstanceTags(tags), time.Microsecond)
}
func (s *subScope) NewMilliTimer(name string) Timer {
return s.NewMilliTimerWithTags(name, nil)
}
func (s *subScope) NewMilliTimerWithTags(name string, tags map[string]string) Timer {
return s.registry.newTimerWithTagSet(joinScopes(s.name, name), s.tags.MergeTags(tags), time.Millisecond)
}
func (s *subScope) NewPerInstanceMilliTimer(name string, tags map[string]string) Timer {
s.registry.validateTags(tags)
return s.registry.newTimerWithTagSet(joinScopes(s.name, name),
s.tags.MergePerInstanceTags(tags), time.Millisecond)
}
func joinScopes(parent, child string) string {
return parent + "." + child
}