forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampling.go
70 lines (58 loc) · 2.28 KB
/
sampling.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
package tracing
import (
"math/rand"
)
// Sampler is an interface defining a sampling strategy.
type Sampler interface {
// ShouldSample returns true if the trace should be sampled
// false otherwise.
ShouldSample() bool
}
// ProbabilisticSampler implements the ProbabilisticSampler interface and allows
// to take probabilistic sampling decisions based on a sampling rate.
type ProbabilisticSampler struct {
// random is a random number generator used by the sampler.
random *rand.Rand
// samplingRate is a chance value defined as a percentage
// value within 0.0 <= samplingRate <= 1.0 bounds.
samplingRate float64
}
// NewProbabilisticSampler returns a new ProbablisticSampler with the provided sampling rate.
//
// Note that the sampling rate is a percentage value within 0.0 <= samplingRate <= 1.0 bounds.
// If the provided sampling rate is outside of this range, it will be clamped to the closest
// bound.
func NewProbabilisticSampler(samplingRate float64) *ProbabilisticSampler {
// Ensure that the sampling rate is within the 0.0 <= samplingRate <= 1.0 bounds.
if samplingRate < 0.0 {
samplingRate = 0.0
} else if samplingRate > 1.0 {
samplingRate = 1.0
}
return &ProbabilisticSampler{samplingRate: samplingRate}
}
// ShouldSample returns true if the trace should be sampled.
//
// Its return value is probabilistic, based on the selected
// sampling rate S, there is S percent chance that the
// returned value is true.
func (ps ProbabilisticSampler) ShouldSample() bool {
return chance(ps.random, ps.samplingRate)
}
// Ensure that ProbabilisticSampler implements the Sampler interface.
var _ Sampler = &ProbabilisticSampler{}
// AlwaysOnSampler implements the Sampler interface and allows to bypass
// sampling decisions by returning true for all Sampled() calls.
//
// This is useful in cases where the user either does not provide
// the sampling option, or set it to 100% as it will avoid any
// call to the random number generator.
type AlwaysOnSampler struct{}
// NewAlwaysOnSampler returns a new AlwaysSampledSampler.
func NewAlwaysOnSampler() *AlwaysOnSampler {
return &AlwaysOnSampler{}
}
// ShouldSample always returns true.
func (AlwaysOnSampler) ShouldSample() bool { return true }
// Ensure that AlwaysOnSampler implements the Sampler interface.
var _ Sampler = &AlwaysOnSampler{}