@@ -162,27 +162,49 @@ func TestStartInternalCallbackSpan(t *testing.T) {
162
162
163
163
t .Run ("traceparent is provided with sampling flag = 0 and sampling is enabled (but not P=1.00)" , func (t * testing.T ) {
164
164
// We use a fixed seed for the RNG so we can use an exact number here
165
- const expectSampled = 1051
165
+ const expectSampled = 0
166
166
const numTraces = 100000
167
- sampledCount := runTraces (t , "test_trace" , numTraces , "0.01" , 0 )
167
+ sampledCount := runTraces (t , "test_trace" , numTraces , "0.01" , true , 0 )
168
168
require .Equal (t , expectSampled , sampledCount , "Expected to sample %d traces but sampled %d" , expectSampled , sampledCount )
169
169
require .Less (t , sampledCount , numTraces , "Expected to sample fewer than the total number of traces, but sampled all of them!" )
170
170
})
171
171
172
172
t .Run ("traceparent is provided with sampling flag = 0 and sampling is enabled (and P=1.00)" , func (t * testing.T ) {
173
+ const expectSampled = 0
173
174
const numTraces = 1000
174
- sampledCount := runTraces (t , "test_trace" , numTraces , "1.00" , 0 )
175
- require .Equal (t , numTraces , sampledCount , "Expected to sample all traces (%d) but only sampled %d" , numTraces , sampledCount )
175
+ sampledCount := runTraces (t , "test_trace" , numTraces , "1.00" , true , 0 )
176
+ require .Equal (t , expectSampled , sampledCount , "Expected to sample all traces (%d) but only sampled %d" , numTraces , sampledCount )
176
177
})
177
178
178
179
t .Run ("traceparent is provided with sampling flag = 1 and sampling is enabled (but not P=1.00)" , func (t * testing.T ) {
179
180
const numTraces = 1000
180
- sampledCount := runTraces (t , "test_trace" , numTraces , "0.00001" , 1 )
181
+ sampledCount := runTraces (t , "test_trace" , numTraces , "0.00001" , true , 1 )
182
+ require .Equal (t , numTraces , sampledCount , "Expected to sample all traces (%d) but only sampled %d" , numTraces , sampledCount )
183
+ })
184
+
185
+ t .Run ("traceparent is not provided and sampling is enabled (but not P=1.00)" , func (t * testing.T ) {
186
+ // We use a fixed seed for the RNG so we can use an exact number here
187
+ const expectSampled = 1000 // we allow for a 10% margin of error to account for randomness
188
+ const numTraces = 100000
189
+ sampledCount := runTraces (t , "test_trace" , numTraces , "0.01" , false , 0 )
190
+ require .InEpsilon (t , expectSampled , sampledCount , 0.1 , "Expected to sample %d (+/- 10%) traces but sampled %d" , expectSampled , sampledCount )
191
+ require .Less (t , sampledCount , numTraces , "Expected to sample fewer than the total number of traces, but sampled all of them!" )
192
+ })
193
+
194
+ t .Run ("traceparent is not provided and sampling is enabled (and P=1.00)" , func (t * testing.T ) {
195
+ const numTraces = 1000
196
+ sampledCount := runTraces (t , "test_trace" , numTraces , "1.00" , false , 0 )
181
197
require .Equal (t , numTraces , sampledCount , "Expected to sample all traces (%d) but only sampled %d" , numTraces , sampledCount )
182
198
})
199
+
200
+ t .Run ("traceparent is not provided and sampling is enabled (but almost 0 P=0.00001)" , func (t * testing.T ) {
201
+ const numTraces = 1000
202
+ sampledCount := runTraces (t , "test_trace" , numTraces , "0.00001" , false , 0 )
203
+ require .Less (t , sampledCount , int (numTraces * .001 ), "Expected to sample no traces (+/- 10%) but only sampled %d" , sampledCount )
204
+ })
183
205
}
184
206
185
- func runTraces (t * testing.T , testName string , numTraces int , samplingRate string , parentTraceFlag int ) int {
207
+ func runTraces (t * testing.T , testName string , numTraces int , samplingRate string , hasParentSpanContext bool , parentTraceFlag int ) int {
186
208
d := NewDaprTraceSampler (samplingRate )
187
209
tracerOptions := []sdktrace.TracerProviderOption {
188
210
sdktrace .WithSampler (d ),
@@ -199,17 +221,17 @@ func runTraces(t *testing.T, testName string, numTraces int, samplingRate string
199
221
sampledCount := 0
200
222
201
223
for i := 0 ; i < numTraces ; i ++ {
202
- traceID , _ := idg .NewIDs (context .Background ())
203
- scConfig := trace.SpanContextConfig {
204
- TraceID : traceID ,
205
- SpanID : trace.SpanID {0 , 240 , 103 , 170 , 11 , 169 , 2 , 183 },
206
- TraceFlags : trace .TraceFlags (parentTraceFlag ),
207
- }
208
-
209
- parent := trace .NewSpanContext (scConfig )
210
-
211
224
ctx := context .Background ()
212
- ctx = trace .ContextWithRemoteSpanContext (ctx , parent )
225
+ if hasParentSpanContext {
226
+ traceID , _ := idg .NewIDs (context .Background ())
227
+ scConfig := trace.SpanContextConfig {
228
+ TraceID : traceID ,
229
+ SpanID : trace.SpanID {0 , 240 , 103 , 170 , 11 , 169 , 2 , 183 },
230
+ TraceFlags : trace .TraceFlags (parentTraceFlag ),
231
+ }
232
+ parent := trace .NewSpanContext (scConfig )
233
+ ctx = trace .ContextWithRemoteSpanContext (ctx , parent )
234
+ }
213
235
ctx , span := testTracer .Start (ctx , "testTraceSpan" , trace .WithSpanKind (trace .SpanKindClient ))
214
236
assert .NotNil (t , span )
215
237
assert .NotNil (t , ctx )
0 commit comments