-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatstash.go
663 lines (564 loc) · 18.8 KB
/
statstash.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Copyright 2014-2015 pendo.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package statstash is a service used to collect statistics
// for a Google App Engine project and package them up to a backend server.
package statstash
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"math"
"math/rand"
"sort"
"strconv"
"time"
"github.com/pendo-io/appwrap"
)
const (
dsKindStatConfig = "StatConfig"
scTypeTiming = "timing"
scTypeGauge = "gauge"
scTypeCounter = "counter"
defaultAggregationPeriod = time.Duration(5 * time.Minute)
)
var ErrStatFlushTooSoon = errors.New("Too Soon to Flush Stats")
var ErrStatNotSampled = errors.New("Skipped sample because sample rate given")
type ErrStatDropped struct {
typ string
name string
source string
t time.Time
value float64
err error
}
func NewErrStatDropped(typ, name, source string, t time.Time, value float64, err error) error {
return &ErrStatDropped{typ, name, source, t, value, err}
}
func (e *ErrStatDropped) Error() string {
return fmt.Sprintf("Stat not stored: [type/name/source: %s/%s/%s, time: %s, value: %f]. Reason: %s",
e.typ, e.name, e.source, e.t, e.value, e.err)
}
type StatConfig struct {
Name string `datastore:",noindex" json:"name"`
Source string `datastore:",noindex" json:"source"`
Type string `datastore:",noindex" json:"type"`
LastRead time.Time `json:"lastread"`
}
func (sc StatConfig) String() string {
return fmt.Sprintf("[StatConfig] name=%s, source=%s, type=%s, lastread=%s",
sc.Name, sc.Source, sc.Type, sc.LastRead)
}
func (sc StatConfig) BucketKey(t time.Time, offset int) string {
return fmt.Sprintf("ss-metric:%s-%s-%s-%d", sc.Type, sc.Name, sc.Source, getStartOfFlushPeriod(t, offset).Unix())
}
// StatInterface defines the interface for the application to
type StatInterface interface {
IncrementCounter(name, source string) error
IncrementCounterBy(name, source string, delta int64) error
RecordGauge(name, source string, value float64) error
RecordTiming(name, source string, value, sampleRate float64) error
UpdateBackend(periodStart time.Time, flusher StatsFlusher, cfg *FlusherConfig, force bool) error
}
func NewNullStatImplementation() StatInterface {
return NullStatImplementation{}
}
type NullStatImplementation struct {
}
func (m NullStatImplementation) IncrementCounter(name, source string) error { return nil }
func (m NullStatImplementation) IncrementCounterBy(name, source string, delta int64) error {
return nil
}
func (m NullStatImplementation) RecordGauge(name, source string, value float64) error { return nil }
func (m NullStatImplementation) RecordTiming(name, source string, value, sampleRate float64) error {
return nil
}
func (m NullStatImplementation) UpdateBackend(periodStart time.Time, flusher StatsFlusher, cfg *FlusherConfig, force bool) error {
return nil
}
func NewStatInterface(log appwrap.Logging, ds appwrap.Datastore, cache appwrap.Memcache, debug bool) StatInterface {
return StatImplementation{
log: log,
ds: ds,
cache: cache,
randGen: rand.New(rand.NewSource(time.Now().UnixNano())),
debug: debug,
}
}
type StatImplementation struct {
log appwrap.Logging
ds appwrap.Datastore
cache appwrap.Memcache
randGen *rand.Rand
debug bool
}
func (s StatImplementation) IncrementCounter(name, source string) error {
return s.IncrementCounterBy(name, source, 1)
}
func (s StatImplementation) IncrementCounterBy(name, source string, delta int64) error {
s.debugf("Increment counter/%s/%s: delta=%d", name, source, delta)
bucketKey, err := s.getBucketKey(scTypeCounter, name, source, time.Now())
if err != nil {
return err
}
s.log.Debugf("record bucketKey: %s", bucketKey)
if _, err = s.cache.IncrementExisting(bucketKey, delta); err == appwrap.ErrCacheMiss {
cachedItem := &appwrap.CacheItem{
Value: []byte(strconv.FormatInt(delta, 10)),
Key: bucketKey,
Expiration: time.Duration(2 * defaultAggregationPeriod),
}
err = s.cache.Add(cachedItem)
} else if err != nil {
s.log.Warningf("Failed to increment %s delta %d", bucketKey, delta)
}
return err
}
func (s StatImplementation) RecordGauge(name, source string, value float64) error {
return s.recordGaugeOrTiming(scTypeGauge, name, source, value, 1.0)
}
func (s StatImplementation) RecordTiming(name, source string, value, sampleRate float64) error {
return s.recordGaugeOrTiming(scTypeTiming, name, source, value, sampleRate)
}
func (s StatImplementation) UpdateBackend(periodStart time.Time, flusher StatsFlusher, flushConfig *FlusherConfig, force bool) error {
if !force {
lastFlushedPeriod := s.getLastPeriodFlushed()
if periodStart.Sub(lastFlushedPeriod) < defaultAggregationPeriod {
s.log.Warningf("Refusing to update backend since it's too soon (last flush period %s, current period requested %s, aggregation period %s)", lastFlushedPeriod, periodStart, defaultAggregationPeriod)
return ErrStatFlushTooSoon
}
}
cfgMap, err := s.getActiveConfigs(periodStart, 0)
if err != nil {
s.log.Errorf("Failed to get active buckets when updating backend: %s", err)
return err
}
if len(cfgMap) == 0 {
return nil // nothing to do
}
bucketKeys := make([]string, 0, len(cfgMap))
for k := range cfgMap {
bucketKeys = append(bucketKeys, k)
}
if itemMap, err := s.cache.GetMulti(bucketKeys); err != nil {
s.log.Errorf("Failed to fetch items from memcache when updating backend: %s", err)
} else {
// Get our data from memcache in one go
data := make([]interface{}, 0, len(itemMap))
for k, item := range itemMap {
var datum interface{}
cfgItem := cfgMap[k]
switch cfgItem.Type {
case scTypeTiming, scTypeGauge:
var gm []float64
if err := s.gobUnmarshal(item.Value, &gm); err != nil {
s.log.Errorf("Bad data found in memcache: key %s, error: %s", k, err)
continue
}
if len(gm) == 0 {
panic("Something went terribly wrong; empty list cached!")
}
if cfgItem.Type == scTypeTiming {
var median, sum, sumSquares float64
// sort our list
sort.Float64s(gm)
count := len(gm)
min := gm[0]
max := gm[count-1]
if count == 1 {
median = gm[0]
} else if count%2 == 0 {
median = (gm[(count/2)-1] + gm[count/2]) / 2.0
} else {
median = gm[(count / 2)]
}
const ninthDecile = 0.9
const threeNinesPercentile = 0.999
ninthdecileCount, ninthdecileValue := getPercentileCount(gm, ninthDecile, count)
threeNinesCount, threeNinesValue := getPercentileCount(gm, threeNinesPercentile, count)
ninthdecileSum := 0.0
threeNinesSum := 0.0
for i, m := range gm {
if i < ninthdecileCount {
ninthdecileSum += m
}
if i < threeNinesCount {
threeNinesSum += m
}
sum += m
sumSquares += math.Pow(m, 2.0)
}
datum = StatDataTiming{
StatConfig: cfgItem,
Count: count,
Min: min,
Max: max,
Sum: sum,
SumSquares: sumSquares,
Median: median,
NinthDecileCount: ninthdecileCount,
NinthDecileSum: ninthdecileSum,
NinthDecileValue: ninthdecileValue,
ThreeNinesCount: threeNinesCount,
ThreeNinesSum: threeNinesSum,
ThreeNinesValue: threeNinesValue,
}
} else {
datum = StatDataGauge{StatConfig: cfgItem, Value: gm[0]}
}
case scTypeCounter:
count, _ := strconv.ParseUint(string(item.Value), 10, 64)
datum = StatDataCounter{StatConfig: cfgItem, Count: count}
default:
panic("If this happened, things are horribly wrong.")
}
data = append(data, datum)
}
if len(data) > 0 {
// Now flush to the backend
if err := flusher.Flush(data, flushConfig); err != nil {
s.log.Errorf("Failed to flush to backend: %s", err)
return err
} else {
s.updateLastPeriodFlushed(periodStart)
}
}
}
return nil
}
func getPercentileCount(gm []float64, percentile float64, count int) (int, float64) {
ninthdecileCount := int(math.Ceil(percentile * float64(count)))
ninthdecileValue := gm[ninthdecileCount-1]
return ninthdecileCount, ninthdecileValue
}
func (s StatImplementation) Purge() error {
sc, err := s.getAllConfigs()
if err != nil {
return err
}
if len(sc) == 0 {
return nil // nothing to do
}
now := time.Now()
dsKeys := make([]*appwrap.DatastoreKey, 0, len(sc))
memcacheKeys := make([]string, 0, len(sc))
for _, cfg := range sc {
dsKeys = append(dsKeys, s.getStatConfigDatastoreKey(cfg.Type, cfg.Name, cfg.Source))
memcacheKeys = append(memcacheKeys, cfg.BucketKey(now, 0))
memcacheKeys = append(memcacheKeys, cfg.BucketKey(now, -1))
}
if err := s.ds.DeleteMulti(dsKeys); err != nil {
s.log.Errorf("Stats: purge datastore failed: %s", err)
return err
}
s.cache.DeleteMulti(memcacheKeys)
return nil
}
func (s StatImplementation) getAllConfigs() ([]StatConfig, error) {
q := s.ds.NewQuery(dsKindStatConfig)
var cfgs []StatConfig
_, err := q.GetAll(&cfgs)
return cfgs, err
}
func (s StatImplementation) getActiveConfigs(at time.Time, offset int) (map[string]StatConfig, error) {
statConfigs := make(map[string]StatConfig)
var finalError error
cutoffTime := at.Add(time.Duration(time.Hour * 24 * -2))
q := s.ds.NewQuery(dsKindStatConfig).Filter("LastRead >", cutoffTime)
iter := q.Run()
for {
var sc StatConfig
_, err := iter.Next(&sc)
if err == appwrap.DatastoreDone {
break // end of iteration
} else if err != nil {
s.log.Warningf("Failed iterating stat config items to get active buckets: %s", err)
finalError = err
break
}
bucketKey := sc.BucketKey(at, offset)
statConfigs[bucketKey] = sc
}
s.debugf("Found %d stat configs (cutoff time %s)", len(statConfigs), cutoffTime)
return statConfigs, finalError
}
func (s StatImplementation) getBucketKey(typ, name, source string, at time.Time) (string, error) {
statConfig, err := s.getStatConfig(typ, name, source)
if err != nil {
return "", err
}
return statConfig.BucketKey(at, 0), nil
}
func (s StatImplementation) getStatConfigKeyName(typ, name, source string) string {
return fmt.Sprintf("%s-%s-%s", typ, name, source)
}
func (s StatImplementation) getStatConfigMemcacheKey(typ, name, source string) string {
return fmt.Sprintf("ss-conf:%s", s.getStatConfigKeyName(typ, name, source))
}
func (s StatImplementation) getStatConfigDatastoreKey(typ, name, source string) *appwrap.DatastoreKey {
return s.ds.NewKey(dsKindStatConfig, s.getStatConfigKeyName(typ, name, source), 0, nil)
}
func (s StatImplementation) getStatConfig(typ, name, source string) (StatConfig, error) {
var sc StatConfig
// First, query memcache
if item, err := s.cache.Get(s.getStatConfigMemcacheKey(typ, name, source)); err == nil {
if err := s.gobUnmarshal(item.Value, &sc); err != nil {
return StatConfig{}, err
} else {
return sc, nil
}
}
k := s.getStatConfigDatastoreKey(typ, name, source)
now := time.Now()
cache := true
// Now query datastore
if err := s.ds.Get(k, &sc); err != nil && err != appwrap.ErrNoSuchEntity {
return StatConfig{}, err
} else if err == appwrap.ErrNoSuchEntity {
sc.Name = name
sc.Source = source
sc.Type = typ
}
sc.LastRead = now
// Store item in datastore if it needed the update
if _, err := s.ds.Put(k, &sc); err != nil {
s.log.Warningf("Failed to update StatConfig %s: %s", sc, err)
cache = false
}
// Only attempt adding if the update was needed and succeeded
if cache {
if b, err := s.gobMarshal(&sc); err != nil {
s.log.Warningf("Failed to encode stat config item into memcache: %s", err)
return StatConfig{}, nil
} else {
s.cache.Add(&appwrap.CacheItem{
Key: s.getStatConfigMemcacheKey(typ, name, source),
Value: b,
Expiration: time.Duration(24 * time.Hour),
})
}
}
return sc, nil
}
func (s StatImplementation) peekCounter(name, source string, at time.Time) (uint64, error) {
bucketKey, err := s.getBucketKey(scTypeCounter, name, source, time.Now())
if err != nil {
return uint64(0), err
}
s.log.Debugf("peek bucketKey: %s", bucketKey)
if item, err := s.cache.Get(bucketKey); err == nil {
return strconv.ParseUint(string(item.Value), 10, 64)
} else {
return uint64(0), err
}
}
func (s StatImplementation) peekGauge(name, source string, at time.Time) ([]float64, error) {
bucketKey, err := s.getBucketKey(scTypeGauge, name, source, time.Now())
if err != nil {
return nil, err
}
var gm []float64
if item, err := s.cache.Get(bucketKey); err != nil {
return nil, err
} else {
if s.gobUnmarshal(item.Value, &gm); err != nil {
s.log.Errorf("Error decoding gauge values: %s", err)
return nil, err
}
return gm, nil
}
}
func (s StatImplementation) peekTiming(name, source string, at time.Time) ([]float64, error) {
bucketKey, err := s.getBucketKey(scTypeTiming, name, source, time.Now())
if err != nil {
return nil, err
}
var gm []float64
if item, err := s.cache.Get(bucketKey); err != nil {
return nil, err
} else {
if s.gobUnmarshal(item.Value, &gm); err != nil {
s.log.Errorf("Error decoding timing values: %s", err)
return nil, err
}
return gm, nil
}
}
func (s StatImplementation) recordGaugeOrTiming(typ, name, source string, value, sampleRate float64) error {
s.debugf("Recording %s/%s/%s: value=%f, samplerate=%f)", typ, name, source, value, sampleRate)
if sampleRate < 1.0 && s.randGen.Float64() > sampleRate {
s.debugf("Not recording value due to sampling rate")
return ErrStatNotSampled // do nothing here, as we are sampling
}
now := time.Now()
bucketKey, err := s.getBucketKey(typ, name, source, now)
if err != nil {
wrappedErr := NewErrStatDropped(typ, name, source, now, value, err)
s.log.Warningf("%s (getting bucket key)", wrappedErr)
return wrappedErr
}
s.log.Debugf("record bucketKey: %s", bucketKey)
var cached []float64
cachedItem, err := s.cache.Get(bucketKey)
if err == appwrap.ErrCacheMiss {
cached = make([]float64, 0)
cachedItem = &appwrap.CacheItem{
Key: bucketKey,
Expiration: time.Duration(2 * defaultAggregationPeriod),
}
} else if err != nil {
wrappedErr := NewErrStatDropped(typ, name, source, now, value, err)
s.log.Warningf("%s (getting value from memcache)", wrappedErr)
return wrappedErr
} else {
if s.gobUnmarshal(cachedItem.Value, &cached); err != nil {
wrappedErr := NewErrStatDropped(typ, name, source, now, value, err)
s.log.Warningf("%s (decoding value from memcache)", wrappedErr)
return wrappedErr
}
}
switch typ {
case scTypeTiming:
cached = append(cached, value)
case scTypeGauge:
cached = []float64{value}
}
if b, err := s.gobMarshal(&cached); err != nil {
wrappedErr := NewErrStatDropped(typ, name, source, now, value, err)
s.log.Warningf("%s (failed to encode new value)", wrappedErr)
return wrappedErr
} else {
cachedItem.Value = b
if err := s.cache.Set(cachedItem); err != nil {
wrappedErr := NewErrStatDropped(typ, name, source, now, value, err)
s.log.Warningf("%s (failed to set value)", wrappedErr)
return wrappedErr
}
}
return nil
}
func (s StatImplementation) getLastPeriodFlushed() time.Time {
var lastPeriodFlushed time.Time
if item, err := s.cache.Get("ss-lpf"); err != nil {
return time.Time{}
} else {
if err := s.gobUnmarshal(item.Value, &lastPeriodFlushed); err != nil {
s.log.Errorf("Failed to get last period flushed: %s", err)
return time.Time{}
}
}
s.log.Debugf("lastPeriodFlushed %s", lastPeriodFlushed)
return lastPeriodFlushed
}
func (s StatImplementation) updateLastPeriodFlushed(lastPeriodFlushed time.Time) error {
if b, err := s.gobMarshal(&lastPeriodFlushed); err != nil {
s.log.Errorf("Failed to set last period flushed: %s", err)
return err
} else {
s.log.Debugf("FOOOO")
return s.cache.Set(&appwrap.CacheItem{
Key: "ss-lpf",
Value: b,
})
}
}
func getStartOfFlushPeriod(at time.Time, offset int) time.Time {
startOfPeriod := at.Truncate(defaultAggregationPeriod)
if offset != 0 {
startOfPeriod = startOfPeriod.Add(time.Duration(offset) * defaultAggregationPeriod)
}
return startOfPeriod
}
func (s StatImplementation) debugf(format string, args ...interface{}) {
if s.debug {
s.log.Debugf(format, args...)
}
}
func (s StatImplementation) gobMarshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s StatImplementation) gobUnmarshal(data []byte, v interface{}) error {
return gob.NewDecoder(bytes.NewBuffer(data)).Decode(v)
}
type StatDataCounter struct {
StatConfig
Count uint64
}
func (dc StatDataCounter) String() string {
return fmt.Sprintf("[Counter: name=%s, source=%s] Value: %d",
dc.Name, dc.Source, dc.Count)
}
type StatDataTiming struct {
StatConfig
Count int
Min float64
Max float64
Sum float64
SumSquares float64
Median float64
NinthDecileValue float64
NinthDecileSum float64
NinthDecileCount int
ThreeNinesValue float64
ThreeNinesSum float64
ThreeNinesCount int
}
func (dt StatDataTiming) String() string {
return fmt.Sprintf("[Timing: name=%s, source=%s] Count: %d, Min: %f, Max: %f, Sum: %f, SumSquares: %f, Median: %f, 90th percentile (count: %d, value: %f, sum: %f), 99.9th percentile (count: %d, value: %f, sum: %f):",
dt.Name, dt.Source, dt.Count, dt.Min, dt.Max, dt.Sum, dt.SumSquares, dt.Median, dt.NinthDecileCount, dt.NinthDecileValue, dt.NinthDecileSum, dt.ThreeNinesCount, dt.ThreeNinesValue, dt.ThreeNinesSum)
}
type StatDataGauge struct {
StatConfig
Value float64
}
func (dg StatDataGauge) String() string {
return fmt.Sprintf("[Gauge: name=%s, source=%s] Value: %f",
dg.Name, dg.Source, dg.Value)
}
// StatsFlusher is an interface used to flush stats to various locations
type StatsFlusher interface {
Flush(data []interface{}, cfg *FlusherConfig) error
}
type FlusherConfig struct {
Username string
Password string
ApiKey string
}
// LogOnlyStatsFlusher is used to "flush" stats for testing and development.
// Stats that are flushed are logged only.
type LogOnlyStatsFlusher struct {
log appwrap.Logging
}
func NewLogOnlyStatsFlusher(log appwrap.Logging) StatsFlusher {
return LogOnlyStatsFlusher{log}
}
func (f LogOnlyStatsFlusher) Flush(data []interface{}, cfg *FlusherConfig) error {
for i := range data {
var datum interface{}
switch data[i].(type) {
case StatDataCounter:
datum = data[i].(StatDataCounter)
case StatDataTiming:
datum = data[i].(StatDataTiming)
case StatDataGauge:
datum = data[i].(StatDataGauge)
}
f.log.Infof("%s", datum)
}
return nil
}