-
Notifications
You must be signed in to change notification settings - Fork 10
/
totalthroughput.go
183 lines (160 loc) · 5.58 KB
/
totalthroughput.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
package dynsampler
import (
"fmt"
"math"
"sync"
"time"
)
// TotalThroughput implements Sampler and attempts to meet a goal of a fixed
// number of events per second sent to Honeycomb.
//
// If your key space is sharded across different servers, this is a good method
// for making sure each server sends roughly the same volume of content to
// Honeycomb. It performs poorly when the active keyspace is very large.
//
// GoalThroughputSec * ClearFrequencyDuration (in seconds) defines the upper
// limit of the number of keys that can be reported and stay under the goal, but
// with that many keys, you'll only get one event per key per ClearFrequencySec,
// which is very coarse. You should aim for at least 1 event per key per sec to
// 1 event per key per 10sec to get reasonable data. In other words, the number
// of active keys should be less than 10*GoalThroughputSec.
type TotalThroughput struct {
// DEPRECATED -- use ClearFrequencyDuration.
// ClearFrequencySec is how often the counters reset in seconds.
ClearFrequencySec int
// ClearFrequencyDuration is how often the counters reset as a Duration.
// Note that either this or ClearFrequencySec can be specified, but not both.
// If neither one is set, the default is 30s.
ClearFrequencyDuration time.Duration
// GoalThroughputPerSec is the target number of events to send per second.
// Sample rates are generated to squash the total throughput down to match the
// goal throughput. Actual throughput may exceed goal throughput. default 100
GoalThroughputPerSec int
// MaxKeys, if greater than 0, limits the number of distinct keys used to build
// the sample rate map within the interval defined by `ClearFrequencySec`. Once
// MaxKeys is reached, new keys will not be included in the sample rate map, but
// existing keys will continue to be be counted.
MaxKeys int
savedSampleRates map[string]int
currentCounts map[string]int
done chan struct{}
lock sync.Mutex
// metrics
requestCount int64
eventCount int64
}
// Ensure we implement the sampler interface
var _ Sampler = (*TotalThroughput)(nil)
func (t *TotalThroughput) Start() error {
// apply defaults
if t.ClearFrequencyDuration != 0 && t.ClearFrequencySec != 0 {
return fmt.Errorf("the ClearFrequencySec configuration value is deprecated; use only ClearFrequencyDuration")
}
if t.ClearFrequencyDuration == 0 && t.ClearFrequencySec == 0 {
t.ClearFrequencyDuration = 30 * time.Second
} else if t.ClearFrequencySec != 0 {
t.ClearFrequencyDuration = time.Duration(t.ClearFrequencySec) * time.Second
}
if t.GoalThroughputPerSec == 0 {
t.GoalThroughputPerSec = 100
}
// initialize internal variables
t.savedSampleRates = make(map[string]int)
t.currentCounts = make(map[string]int)
t.done = make(chan struct{})
// spin up calculator
go func() {
ticker := time.NewTicker(t.ClearFrequencyDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
t.updateMaps()
case <-t.done:
return
}
}
}()
return nil
}
func (t *TotalThroughput) Stop() error {
close(t.done)
return nil
}
// updateMaps calculates a new saved rate map based on the contents of the
// counter map
func (t *TotalThroughput) updateMaps() {
// make a local copy of the sample counters for calculation
t.lock.Lock()
tmpCounts := t.currentCounts
t.currentCounts = make(map[string]int)
t.lock.Unlock()
// short circuit if no traffic
numKeys := len(tmpCounts)
if numKeys == 0 {
// no traffic the last 30s. clear the result map
t.lock.Lock()
defer t.lock.Unlock()
t.savedSampleRates = make(map[string]int)
return
}
// figure out our target throughput per key over ClearFrequencyDuration
totalGoalThroughput := float64(t.GoalThroughputPerSec) * t.ClearFrequencyDuration.Seconds()
// split the total throughput equally across the number of keys.
throughputPerKey := float64(totalGoalThroughput) / float64(numKeys)
// for each key, calculate sample rate by dividing counted events by the
// desired number of events
newSavedSampleRates := make(map[string]int)
for k, v := range tmpCounts {
rate := int(math.Max(1, (float64(v) / float64(throughputPerKey))))
newSavedSampleRates[k] = rate
}
// save newly calculated sample rates
t.lock.Lock()
defer t.lock.Unlock()
t.savedSampleRates = newSavedSampleRates
}
// GetSampleRate takes a key and returns the appropriate sample rate for that
// key.
func (t *TotalThroughput) GetSampleRate(key string) int {
return t.GetSampleRateMulti(key, 1)
}
// GetSampleRateMulti takes a key representing count spans and returns the
// appropriate sample rate for that key.
func (t *TotalThroughput) GetSampleRateMulti(key string, count int) int {
t.lock.Lock()
defer t.lock.Unlock()
t.requestCount++
t.eventCount += int64(count)
// Enforce MaxKeys limit on the size of the map
if t.MaxKeys > 0 {
// If a key already exists, increment it. If not, but we're under the limit, store a new key
if _, found := t.currentCounts[key]; found || len(t.currentCounts) < t.MaxKeys {
t.currentCounts[key] += count
}
} else {
t.currentCounts[key] += count
}
if rate, found := t.savedSampleRates[key]; found {
return rate
}
return 1
}
// SaveState is not implemented
func (t *TotalThroughput) SaveState() ([]byte, error) {
return nil, nil
}
// LoadState is not implemented
func (t *TotalThroughput) LoadState(state []byte) error {
return nil
}
func (t *TotalThroughput) GetMetrics(prefix string) map[string]int64 {
t.lock.Lock()
defer t.lock.Unlock()
mets := map[string]int64{
prefix + "request_count": t.requestCount,
prefix + "event_count": t.eventCount,
prefix + "keyspace_size": int64(len(t.currentCounts)),
}
return mets
}