-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstatic.go
75 lines (62 loc) · 1.7 KB
/
static.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
package dynsampler
import "sync"
// Static implements Sampler with a static mapping for sample rates. This is
// useful if you have a known set of keys that you want to sample at specific
// rates and apply a default to everything else.
type Static struct {
// Rates is the set of sample rates to use
Rates map[string]int
// Default is the value to use if the key is not whitelisted in Rates
Default int
lock sync.Mutex
// metrics
requestCount int64
eventCount int64
}
// Ensure we implement the sampler interface
var _ Sampler = (*Static)(nil)
// Start initializes the static dynsampler
func (s *Static) Start() error {
if s.Default == 0 {
s.Default = 1
}
return nil
}
func (s *Static) Stop() error {
return nil
}
// GetSampleRate takes a key and returns the appropriate sample rate for that
// key.
func (s *Static) GetSampleRate(key string) int {
return s.GetSampleRateMulti(key, 1)
}
// GetSampleRateMulti takes a key representing count spans and returns the
// appropriate sample rate for that key.
func (s *Static) GetSampleRateMulti(key string, count int) int {
s.lock.Lock()
defer s.lock.Unlock()
s.requestCount++
s.eventCount += int64(count)
if rate, found := s.Rates[key]; found {
return rate
}
return s.Default
}
// SaveState is not implemented
func (s *Static) SaveState() ([]byte, error) {
return nil, nil
}
// LoadState is not implemented
func (s *Static) LoadState(state []byte) error {
return nil
}
func (s *Static) GetMetrics(prefix string) map[string]int64 {
s.lock.Lock()
defer s.lock.Unlock()
mets := map[string]int64{
prefix + "request_count": s.requestCount,
prefix + "event_count": s.eventCount,
prefix + "keyspace_size": int64(len(s.Rates)),
}
return mets
}