/
sampler.go
453 lines (409 loc) · 13.1 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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package tracer
import (
"encoding/json"
"fmt"
"io"
"math"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"golang.org/x/time/rate"
)
// Sampler is the generic interface of any sampler. It must be safe for concurrent use.
type Sampler interface {
// Sample returns true if the given span should be sampled.
Sample(span Span) bool
}
// RateSampler is a sampler implementation which randomly selects spans using a
// provided rate. For example, a rate of 0.75 will permit 75% of the spans.
// RateSampler implementations should be safe for concurrent use.
type RateSampler interface {
Sampler
// Rate returns the current sample rate.
Rate() float64
// SetRate sets a new sample rate.
SetRate(rate float64)
}
// rateSampler samples from a sample rate.
type rateSampler struct {
sync.RWMutex
rate float64
}
// NewAllSampler is a short-hand for NewRateSampler(1). It is all-permissive.
func NewAllSampler() RateSampler { return NewRateSampler(1) }
// NewRateSampler returns an initialized RateSampler with a given sample rate.
func NewRateSampler(rate float64) RateSampler {
return &rateSampler{rate: rate}
}
// Rate returns the current rate of the sampler.
func (r *rateSampler) Rate() float64 {
r.RLock()
defer r.RUnlock()
return r.rate
}
// SetRate sets a new sampling rate.
func (r *rateSampler) SetRate(rate float64) {
r.Lock()
r.rate = rate
r.Unlock()
}
// constants used for the Knuth hashing, same as agent.
const knuthFactor = uint64(1111111111111111111)
// Sample returns true if the given span should be sampled.
func (r *rateSampler) Sample(spn ddtrace.Span) bool {
if r.rate == 1 {
// fast path
return true
}
s, ok := spn.(*span)
if !ok {
return false
}
r.RLock()
defer r.RUnlock()
return sampledByRate(s.TraceID, r.rate)
}
// sampledByRate verifies if the number n should be sampled at the specified
// rate.
func sampledByRate(n uint64, rate float64) bool {
if rate < 1 {
return n*knuthFactor < uint64(rate*math.MaxUint64)
}
return true
}
// prioritySampler holds a set of per-service sampling rates and applies
// them to spans.
type prioritySampler struct {
mu sync.RWMutex
rates map[string]float64
defaultRate float64
}
func newPrioritySampler() *prioritySampler {
return &prioritySampler{
rates: make(map[string]float64),
defaultRate: 1.,
}
}
// readRatesJSON will try to read the rates as JSON from the given io.ReadCloser.
func (ps *prioritySampler) readRatesJSON(rc io.ReadCloser) error {
var payload struct {
Rates map[string]float64 `json:"rate_by_service"`
}
if err := json.NewDecoder(rc).Decode(&payload); err != nil {
return err
}
rc.Close()
const defaultRateKey = "service:,env:"
ps.mu.Lock()
defer ps.mu.Unlock()
ps.rates = payload.Rates
if v, ok := ps.rates[defaultRateKey]; ok {
ps.defaultRate = v
delete(ps.rates, defaultRateKey)
}
return nil
}
// getRate returns the sampling rate to be used for the given span. Callers must
// guard the span.
func (ps *prioritySampler) getRate(spn *span) float64 {
key := "service:" + spn.Service + ",env:" + spn.Meta[ext.Environment]
ps.mu.RLock()
defer ps.mu.RUnlock()
if rate, ok := ps.rates[key]; ok {
return rate
}
return ps.defaultRate
}
// apply applies sampling priority to the given span. Caller must ensure it is safe
// to modify the span.
func (ps *prioritySampler) apply(spn *span) {
rate := ps.getRate(spn)
if sampledByRate(spn.TraceID, rate) {
spn.SetTag(ext.SamplingPriority, ext.PriorityAutoKeep)
} else {
spn.SetTag(ext.SamplingPriority, ext.PriorityAutoReject)
}
spn.SetTag(keySamplingPriorityRate, rate)
}
// rulesSampler allows a user-defined list of rules to apply to spans.
// These rules can match based on the span's Service, Name or both.
// When making a sampling decision, the rules are checked in order until
// a match is found.
// If a match is found, the rate from that rule is used.
// If no match is found, and the DD_TRACE_SAMPLE_RATE environment variable
// was set to a valid rate, that value is used.
// Otherwise, the rules sampler didn't apply to the span, and the decision
// is passed to the priority sampler.
//
// The rate is used to determine if the span should be sampled, but an upper
// limit can be defined using the DD_TRACE_RATE_LIMIT environment variable.
// Its value is the number of spans to sample per second.
// Spans that matched the rules but exceeded the rate limit are not sampled.
type rulesSampler struct {
rules []SamplingRule // the rules to match spans with
globalRate float64 // a rate to apply when no rules match a span
limiter *rateLimiter // used to limit the volume of spans sampled
}
// newRulesSampler configures a *rulesSampler instance using the given set of rules.
// Invalid rules or environment variable values are tolerated, by logging warnings and then ignoring them.
func newRulesSampler(rules []SamplingRule) *rulesSampler {
return &rulesSampler{
rules: rules,
globalRate: globalSampleRate(),
limiter: newRateLimiter(),
}
}
// samplingRulesFromEnv parses sampling rules from the DD_TRACE_SAMPLING_RULES
// environment variable.
func samplingRulesFromEnv() ([]SamplingRule, error) {
rulesFromEnv := os.Getenv("DD_TRACE_SAMPLING_RULES")
if rulesFromEnv == "" {
return nil, nil
}
jsonRules := []struct {
Service string `json:"service"`
Name string `json:"name"`
Rate json.Number `json:"sample_rate"`
}{}
err := json.Unmarshal([]byte(rulesFromEnv), &jsonRules)
if err != nil {
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)
}
rules := make([]SamplingRule, 0, len(jsonRules))
var errs []string
for i, v := range jsonRules {
if v.Rate == "" {
errs = append(errs, fmt.Sprintf("at index %d: rate not provided", i))
continue
}
rate, err := v.Rate.Float64()
if err != nil {
errs = append(errs, fmt.Sprintf("at index %d: %v", i, err))
continue
}
if !(rate >= 0.0 && rate <= 1.0) {
log.Warn("at index %d: ignoring rule %+v: rate is out of [0.0, 1.0] range", i, v)
continue
}
switch {
case v.Service != "" && v.Name != "":
rules = append(rules, NameServiceRule(v.Name, v.Service, rate))
case v.Service != "":
rules = append(rules, ServiceRule(v.Service, rate))
case v.Name != "":
rules = append(rules, NameRule(v.Name, rate))
}
}
if len(errs) != 0 {
return rules, fmt.Errorf("found errors:\n\t%s", strings.Join(errs, "\n\t"))
}
return rules, nil
}
// globalSampleRate returns the sampling rate found in the DD_TRACE_SAMPLE_RATE environment variable.
// If it is invalid or not within the 0-1 range, NaN is returned.
func globalSampleRate() float64 {
defaultRate := math.NaN()
v := os.Getenv("DD_TRACE_SAMPLE_RATE")
if v == "" {
return defaultRate
}
r, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Warn("ignoring DD_TRACE_SAMPLE_RATE: error: %v", err)
return defaultRate
}
if r >= 0.0 && r <= 1.0 {
return r
}
log.Warn("ignoring DD_TRACE_SAMPLE_RATE: out of range %f", r)
return defaultRate
}
// defaultRateLimit specifies the default trace rate limit used when DD_TRACE_RATE_LIMIT is not set.
const defaultRateLimit = 100.0
// newRateLimiter returns a rate limiter which restricts the number of traces sampled per second.
// This defaults to 100.0. The DD_TRACE_RATE_LIMIT environment variable may override the default.
func newRateLimiter() *rateLimiter {
limit := defaultRateLimit
v := os.Getenv("DD_TRACE_RATE_LIMIT")
if v != "" {
l, err := strconv.ParseFloat(v, 64)
if err != nil {
log.Warn("using default rate limit because DD_TRACE_RATE_LIMIT is invalid: %v", err)
} else if l < 0.0 {
log.Warn("using default rate limit because DD_TRACE_RATE_LIMIT is negative: %f", l)
} else {
// override the default limit
limit = l
}
}
return &rateLimiter{
limiter: rate.NewLimiter(rate.Limit(limit), int(math.Ceil(limit))),
prevTime: time.Now(),
}
}
// apply uses the sampling rules to determine the sampling rate for the
// provided span. If the rules don't match, and a default rate hasn't been
// set using DD_TRACE_SAMPLE_RATE, then it returns false and the span is not
// modified.
func (rs *rulesSampler) apply(span *span) bool {
if len(rs.rules) == 0 && math.IsNaN(rs.globalRate) {
// short path when disabled
return false
}
var matched bool
rate := rs.globalRate
for _, rule := range rs.rules {
if rule.match(span) {
matched = true
rate = rule.Rate
break
}
}
if !matched && math.IsNaN(rate) {
// no matching rule or global rate, so we want to fall back
// to priority sampling
return false
}
rs.applyRate(span, rate, time.Now())
return true
}
func (rs *rulesSampler) applyRate(span *span, rate float64, now time.Time) {
span.SetTag(keyRulesSamplerAppliedRate, rate)
if !sampledByRate(span.TraceID, rate) {
span.SetTag(ext.SamplingPriority, ext.PriorityAutoReject)
return
}
sampled, rate := rs.limiter.allowOne(now)
if sampled {
span.SetTag(ext.SamplingPriority, ext.PriorityAutoKeep)
} else {
span.SetTag(ext.SamplingPriority, ext.PriorityAutoReject)
}
span.SetTag(keyRulesSamplerLimiterRate, rate)
}
// SamplingRule is used for applying sampling rates to spans that match
// the service name, operation name or both.
// For basic usage, consider using the helper functions ServiceRule, NameRule, etc.
type SamplingRule struct {
Service *regexp.Regexp
Name *regexp.Regexp
Rate float64
exactService string
exactName string
}
// ServiceRule returns a SamplingRule that applies the provided sampling rate
// to spans that match the service name provided.
func ServiceRule(service string, rate float64) SamplingRule {
return SamplingRule{
exactService: service,
Rate: rate,
}
}
// NameRule returns a SamplingRule that applies the provided sampling rate
// to spans that match the operation name provided.
func NameRule(name string, rate float64) SamplingRule {
return SamplingRule{
exactName: name,
Rate: rate,
}
}
// NameServiceRule returns a SamplingRule that applies the provided sampling rate
// to spans matching both the operation and service names provided.
func NameServiceRule(name string, service string, rate float64) SamplingRule {
return SamplingRule{
exactService: service,
exactName: name,
Rate: rate,
}
}
// RateRule returns a SamplingRule that applies the provided sampling rate to all spans.
func RateRule(rate float64) SamplingRule {
return SamplingRule{
Rate: rate,
}
}
// match returns true when the span's details match all the expected values in the rule.
func (sr *SamplingRule) match(s *span) bool {
if sr.Service != nil && !sr.Service.MatchString(s.Service) {
return false
} else if sr.exactService != "" && sr.exactService != s.Service {
return false
}
if sr.Name != nil && !sr.Name.MatchString(s.Name) {
return false
} else if sr.exactName != "" && sr.exactName != s.Name {
return false
}
return true
}
// MarshalJSON implements the json.Marshaler interface.
func (sr *SamplingRule) MarshalJSON() ([]byte, error) {
s := struct {
Service string `json:"service"`
Name string `json:"name"`
Rate float64 `json:"sample_rate"`
}{}
if sr.exactService != "" {
s.Service = sr.exactService
} else if sr.Service != nil {
s.Service = fmt.Sprintf("%s", sr.Service)
}
if sr.exactName != "" {
s.Name = sr.exactName
} else if sr.Name != nil {
s.Name = fmt.Sprintf("%s", sr.Name)
}
s.Rate = sr.Rate
return json.Marshal(&s)
}
// rateLimiter is a wrapper on top of golang.org/x/time/rate which implements a rate limiter but also
// returns the effective rate of allowance.
type rateLimiter struct {
limiter *rate.Limiter
mu sync.Mutex // guards below fields
prevTime time.Time // time at which prevAllowed and prevSeen were set
allowed float64 // number of spans allowed in the current period
seen float64 // number of spans seen in the current period
prevAllowed float64 // number of spans allowed in the previous period
prevSeen float64 // number of spans seen in the previous period
}
// allowOne returns the rate limiter's decision to allow the span to be sampled, and the
// effective rate at the time it is called. The effective rate is computed by averaging the rate
// for the previous second with the current rate
func (r *rateLimiter) allowOne(now time.Time) (bool, float64) {
r.mu.Lock()
defer r.mu.Unlock()
if d := now.Sub(r.prevTime); d >= time.Second {
// enough time has passed to reset the counters
if d.Truncate(time.Second) == time.Second && r.seen > 0 {
// exactly one second, so update prev
r.prevAllowed = r.allowed
r.prevSeen = r.seen
} else {
// more than one second, so reset previous rate
r.prevAllowed = 0
r.prevSeen = 0
}
r.prevTime = now
r.allowed = 0
r.seen = 0
}
r.seen++
var sampled bool
if r.limiter.AllowN(now, 1) {
r.allowed++
sampled = true
}
er := (r.prevAllowed + r.allowed) / (r.prevSeen + r.seen)
return sampled, er
}