forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traces_sampler_test.go
59 lines (54 loc) · 1.34 KB
/
traces_sampler_test.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
package sentry
import (
"fmt"
"sync"
"testing"
)
func TestFixedRateSampler(t *testing.T) {
ctx := NewTestContext(ClientOptions{})
rootSpan := StartSpan(ctx, "root")
t.Run("UniformRate", func(t *testing.T) {
// The sample decision for the root span should observe the configured
// rate.
tests := []struct {
Rate float64
Tolerance float64
}{
{0.0, 0.0},
{0.25, 0.1},
{0.5, 0.1},
{0.75, 0.1},
{1.0, 0.0},
}
for _, tt := range tests {
tt := tt
t.Run(fmt.Sprint(tt.Rate), func(t *testing.T) {
got := repeatedSample(UniformTracesSampler(tt.Rate), SamplingContext{Span: rootSpan}, 10000)
if got < tt.Rate*(1-tt.Tolerance) || got > tt.Rate*(1+tt.Tolerance) {
t.Errorf("got rootSpan sample rate %.2f, want %.2f±%.0f%%", got, tt.Rate, 100*tt.Tolerance)
}
})
}
})
t.Run("Concurrency", func(t *testing.T) {
// This test is for use with -race to catch data races.
var wg sync.WaitGroup
for i := 0; i < 32; i++ {
wg.Add(1)
go func() {
defer wg.Done()
repeatedSample(UniformTracesSampler(0.5), SamplingContext{Span: rootSpan}, 10000)
}()
}
wg.Wait()
})
}
func repeatedSample(sampler TracesSampler, ctx SamplingContext, count int) (observedRate float64) {
var n float64
for i := 0; i < count; i++ {
if sampler.Sample(ctx).Bool() {
n++
}
}
return n / float64(count)
}