@@ -2,78 +2,72 @@ package sampler
22
33import (
44 "context"
5- "math/rand"
65 "testing"
76
87 "github.com/stretchr/testify/require"
8+ "go.opentelemetry.io/contrib/propagators/aws/xray"
99 sdktrace "go.opentelemetry.io/otel/sdk/trace"
1010 "go.opentelemetry.io/otel/trace"
1111)
1212
13- type mockGenerator struct {
14- mockedValue float64
15- }
16-
17- func (g * mockGenerator ) Float64 () float64 {
18- return g .mockedValue
19- }
20-
2113func Test_ShouldSample (t * testing.T ) {
22- traceID , _ := trace .TraceIDFromHex ("4bf92f3577b34da6a3ce929d0e0e4736" )
2314 parentCtx := trace .ContextWithSpanContext (
2415 context .Background (),
2516 trace .NewSpanContext (trace.SpanContextConfig {
2617 TraceState : trace.TraceState {},
2718 }),
2819 )
2920
21+ generator := xray .NewIDGenerator ()
22+
3023 tests := []struct {
31- name string
32- samplingDecision sdktrace.SamplingDecision
33- fraction float64
34- generator randGenerator
24+ name string
25+ fraction float64
3526 }{
3627 {
37- name : "should always sample" ,
38- samplingDecision : sdktrace .RecordAndSample ,
39- fraction : 1 ,
40- generator : rand .New (rand .NewSource (rand .Int63 ())),
28+ name : "should always sample" ,
29+ fraction : 1 ,
4130 },
4231 {
43- name : "should nerver sample" ,
44- samplingDecision : sdktrace .Drop ,
45- fraction : 0 ,
46- generator : rand .New (rand .NewSource (rand .Int63 ())),
32+ name : "should nerver sample" ,
33+ fraction : 0 ,
4734 },
4835 {
49- name : "should sample when fraction is above generated" ,
50- samplingDecision : sdktrace .RecordAndSample ,
51- fraction : 0.5 ,
52- generator : & mockGenerator {0.2 },
36+ name : "should sample 50%" ,
37+ fraction : 0.5 ,
5338 },
5439 {
55- name : "should not sample when fraction is not above generated" ,
56- samplingDecision : sdktrace .Drop ,
57- fraction : 0.5 ,
58- generator : & mockGenerator {0.8 },
40+ name : "should sample 10%" ,
41+ fraction : 0.1 ,
42+ },
43+ {
44+ name : "should sample 1%" ,
45+ fraction : 0.01 ,
5946 },
6047 }
6148
49+ totalIterations := 10000
6250 for _ , tt := range tests {
6351 t .Run (tt .name , func (t * testing.T ) {
64- s := NewRandomRatioBased (tt .fraction , tt .generator )
65- for i := 0 ; i < 100 ; i ++ {
66-
52+ totalSampled := 0
53+ s := NewRandomRatioBased (tt .fraction )
54+ for i := 0 ; i < totalIterations ; i ++ {
55+ traceId , _ := generator .NewIDs (context .Background ())
6756 r := s .ShouldSample (
6857 sdktrace.SamplingParameters {
6958 ParentContext : parentCtx ,
70- TraceID : traceID ,
59+ TraceID : traceId ,
7160 Name : "test" ,
7261 Kind : trace .SpanKindServer ,
7362 })
74-
75- require .Equal (t , tt .samplingDecision , r .Decision )
63+ if r .Decision == sdktrace .RecordAndSample {
64+ totalSampled += 1
65+ }
7666 }
67+
68+ tolerance := 0.1
69+ expected := tt .fraction * float64 (totalIterations )
70+ require .InDelta (t , expected , totalSampled , expected * tolerance )
7771 })
7872 }
7973}
0 commit comments