Debian Codegraph @

sdk/trace/sampling_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package trace
5
6 import (
7 "fmt"
8 "math/rand/v2"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "go.opentelemetry.io/otel/trace"
15 )
16
17 func TestParentBasedDefaultLocalParentSampled(t *testing.T) {
18 sampler := ParentBased(AlwaysSample())
19 traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
20 spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
21 parentCtx := trace.ContextWithSpanContext(
22 t.Context(),
23 trace.NewSpanContext(trace.SpanContextConfig{
24 TraceID: traceID,
25 SpanID: spanID,
26 TraceFlags: trace.FlagsSampled,
27 }),
28 )
29 if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != RecordAndSample {
30 t.Error("Sampling decision should be RecordAndSample")
31 }
32 }
33
34 func TestParentBasedDefaultLocalParentNotSampled(t *testing.T) {
35 sampler := ParentBased(AlwaysSample())
36 traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
37 spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
38 parentCtx := trace.ContextWithSpanContext(
39 t.Context(),
40 trace.NewSpanContext(trace.SpanContextConfig{
41 TraceID: traceID,
42 SpanID: spanID,
43 }),
44 )
45 if sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx}).Decision != Drop {
46 t.Error("Sampling decision should be Drop")
47 }
48 }
49
50 func TestParentBasedWithNoParent(t *testing.T) {
51 params := SamplingParameters{}
52
53 sampler := ParentBased(AlwaysSample())
54 if sampler.ShouldSample(params).Decision != RecordAndSample {
55 t.Error("Sampling decision should be RecordAndSample")
56 }
57
58 sampler = ParentBased(NeverSample())
59 if sampler.ShouldSample(params).Decision != Drop {
60 t.Error("Sampling decision should be Drop")
61 }
62 }
63
64 func TestParentBasedWithSamplerOptions(t *testing.T) {
65 testCases := []struct {
66 name string
67 samplerOption ParentBasedSamplerOption
68 isParentRemote, isParentSampled bool
69 expectedDecision SamplingDecision
70 }{
71 {
72 "localParentSampled",
73 WithLocalParentSampled(NeverSample()),
74 false,
75 true,
76 Drop,
77 },
78 {
79 "localParentNotSampled",
80 WithLocalParentNotSampled(AlwaysSample()),
81 false,
82 false,
83 RecordAndSample,
84 },
85 {
86 "remoteParentSampled",
87 WithRemoteParentSampled(NeverSample()),
88 true,
89 true,
90 Drop,
91 },
92 {
93 "remoteParentNotSampled",
94 WithRemoteParentNotSampled(AlwaysSample()),
95 true,
96 false,
97 RecordAndSample,
98 },
99 }
100
101 for _, tc := range testCases {
102 t.Run(tc.name, func(t *testing.T) {
103 traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
104 spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
105 pscc := trace.SpanContextConfig{
106 TraceID: traceID,
107 SpanID: spanID,
108 Remote: tc.isParentRemote,
109 }
110 if tc.isParentSampled {
111 pscc.TraceFlags = trace.FlagsSampled
112 }
113
114 params := SamplingParameters{
115 ParentContext: trace.ContextWithSpanContext(
116 t.Context(),
117 trace.NewSpanContext(pscc),
118 ),
119 }
120
121 sampler := ParentBased(
122 nil,
123 tc.samplerOption,
124 )
125
126 var wantStr, gotStr string
127 switch tc.expectedDecision {
128 case RecordAndSample:
129 wantStr = "RecordAndSample"
130 case Drop:
131 wantStr = "Drop"
132 default:
133 wantStr = "unknown"
134 }
135
136 actualDecision := sampler.ShouldSample(params).Decision
137 switch actualDecision {
138 case RecordAndSample:
139 gotStr = "RecordAndSample"
140 case Drop:
141 gotStr = "Drop"
142 default:
143 gotStr = "unknown"
144 }
145
146 assert.Equalf(t, tc.expectedDecision, actualDecision, "want %s, got %s", wantStr, gotStr)
147 })
148 }
149 }
150
151 func TestParentBasedDefaultDescription(t *testing.T) {
152 sampler := ParentBased(AlwaysSample())
153
154 expectedDescription := fmt.Sprintf("ParentBased{root:%s,remoteParentSampled:%s,"+
155 "remoteParentNotSampled:%s,localParentSampled:%s,localParentNotSampled:%s}",
156 AlwaysSample().Description(),
157 AlwaysSample().Description(),
158 NeverSample().Description(),
159 AlwaysSample().Description(),
160 NeverSample().Description())
161
162 if sampler.Description() != expectedDescription {
163 t.Errorf("Sampler description should be %s, got '%s' instead",
164 expectedDescription,
165 sampler.Description(),
166 )
167 }
168 }
169
170 // TraceIDRatioBased sampler requirements state
171 //
172 // "A TraceIDRatioBased sampler with a given sampling rate MUST also sample
173 // all traces that any TraceIDRatioBased sampler with a lower sampling rate
174 // would sample."
175 func TestTraceIdRatioSamplesInclusively(t *testing.T) {
176 const (
177 numSamplers = 1000
178 numTraces = 100
179 )
180 idg := defaultIDGenerator()
181
182 for range numSamplers {
183 ratioLo, ratioHi := rand.Float64(), rand.Float64()
184 if ratioHi < ratioLo {
185 ratioLo, ratioHi = ratioHi, ratioLo
186 }
187 samplerHi := TraceIDRatioBased(ratioHi)
188 samplerLo := TraceIDRatioBased(ratioLo)
189 for range numTraces {
190 traceID, _ := idg.NewIDs(t.Context())
191
192 params := SamplingParameters{TraceID: traceID}
193 if samplerLo.ShouldSample(params).Decision == RecordAndSample {
194 require.Equal(t, RecordAndSample, samplerHi.ShouldSample(params).Decision,
195 "%s sampled but %s did not", samplerLo.Description(), samplerHi.Description())
196 }
197 }
198 }
199 }
200
201 func TestTracestateIsPassed(t *testing.T) {
202 testCases := []struct {
203 name string
204 sampler Sampler
205 }{
206 {
207 "notSampled",
208 NeverSample(),
209 },
210 {
211 "sampled",
212 AlwaysSample(),
213 },
214 {
215 "parentSampled",
216 ParentBased(AlwaysSample()),
217 },
218 {
219 "parentNotSampled",
220 ParentBased(NeverSample()),
221 },
222 {
223 "traceIDRatioSampler",
224 TraceIDRatioBased(.5),
225 },
226 }
227
228 for _, tc := range testCases {
229 t.Run(tc.name, func(t *testing.T) {
230 traceState, err := trace.ParseTraceState("k=v")
231 if err != nil {
232 t.Error(err)
233 }
234
235 params := SamplingParameters{
236 ParentContext: trace.ContextWithSpanContext(
237 t.Context(),
238 trace.NewSpanContext(trace.SpanContextConfig{
239 TraceState: traceState,
240 }),
241 ),
242 }
243
244 require.Equal(t, traceState, tc.sampler.ShouldSample(params).Tracestate, "TraceState is not equal")
245 })
246 }
247 }
248
249 func TestAlwaysRecordSamplingDecision(t *testing.T) {
250 traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
251 spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
252
253 testCases := []struct {
254 name string
255 rootSampler Sampler
256 expectedDecision SamplingDecision
257 }{
258 {
259 name: "when root sampler decision is RecordAndSample, AlwaysRecord returns RecordAndSample",
260 rootSampler: AlwaysSample(),
261 expectedDecision: RecordAndSample,
262 },
263 {
264 name: "when root sampler decision is Drop, AlwaysRecord returns RecordOnly",
265 rootSampler: NeverSample(),
266 expectedDecision: RecordOnly,
267 },
268 {
269 name: "when root sampler decision is RecordOnly, AlwaysRecord returns RecordOnly",
270 rootSampler: RecordingOnly(),
271 expectedDecision: RecordOnly,
272 },
273 }
274
275 for _, tc := range testCases {
276 t.Run(tc.name, func(t *testing.T) {
277 sampler := AlwaysRecord(tc.rootSampler)
278 parentCtx := trace.ContextWithSpanContext(
279 t.Context(),
280 trace.NewSpanContext(trace.SpanContextConfig{
281 TraceID: traceID,
282 SpanID: spanID,
283 TraceFlags: trace.FlagsSampled,
284 }),
285 )
286 samplingResult := sampler.ShouldSample(SamplingParameters{ParentContext: parentCtx})
287 if samplingResult.Decision != tc.expectedDecision {
288 t.Errorf("Sampling decision should be %v, got %v instead",
289 tc.expectedDecision,
290 samplingResult.Decision,
291 )
292 }
293 })
294 }
295 }
296
297 func TestAlwaysRecordDefaultDescription(t *testing.T) {
298 sampler := AlwaysRecord(NeverSample())
299
300 expectedDescription := fmt.Sprintf("AlwaysRecord{root:%s}", NeverSample().Description())
301
302 if sampler.Description() != expectedDescription {
303 t.Errorf("Sampler description should be %s, got '%s' instead",
304 expectedDescription,
305 sampler.Description(),
306 )
307 }
308 }
309
310 func TestDescriptions(t *testing.T) {
311 assert.Equal(t, "AlwaysOnSampler", AlwaysSample().Description())
312 assert.Equal(t, "AlwaysOffSampler", NeverSample().Description())
313 assert.Equal(t, "TraceIDRatioBased{0.5}", TraceIDRatioBased(0.5).Description())
314 assert.Equal(t, "TraceIDRatioBased{1}", TraceIDRatioBased(1).Description())
315 assert.Equal(t, "TraceIDRatioBased{1}", TraceIDRatioBased(1.5).Description())
316 assert.Equal(t, "TraceIDRatioBased{0}", TraceIDRatioBased(0).Description())
317 assert.Equal(t, "TraceIDRatioBased{0}", TraceIDRatioBased(-0.5).Description())
318 }