forked from tracer/tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.go
93 lines (79 loc) · 1.85 KB
/
sampler.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
package tracer
import (
"math/rand"
"sync"
"time"
)
// A Sampler determines whether a span should be sampled or not by
// returning true or false.
type Sampler interface {
Sample(id uint64) bool
}
type constSampler struct {
decision bool
}
// NewConstSampler returns a constant sampler that always returns the
// same decision.
func NewConstSampler(decision bool) Sampler {
return constSampler{decision}
}
// Sample implements the Sampler interface.
func (c constSampler) Sample(uint64) bool {
return c.decision
}
type probabilisticSampler struct {
chance float64
rng *rand.Rand
}
// NewProbabilisticSampler returns a sampler that samples spans with a
// certain chance, which should be in [0, 1].
func NewProbabilisticSampler(chance float64) Sampler {
return probabilisticSampler{chance, rand.New(rand.NewSource(time.Now().UnixNano()))}
}
// Sample implements the Sampler interface.
func (p probabilisticSampler) Sample(uint64) bool {
return p.rng.Float64() < p.chance
}
type rateLimiter struct {
mu sync.Mutex
rate int
tokens int
t time.Time
nowFn func() time.Time
}
func newRateLimiter(rate int) *rateLimiter {
return &rateLimiter{
rate: rate,
tokens: rate,
t: time.Now().Add(time.Second),
nowFn: time.Now,
}
}
func (r *rateLimiter) Allow() bool {
r.mu.Lock()
defer r.mu.Unlock()
add := int((float64(r.nowFn().Sub(r.t).Nanoseconds()/int64(time.Millisecond)) / 1000) * float64(r.rate))
if add > 0 {
r.tokens = r.tokens + add
if r.tokens > r.rate {
r.tokens = r.rate
}
r.t = r.nowFn()
}
if r.tokens > 0 {
r.tokens--
return true
}
return false
}
type rateSampler struct {
l *rateLimiter
}
// NewRateSampler returns a sampler that samples up to n samples per
// second.
func NewRateSampler(n int) Sampler {
return rateSampler{newRateLimiter(n)}
}
func (r rateSampler) Sample(uint64) bool {
return r.l.Allow()
}