@@ -28,6 +28,7 @@ import (
28
28
"go.opentelemetry.io/collector/consumer"
29
29
"go.opentelemetry.io/collector/consumer/consumertest"
30
30
"go.opentelemetry.io/collector/consumer/pdata"
31
+ "go.opentelemetry.io/collector/translator/conventions"
31
32
tracetranslator "go.opentelemetry.io/collector/translator/trace"
32
33
)
33
34
@@ -71,6 +72,7 @@ func TestNewTraceProcessor(t *testing.T) {
71
72
if ! tt .wantErr {
72
73
// The truncation below with uint32 cannot be defined at initialization (compiler error), performing it at runtime.
73
74
tt .want .(* tracesamplerprocessor ).scaledSamplingRate = uint32 (tt .cfg .SamplingPercentage * percentageScaleFactor )
75
+ tt .want .(* tracesamplerprocessor ).samplingProbability = float64 (tt .cfg .SamplingPercentage ) * 0.01
74
76
}
75
77
got , err := newTraceProcessor (tt .nextConsumer , tt .cfg )
76
78
if (err != nil ) != tt .wantErr {
@@ -227,15 +229,7 @@ func Test_tracesamplerprocessor_SamplingPercentageRange_MultipleResourceSpans(t
227
229
228
230
// Test_tracesamplerprocessor_SpanSamplingPriority checks if handling of "sampling.priority" is correct.
229
231
func Test_tracesamplerprocessor_SpanSamplingPriority (t * testing.T ) {
230
- singleSpanWithAttrib := func (key string , attribValue pdata.AttributeValue ) pdata.Traces {
231
- traces := pdata .NewTraces ()
232
- traces .ResourceSpans ().Resize (1 )
233
- rs := traces .ResourceSpans ().At (0 )
234
- rs .InstrumentationLibrarySpans ().Resize (1 )
235
- instrLibrarySpans := rs .InstrumentationLibrarySpans ().At (0 )
236
- instrLibrarySpans .Spans ().Append (getSpanWithAttributes (key , attribValue ))
237
- return traces
238
- }
232
+
239
233
tests := []struct {
240
234
name string
241
235
cfg Config
@@ -247,7 +241,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
247
241
cfg : Config {
248
242
SamplingPercentage : 0.0 ,
249
243
},
250
- td : singleSpanWithAttrib (
244
+ td : getTracesWithSpanWithAttribute (
251
245
"sampling.priority" ,
252
246
pdata .NewAttributeValueInt (2 )),
253
247
sampled : true ,
@@ -257,7 +251,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
257
251
cfg : Config {
258
252
SamplingPercentage : 0.0 ,
259
253
},
260
- td : singleSpanWithAttrib (
254
+ td : getTracesWithSpanWithAttribute (
261
255
"sampling.priority" ,
262
256
pdata .NewAttributeValueDouble (1 )),
263
257
sampled : true ,
@@ -267,7 +261,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
267
261
cfg : Config {
268
262
SamplingPercentage : 0.0 ,
269
263
},
270
- td : singleSpanWithAttrib (
264
+ td : getTracesWithSpanWithAttribute (
271
265
"sampling.priority" ,
272
266
pdata .NewAttributeValueString ("1" )),
273
267
sampled : true ,
@@ -277,7 +271,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
277
271
cfg : Config {
278
272
SamplingPercentage : 100.0 ,
279
273
},
280
- td : singleSpanWithAttrib (
274
+ td : getTracesWithSpanWithAttribute (
281
275
"sampling.priority" ,
282
276
pdata .NewAttributeValueInt (0 )),
283
277
},
@@ -286,7 +280,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
286
280
cfg : Config {
287
281
SamplingPercentage : 100.0 ,
288
282
},
289
- td : singleSpanWithAttrib (
283
+ td : getTracesWithSpanWithAttribute (
290
284
"sampling.priority" ,
291
285
pdata .NewAttributeValueDouble (0 )),
292
286
},
@@ -295,7 +289,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
295
289
cfg : Config {
296
290
SamplingPercentage : 100.0 ,
297
291
},
298
- td : singleSpanWithAttrib (
292
+ td : getTracesWithSpanWithAttribute (
299
293
"sampling.priority" ,
300
294
pdata .NewAttributeValueString ("0" )),
301
295
},
@@ -304,7 +298,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
304
298
cfg : Config {
305
299
SamplingPercentage : 0.0 ,
306
300
},
307
- td : singleSpanWithAttrib (
301
+ td : getTracesWithSpanWithAttribute (
308
302
"no.sampling.priority" ,
309
303
pdata .NewAttributeValueInt (2 )),
310
304
},
@@ -313,7 +307,7 @@ func Test_tracesamplerprocessor_SpanSamplingPriority(t *testing.T) {
313
307
cfg : Config {
314
308
SamplingPercentage : 100.0 ,
315
309
},
316
- td : singleSpanWithAttrib (
310
+ td : getTracesWithSpanWithAttribute (
317
311
"no.sampling.priority" ,
318
312
pdata .NewAttributeValueInt (2 )),
319
313
sampled : true ,
@@ -416,13 +410,76 @@ func Test_parseSpanSamplingPriority(t *testing.T) {
416
410
}
417
411
}
418
412
413
+ // Test_tracesamplerprocessor_SamplingProbabilityAttribute verifies if the attribute describing current sampling rate is included in sampled spans
414
+ func Test_tracesamplerprocessor_SamplingProbabilityAttribute (t * testing.T ) {
415
+ cfg := Config {
416
+ SamplingPercentage : 100.0 ,
417
+ }
418
+
419
+ tests := []struct {
420
+ name string
421
+ traces pdata.Traces
422
+ wantSamplingProbabilityAttribute pdata.AttributeValue
423
+ }{
424
+ {
425
+ name : "simple_span" ,
426
+ traces : getTracesWithSpanWithAttribute ("foo" , pdata .NewAttributeValueString ("bar" )),
427
+ wantSamplingProbabilityAttribute : pdata .NewAttributeValueDouble (1.0 ),
428
+ },
429
+ {
430
+ name : "span_came_through_sampler_already" ,
431
+ traces : getTracesWithSpanWithAttribute (conventions .AttributeSamplingProbability , pdata .NewAttributeValueDouble (0.01 )),
432
+ wantSamplingProbabilityAttribute : pdata .NewAttributeValueDouble (0.01 ),
433
+ },
434
+ {
435
+ name : "simple_with_invalid_attribute_value" ,
436
+ traces : getTracesWithSpanWithAttribute (conventions .AttributeSamplingProbability , pdata .NewAttributeValueString ("bar" )),
437
+ wantSamplingProbabilityAttribute : pdata .NewAttributeValueDouble (1.0 ),
438
+ },
439
+ }
440
+
441
+ for _ , tt := range tests {
442
+ t .Run (tt .name , func (t * testing.T ) {
443
+ sink := new (consumertest.TracesSink )
444
+ tsp , err := newTraceProcessor (sink , cfg )
445
+ if err != nil {
446
+ t .Errorf ("error when creating tracesamplerprocessor: %v" , err )
447
+ return
448
+ }
449
+
450
+ if err := tsp .ConsumeTraces (context .Background (), tt .traces ); err != nil {
451
+ t .Errorf ("tracesamplerprocessor.ConsumeTraceData() error = %v" , err )
452
+ return
453
+ }
454
+ assert .Equal (t , 1 , sink .SpansCount ())
455
+ for _ , td := range sink .AllTraces () {
456
+ span := td .ResourceSpans ().At (0 ).InstrumentationLibrarySpans ().At (0 ).Spans ().At (0 )
457
+ attrValue , found := span .Attributes ().Get (conventions .AttributeSamplingProbability )
458
+ assert .True (t , found , "Sampling probability attribute not found" )
459
+ assert .Equal (t , tt .wantSamplingProbabilityAttribute , attrValue )
460
+ }
461
+ sink .Reset ()
462
+ })
463
+ }
464
+ }
465
+
419
466
func getSpanWithAttributes (key string , value pdata.AttributeValue ) pdata.Span {
420
467
span := pdata .NewSpan ()
421
468
span .SetName ("spanName" )
422
469
span .Attributes ().InitFromMap (map [string ]pdata.AttributeValue {key : value })
423
470
return span
424
471
}
425
472
473
+ func getTracesWithSpanWithAttribute (key string , attribValue pdata.AttributeValue ) pdata.Traces {
474
+ traces := pdata .NewTraces ()
475
+ traces .ResourceSpans ().Resize (1 )
476
+ rs := traces .ResourceSpans ().At (0 )
477
+ rs .InstrumentationLibrarySpans ().Resize (1 )
478
+ instrLibrarySpans := rs .InstrumentationLibrarySpans ().At (0 )
479
+ instrLibrarySpans .Spans ().Append (getSpanWithAttributes (key , attribValue ))
480
+ return traces
481
+ }
482
+
426
483
// Test_hash ensures that the hash function supports different key lengths even if in
427
484
// practice it is only expected to receive keys with length 16 (trace id length in OC proto).
428
485
func Test_hash (t * testing.T ) {
0 commit comments