-
Notifications
You must be signed in to change notification settings - Fork 781
/
sampling_test.go
73 lines (66 loc) · 1.49 KB
/
sampling_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package sampler
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/contrib/propagators/aws/xray"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
func Test_ShouldSample(t *testing.T) {
parentCtx := trace.ContextWithSpanContext(
context.Background(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceState: trace.TraceState{},
}),
)
generator := xray.NewIDGenerator()
tests := []struct {
name string
fraction float64
}{
{
name: "should always sample",
fraction: 1,
},
{
name: "should nerver sample",
fraction: 0,
},
{
name: "should sample 50%",
fraction: 0.5,
},
{
name: "should sample 10%",
fraction: 0.1,
},
{
name: "should sample 1%",
fraction: 0.01,
},
}
totalIterations := 10000
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
totalSampled := 0
s := NewXrayTraceIDRatioBased(tt.fraction)
for i := 0; i < totalIterations; i++ {
traceID, _ := generator.NewIDs(context.Background())
r := s.ShouldSample(
sdktrace.SamplingParameters{
ParentContext: parentCtx,
TraceID: traceID,
Name: "test",
Kind: trace.SpanKindServer,
})
if r.Decision == sdktrace.RecordAndSample {
totalSampled++
}
}
tolerance := 0.1
expected := tt.fraction * float64(totalIterations)
require.InDelta(t, expected, totalSampled, expected*tolerance)
})
}
}