11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- package trace_test
4+ package trace
55
66import (
77 "context"
@@ -12,22 +12,17 @@ import (
1212 "testing"
1313 "time"
1414
15- "github.com/go-logr/logr"
16- "github.com/go-logr/logr/funcr"
1715 "github.com/stretchr/testify/assert"
1816 "github.com/stretchr/testify/require"
1917
20- "go.opentelemetry.io/otel/internal/global"
2118 "go.opentelemetry.io/otel/sdk/internal/env"
2219 ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
23- sdktrace "go.opentelemetry.io/otel/sdk/trace"
24- "go.opentelemetry.io/otel/sdk/trace/tracetest"
2520 "go.opentelemetry.io/otel/trace"
2621)
2722
2823type testBatchExporter struct {
2924 mu sync.Mutex
30- spans []sdktrace. ReadOnlySpan
25+ spans []ReadOnlySpan
3126 sizes []int
3227 batchCount int
3328 shutdownCount int
@@ -37,7 +32,7 @@ type testBatchExporter struct {
3732 err error
3833}
3934
40- func (t * testBatchExporter ) ExportSpans (ctx context.Context , spans []sdktrace. ReadOnlySpan ) error {
35+ func (t * testBatchExporter ) ExportSpans (ctx context.Context , spans []ReadOnlySpan ) error {
4136 t .mu .Lock ()
4237 defer t .mu .Unlock ()
4338
@@ -78,20 +73,20 @@ func (t *testBatchExporter) getBatchCount() int {
7873 return t .batchCount
7974}
8075
81- var _ sdktrace. SpanExporter = (* testBatchExporter )(nil )
76+ var _ SpanExporter = (* testBatchExporter )(nil )
8277
8378func TestNewBatchSpanProcessorWithNilExporter (t * testing.T ) {
8479 tp := basicTracerProvider (t )
85- bsp := sdktrace . NewBatchSpanProcessor (nil )
80+ bsp := NewBatchSpanProcessor (nil )
8681 tp .RegisterSpanProcessor (bsp )
8782 tr := tp .Tracer ("NilExporter" )
8883
8984 _ , span := tr .Start (context .Background (), "foo" )
9085 span .End ()
9186
9287 // These should not panic.
93- bsp .OnStart (context .Background (), span .(sdktrace. ReadWriteSpan ))
94- bsp .OnEnd (span .(sdktrace. ReadOnlySpan ))
88+ bsp .OnStart (context .Background (), span .(ReadWriteSpan ))
89+ bsp .OnEnd (span .(ReadOnlySpan ))
9590 if err := bsp .ForceFlush (context .Background ()); err != nil {
9691 t .Errorf ("failed to ForceFlush the BatchSpanProcessor: %v" , err )
9792 }
@@ -102,7 +97,7 @@ func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
10297
10398type testOption struct {
10499 name string
105- o []sdktrace. BatchSpanProcessorOption
100+ o []BatchSpanProcessorOption
106101 wantNumSpans int
107102 wantBatchCount int
108103 genNumSpans int
@@ -121,50 +116,50 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
121116 },
122117 {
123118 name : "non-default BatchTimeout" ,
124- o : []sdktrace. BatchSpanProcessorOption {
125- sdktrace . WithBatchTimeout (schDelay ),
119+ o : []BatchSpanProcessorOption {
120+ WithBatchTimeout (schDelay ),
126121 },
127122 wantNumSpans : 2053 ,
128123 wantBatchCount : 4 ,
129124 genNumSpans : 2053 ,
130125 },
131126 {
132127 name : "non-default MaxQueueSize and BatchTimeout" ,
133- o : []sdktrace. BatchSpanProcessorOption {
134- sdktrace . WithBatchTimeout (schDelay ),
135- sdktrace . WithMaxQueueSize (200 ),
128+ o : []BatchSpanProcessorOption {
129+ WithBatchTimeout (schDelay ),
130+ WithMaxQueueSize (200 ),
136131 },
137132 wantNumSpans : 205 ,
138133 wantBatchCount : 1 ,
139134 genNumSpans : 205 ,
140135 },
141136 {
142137 name : "non-default MaxQueueSize, BatchTimeout and MaxExportBatchSize" ,
143- o : []sdktrace. BatchSpanProcessorOption {
144- sdktrace . WithBatchTimeout (schDelay ),
145- sdktrace . WithMaxQueueSize (205 ),
146- sdktrace . WithMaxExportBatchSize (20 ),
138+ o : []BatchSpanProcessorOption {
139+ WithBatchTimeout (schDelay ),
140+ WithMaxQueueSize (205 ),
141+ WithMaxExportBatchSize (20 ),
147142 },
148143 wantNumSpans : 210 ,
149144 wantBatchCount : 11 ,
150145 genNumSpans : 210 ,
151146 },
152147 {
153148 name : "blocking option" ,
154- o : []sdktrace. BatchSpanProcessorOption {
155- sdktrace . WithBatchTimeout (schDelay ),
156- sdktrace . WithMaxQueueSize (200 ),
157- sdktrace . WithMaxExportBatchSize (20 ),
149+ o : []BatchSpanProcessorOption {
150+ WithBatchTimeout (schDelay ),
151+ WithMaxQueueSize (200 ),
152+ WithMaxExportBatchSize (20 ),
158153 },
159154 wantNumSpans : 205 ,
160155 wantBatchCount : 11 ,
161156 genNumSpans : 205 ,
162157 },
163158 {
164159 name : "parallel span generation" ,
165- o : []sdktrace. BatchSpanProcessorOption {
166- sdktrace . WithBatchTimeout (schDelay ),
167- sdktrace . WithMaxQueueSize (200 ),
160+ o : []BatchSpanProcessorOption {
161+ WithBatchTimeout (schDelay ),
162+ WithMaxQueueSize (200 ),
168163 },
169164 wantNumSpans : 205 ,
170165 wantBatchCount : 1 ,
@@ -173,9 +168,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
173168 },
174169 {
175170 name : "parallel span blocking" ,
176- o : []sdktrace. BatchSpanProcessorOption {
177- sdktrace . WithBatchTimeout (schDelay ),
178- sdktrace . WithMaxExportBatchSize (200 ),
171+ o : []BatchSpanProcessorOption {
172+ WithBatchTimeout (schDelay ),
173+ WithMaxExportBatchSize (200 ),
179174 },
180175 wantNumSpans : 2000 ,
181176 wantBatchCount : 10 ,
@@ -306,19 +301,19 @@ type stuckExporter struct {
306301}
307302
308303// ExportSpans waits for ctx to expire and returns that error.
309- func (e * stuckExporter ) ExportSpans (ctx context.Context , _ []sdktrace. ReadOnlySpan ) error {
304+ func (e * stuckExporter ) ExportSpans (ctx context.Context , _ []ReadOnlySpan ) error {
310305 <- ctx .Done ()
311306 e .err = ctx .Err ()
312307 return ctx .Err ()
313308}
314309
315310func TestBatchSpanProcessorExportTimeout (t * testing.T ) {
316311 exp := new (stuckExporter )
317- bsp := sdktrace . NewBatchSpanProcessor (
312+ bsp := NewBatchSpanProcessor (
318313 exp ,
319314 // Set a non-zero export timeout so a deadline is set.
320- sdktrace . WithExportTimeout (1 * time .Microsecond ),
321- sdktrace . WithBlocking (),
315+ WithExportTimeout (1 * time .Microsecond ),
316+ WithBlocking (),
322317 )
323318 tp := basicTracerProvider (t )
324319 tp .RegisterSpanProcessor (bsp )
@@ -332,10 +327,10 @@ func TestBatchSpanProcessorExportTimeout(t *testing.T) {
332327 }
333328}
334329
335- func createAndRegisterBatchSP (option testOption , te * testBatchExporter ) sdktrace. SpanProcessor {
330+ func createAndRegisterBatchSP (option testOption , te * testBatchExporter ) SpanProcessor {
336331 // Always use blocking queue to avoid flaky tests.
337- options := append (option .o , sdktrace . WithBlocking ())
338- return sdktrace . NewBatchSpanProcessor (te , options ... )
332+ options := append (option .o , WithBlocking ())
333+ return NewBatchSpanProcessor (te , options ... )
339334}
340335
341336func generateSpan (_ * testing.T , tr trace.Tracer , option testOption ) {
@@ -382,7 +377,7 @@ func getSpanContext() trace.SpanContext {
382377
383378func TestBatchSpanProcessorShutdown (t * testing.T ) {
384379 var bp testBatchExporter
385- bsp := sdktrace . NewBatchSpanProcessor (& bp )
380+ bsp := NewBatchSpanProcessor (& bp )
386381
387382 err := bsp .Shutdown (context .Background ())
388383 if err != nil {
@@ -401,14 +396,14 @@ func TestBatchSpanProcessorShutdown(t *testing.T) {
401396func TestBatchSpanProcessorPostShutdown (t * testing.T ) {
402397 tp := basicTracerProvider (t )
403398 be := testBatchExporter {}
404- bsp := sdktrace . NewBatchSpanProcessor (& be )
399+ bsp := NewBatchSpanProcessor (& be )
405400
406401 tp .RegisterSpanProcessor (bsp )
407402 tr := tp .Tracer ("Normal" )
408403
409404 generateSpanParallel (t , tr , testOption {
410- o : []sdktrace. BatchSpanProcessorOption {
411- sdktrace . WithMaxExportBatchSize (50 ),
405+ o : []BatchSpanProcessorOption {
406+ WithMaxExportBatchSize (50 ),
412407 },
413408 genNumSpans : 60 ,
414409 })
@@ -428,9 +423,9 @@ func TestBatchSpanProcessorForceFlushSucceeds(t *testing.T) {
428423 tp := basicTracerProvider (t )
429424 option := testOption {
430425 name : "default BatchSpanProcessorOptions" ,
431- o : []sdktrace. BatchSpanProcessorOption {
432- sdktrace . WithMaxQueueSize (0 ),
433- sdktrace . WithMaxExportBatchSize (3000 ),
426+ o : []BatchSpanProcessorOption {
427+ WithMaxQueueSize (0 ),
428+ WithMaxExportBatchSize (3000 ),
434429 },
435430 wantNumSpans : 2053 ,
436431 wantBatchCount : 1 ,
@@ -468,9 +463,9 @@ func TestBatchSpanProcessorDropBatchIfFailed(t *testing.T) {
468463 }
469464 tp := basicTracerProvider (t )
470465 option := testOption {
471- o : []sdktrace. BatchSpanProcessorOption {
472- sdktrace . WithMaxQueueSize (0 ),
473- sdktrace . WithMaxExportBatchSize (2000 ),
466+ o : []BatchSpanProcessorOption {
467+ WithMaxQueueSize (0 ),
468+ WithMaxExportBatchSize (2000 ),
474469 },
475470 wantNumSpans : 1000 ,
476471 wantBatchCount : 1 ,
@@ -545,7 +540,7 @@ func (e indefiniteExporter) Shutdown(context.Context) error {
545540 return nil
546541}
547542
548- func (e indefiniteExporter ) ExportSpans (ctx context.Context , _ []sdktrace. ReadOnlySpan ) error {
543+ func (e indefiniteExporter ) ExportSpans (ctx context.Context , _ []ReadOnlySpan ) error {
549544 <- e .stop
550545 return ctx .Err ()
551546}
@@ -555,7 +550,7 @@ func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
555550 // Cancel the context
556551 cancel ()
557552
558- bsp := sdktrace . NewBatchSpanProcessor (newIndefiniteExporter (t ))
553+ bsp := NewBatchSpanProcessor (newIndefiniteExporter (t ))
559554 t .Cleanup (func () {
560555 assert .NoError (t , bsp .Shutdown (context .Background ()))
561556 })
@@ -568,7 +563,7 @@ func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
568563func TestBatchSpanProcessorForceFlushTimeout (t * testing.T ) {
569564 tp := basicTracerProvider (t )
570565 exp := newIndefiniteExporter (t )
571- bsp := sdktrace . NewBatchSpanProcessor (exp )
566+ bsp := NewBatchSpanProcessor (exp )
572567 tp .RegisterSpanProcessor (bsp )
573568 tr := tp .Tracer (t .Name ())
574569 _ , span := tr .Start (context .Background (), "foo" )
@@ -586,11 +581,10 @@ func TestBatchSpanProcessorForceFlushTimeout(t *testing.T) {
586581func TestBatchSpanProcessorForceFlushQueuedSpans (t * testing.T ) {
587582 ctx := context .Background ()
588583
589- exp := tracetest .NewInMemoryExporter ()
590-
591- tp := sdktrace .NewTracerProvider (
592- sdktrace .WithBatcher (exp ),
593- )
584+ var bp testBatchExporter
585+ bsp := NewBatchSpanProcessor (& bp )
586+ tp := basicTracerProvider (t )
587+ tp .RegisterSpanProcessor (bsp )
594588 t .Cleanup (func () {
595589 assert .NoError (t , tp .Shutdown (context .Background ()))
596590 })
@@ -604,14 +598,14 @@ func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
604598 err := tp .ForceFlush (ctx )
605599 assert .NoError (t , err )
606600
607- assert .Len (t , exp . GetSpans () , i + 1 )
601+ assert .Len (t , bp . spans , i + 1 )
608602 }
609603}
610604
611605func TestBatchSpanProcessorConcurrentSafe (t * testing.T ) {
612606 ctx := context .Background ()
613607 var bp testBatchExporter
614- bsp := sdktrace . NewBatchSpanProcessor (& bp )
608+ bsp := NewBatchSpanProcessor (& bp )
615609 tp := basicTracerProvider (t )
616610 tp .RegisterSpanProcessor (bsp )
617611 tr := tp .Tracer (t .Name ())
@@ -650,62 +644,3 @@ func TestBatchSpanProcessorConcurrentSafe(t *testing.T) {
650644
651645 wg .Wait ()
652646}
653-
654- func BenchmarkSpanProcessorOnEnd (b * testing.B ) {
655- for _ , bb := range []struct {
656- batchSize int
657- spansCount int
658- }{
659- {batchSize : 10 , spansCount : 10 },
660- {batchSize : 10 , spansCount : 100 },
661- {batchSize : 100 , spansCount : 10 },
662- {batchSize : 100 , spansCount : 100 },
663- } {
664- b .Run (fmt .Sprintf ("batch: %d, spans: %d" , bb .batchSize , bb .spansCount ), func (b * testing.B ) {
665- bsp := sdktrace .NewBatchSpanProcessor (
666- tracetest .NewNoopExporter (),
667- sdktrace .WithMaxExportBatchSize (bb .batchSize ),
668- )
669- b .Cleanup (func () {
670- _ = bsp .Shutdown (context .Background ())
671- })
672- snap := tracetest.SpanStub {}.Snapshot ()
673-
674- b .ResetTimer ()
675- b .ReportAllocs ()
676- for i := 0 ; i < b .N ; i ++ {
677- // Ensure the export happens for every run
678- for j := 0 ; j < bb .spansCount ; j ++ {
679- bsp .OnEnd (snap )
680- }
681- }
682- })
683- }
684- }
685-
686- func BenchmarkSpanProcessorVerboseLogging (b * testing.B ) {
687- b .Cleanup (func (l logr.Logger ) func () {
688- return func () { global .SetLogger (l ) }
689- }(global .GetLogger ()))
690- global .SetLogger (funcr .New (func (prefix , args string ) {}, funcr.Options {Verbosity : 5 }))
691- tp := sdktrace .NewTracerProvider (
692- sdktrace .WithBatcher (
693- tracetest .NewNoopExporter (),
694- sdktrace .WithMaxExportBatchSize (10 ),
695- ))
696- b .Cleanup (func () {
697- _ = tp .Shutdown (context .Background ())
698- })
699- tracer := tp .Tracer ("bench" )
700- ctx := context .Background ()
701-
702- b .ResetTimer ()
703- b .ReportAllocs ()
704-
705- for i := 0 ; i < b .N ; i ++ {
706- for j := 0 ; j < 10 ; j ++ {
707- _ , span := tracer .Start (ctx , "bench" )
708- span .End ()
709- }
710- }
711- }
0 commit comments