forked from pinpoint-apm/pinpoint-go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampler.go
163 lines (140 loc) · 2.92 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
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
package pinpoint
import (
"golang.org/x/time/rate"
"sync/atomic"
"time"
)
const (
samplingMaxPercentRate = 100 * 100
)
type sampler interface {
isSampled() bool
}
type rateSampler struct {
rate uint64
counter uint64
}
func newRateSampler(rate int) *rateSampler {
if rate < 0 {
rate = 0
}
return &rateSampler{
rate: uint64(rate),
counter: 0,
}
}
func (s *rateSampler) isSampled() bool {
if s.rate == 0 {
return false
}
samplingCount := atomic.AddUint64(&s.counter, 1)
isSampled := samplingCount % s.rate
return isSampled == 0
}
type percentSampler struct {
rate uint64
counter uint64
}
func newPercentSampler(percent float64) *percentSampler {
if percent < 0 {
percent = 0
} else if percent < 0.01 {
percent = 0.01
} else if percent > 100 {
percent = 100
}
return &percentSampler{
rate: uint64(percent * 100),
counter: 0,
}
}
func (s *percentSampler) isSampled() bool {
if s.rate == 0 {
return false
}
samplingCount := atomic.AddUint64(&s.counter, s.rate)
r := samplingCount % samplingMaxPercentRate
return r < s.rate
}
type traceSampler interface {
isNewSampled() bool
isContinueSampled() bool
}
type basicTraceSampler struct {
baseSampler sampler
}
func newBasicTraceSampler(base sampler) *basicTraceSampler {
return &basicTraceSampler{
baseSampler: base,
}
}
func (s *basicTraceSampler) isNewSampled() bool {
sampled := s.baseSampler.isSampled()
if sampled {
incrSampleNew()
} else {
incrUnSampleNew()
}
return sampled
}
func (s *basicTraceSampler) isContinueSampled() bool {
incrSampleCont()
return true
}
type throughputLimitTraceSampler struct {
baseSampler sampler
newSampleLimiter *rate.Limiter
continueSampleLimiter *rate.Limiter
}
func newThroughputLimitTraceSampler(base sampler, newTps int, continueTps int) *throughputLimitTraceSampler {
var (
newLimiter *rate.Limiter
contLimiter *rate.Limiter
)
if newTps > 0 {
newLimiter = rate.NewLimiter(per(newTps, time.Second), 1)
}
if continueTps > 0 {
contLimiter = rate.NewLimiter(per(continueTps, time.Second), 1)
}
return &throughputLimitTraceSampler{
baseSampler: base,
newSampleLimiter: newLimiter,
continueSampleLimiter: contLimiter,
}
}
func per(throughput int, d time.Duration) rate.Limit {
return rate.Every(d / time.Duration(throughput))
}
func (s *throughputLimitTraceSampler) isNewSampled() bool {
sampled := s.baseSampler.isSampled()
if sampled {
if s.newSampleLimiter != nil {
sampled = s.newSampleLimiter.Allow()
if sampled {
incrSampleNew()
} else {
incrSkipNew()
}
} else {
incrSampleNew()
}
} else {
incrUnSampleNew()
}
return sampled
}
func (s *throughputLimitTraceSampler) isContinueSampled() bool {
sampled := true
if s.continueSampleLimiter != nil {
sampled = s.continueSampleLimiter.Allow()
if sampled {
incrSampleCont()
} else {
incrSkipCont()
}
} else {
incrSampleCont()
}
return sampled
}