You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
Move trace sdk tests from trace_test into trace package (#6400)
I would like to be able to use a private option in https://github.com/open-telemetry/opentelemetry-go/pull/6393 in tests, and decided to split this refactoring out into its own PR. This moves the batch span processor benchmarks into benchmark_test.go, and replaces one instance of the tracetest.NewInMemoryExporter with a different test exporter implementation. It then moves most unit tests from `trace_test` to the main `trace` package.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package trace_test
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -12,22 +12,17 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/logr/funcr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/sdk/internal/env"
|
||||
ottest "go.opentelemetry.io/otel/sdk/internal/internaltest"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type testBatchExporter struct {
|
||||
mu sync.Mutex
|
||||
spans []sdktrace.ReadOnlySpan
|
||||
spans []ReadOnlySpan
|
||||
sizes []int
|
||||
batchCount int
|
||||
shutdownCount int
|
||||
@@ -37,7 +32,7 @@ type testBatchExporter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *testBatchExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
|
||||
func (t *testBatchExporter) ExportSpans(ctx context.Context, spans []ReadOnlySpan) error {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
|
||||
@@ -78,11 +73,11 @@ func (t *testBatchExporter) getBatchCount() int {
|
||||
return t.batchCount
|
||||
}
|
||||
|
||||
var _ sdktrace.SpanExporter = (*testBatchExporter)(nil)
|
||||
var _ SpanExporter = (*testBatchExporter)(nil)
|
||||
|
||||
func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
|
||||
tp := basicTracerProvider(t)
|
||||
bsp := sdktrace.NewBatchSpanProcessor(nil)
|
||||
bsp := NewBatchSpanProcessor(nil)
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
tr := tp.Tracer("NilExporter")
|
||||
|
||||
@@ -90,8 +85,8 @@ func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
|
||||
span.End()
|
||||
|
||||
// These should not panic.
|
||||
bsp.OnStart(context.Background(), span.(sdktrace.ReadWriteSpan))
|
||||
bsp.OnEnd(span.(sdktrace.ReadOnlySpan))
|
||||
bsp.OnStart(context.Background(), span.(ReadWriteSpan))
|
||||
bsp.OnEnd(span.(ReadOnlySpan))
|
||||
if err := bsp.ForceFlush(context.Background()); err != nil {
|
||||
t.Errorf("failed to ForceFlush the BatchSpanProcessor: %v", err)
|
||||
}
|
||||
@@ -102,7 +97,7 @@ func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
|
||||
|
||||
type testOption struct {
|
||||
name string
|
||||
o []sdktrace.BatchSpanProcessorOption
|
||||
o []BatchSpanProcessorOption
|
||||
wantNumSpans int
|
||||
wantBatchCount int
|
||||
genNumSpans int
|
||||
@@ -121,8 +116,8 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "non-default BatchTimeout",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
},
|
||||
wantNumSpans: 2053,
|
||||
wantBatchCount: 4,
|
||||
@@ -130,9 +125,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "non-default MaxQueueSize and BatchTimeout",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
sdktrace.WithMaxQueueSize(200),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
WithMaxQueueSize(200),
|
||||
},
|
||||
wantNumSpans: 205,
|
||||
wantBatchCount: 1,
|
||||
@@ -140,10 +135,10 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "non-default MaxQueueSize, BatchTimeout and MaxExportBatchSize",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
sdktrace.WithMaxQueueSize(205),
|
||||
sdktrace.WithMaxExportBatchSize(20),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
WithMaxQueueSize(205),
|
||||
WithMaxExportBatchSize(20),
|
||||
},
|
||||
wantNumSpans: 210,
|
||||
wantBatchCount: 11,
|
||||
@@ -151,10 +146,10 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "blocking option",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
sdktrace.WithMaxQueueSize(200),
|
||||
sdktrace.WithMaxExportBatchSize(20),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
WithMaxQueueSize(200),
|
||||
WithMaxExportBatchSize(20),
|
||||
},
|
||||
wantNumSpans: 205,
|
||||
wantBatchCount: 11,
|
||||
@@ -162,9 +157,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "parallel span generation",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
sdktrace.WithMaxQueueSize(200),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
WithMaxQueueSize(200),
|
||||
},
|
||||
wantNumSpans: 205,
|
||||
wantBatchCount: 1,
|
||||
@@ -173,9 +168,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "parallel span blocking",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithBatchTimeout(schDelay),
|
||||
sdktrace.WithMaxExportBatchSize(200),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithBatchTimeout(schDelay),
|
||||
WithMaxExportBatchSize(200),
|
||||
},
|
||||
wantNumSpans: 2000,
|
||||
wantBatchCount: 10,
|
||||
@@ -306,7 +301,7 @@ type stuckExporter struct {
|
||||
}
|
||||
|
||||
// ExportSpans waits for ctx to expire and returns that error.
|
||||
func (e *stuckExporter) ExportSpans(ctx context.Context, _ []sdktrace.ReadOnlySpan) error {
|
||||
func (e *stuckExporter) ExportSpans(ctx context.Context, _ []ReadOnlySpan) error {
|
||||
<-ctx.Done()
|
||||
e.err = ctx.Err()
|
||||
return ctx.Err()
|
||||
@@ -314,11 +309,11 @@ func (e *stuckExporter) ExportSpans(ctx context.Context, _ []sdktrace.ReadOnlySp
|
||||
|
||||
func TestBatchSpanProcessorExportTimeout(t *testing.T) {
|
||||
exp := new(stuckExporter)
|
||||
bsp := sdktrace.NewBatchSpanProcessor(
|
||||
bsp := NewBatchSpanProcessor(
|
||||
exp,
|
||||
// Set a non-zero export timeout so a deadline is set.
|
||||
sdktrace.WithExportTimeout(1*time.Microsecond),
|
||||
sdktrace.WithBlocking(),
|
||||
WithExportTimeout(1*time.Microsecond),
|
||||
WithBlocking(),
|
||||
)
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
@@ -332,10 +327,10 @@ func TestBatchSpanProcessorExportTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func createAndRegisterBatchSP(option testOption, te *testBatchExporter) sdktrace.SpanProcessor {
|
||||
func createAndRegisterBatchSP(option testOption, te *testBatchExporter) SpanProcessor {
|
||||
// Always use blocking queue to avoid flaky tests.
|
||||
options := append(option.o, sdktrace.WithBlocking())
|
||||
return sdktrace.NewBatchSpanProcessor(te, options...)
|
||||
options := append(option.o, WithBlocking())
|
||||
return NewBatchSpanProcessor(te, options...)
|
||||
}
|
||||
|
||||
func generateSpan(_ *testing.T, tr trace.Tracer, option testOption) {
|
||||
@@ -382,7 +377,7 @@ func getSpanContext() trace.SpanContext {
|
||||
|
||||
func TestBatchSpanProcessorShutdown(t *testing.T) {
|
||||
var bp testBatchExporter
|
||||
bsp := sdktrace.NewBatchSpanProcessor(&bp)
|
||||
bsp := NewBatchSpanProcessor(&bp)
|
||||
|
||||
err := bsp.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
@@ -401,14 +396,14 @@ func TestBatchSpanProcessorShutdown(t *testing.T) {
|
||||
func TestBatchSpanProcessorPostShutdown(t *testing.T) {
|
||||
tp := basicTracerProvider(t)
|
||||
be := testBatchExporter{}
|
||||
bsp := sdktrace.NewBatchSpanProcessor(&be)
|
||||
bsp := NewBatchSpanProcessor(&be)
|
||||
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
tr := tp.Tracer("Normal")
|
||||
|
||||
generateSpanParallel(t, tr, testOption{
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithMaxExportBatchSize(50),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithMaxExportBatchSize(50),
|
||||
},
|
||||
genNumSpans: 60,
|
||||
})
|
||||
@@ -428,9 +423,9 @@ func TestBatchSpanProcessorForceFlushSucceeds(t *testing.T) {
|
||||
tp := basicTracerProvider(t)
|
||||
option := testOption{
|
||||
name: "default BatchSpanProcessorOptions",
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithMaxQueueSize(0),
|
||||
sdktrace.WithMaxExportBatchSize(3000),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithMaxQueueSize(0),
|
||||
WithMaxExportBatchSize(3000),
|
||||
},
|
||||
wantNumSpans: 2053,
|
||||
wantBatchCount: 1,
|
||||
@@ -468,9 +463,9 @@ func TestBatchSpanProcessorDropBatchIfFailed(t *testing.T) {
|
||||
}
|
||||
tp := basicTracerProvider(t)
|
||||
option := testOption{
|
||||
o: []sdktrace.BatchSpanProcessorOption{
|
||||
sdktrace.WithMaxQueueSize(0),
|
||||
sdktrace.WithMaxExportBatchSize(2000),
|
||||
o: []BatchSpanProcessorOption{
|
||||
WithMaxQueueSize(0),
|
||||
WithMaxExportBatchSize(2000),
|
||||
},
|
||||
wantNumSpans: 1000,
|
||||
wantBatchCount: 1,
|
||||
@@ -545,7 +540,7 @@ func (e indefiniteExporter) Shutdown(context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e indefiniteExporter) ExportSpans(ctx context.Context, _ []sdktrace.ReadOnlySpan) error {
|
||||
func (e indefiniteExporter) ExportSpans(ctx context.Context, _ []ReadOnlySpan) error {
|
||||
<-e.stop
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -555,7 +550,7 @@ func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
|
||||
// Cancel the context
|
||||
cancel()
|
||||
|
||||
bsp := sdktrace.NewBatchSpanProcessor(newIndefiniteExporter(t))
|
||||
bsp := NewBatchSpanProcessor(newIndefiniteExporter(t))
|
||||
t.Cleanup(func() {
|
||||
assert.NoError(t, bsp.Shutdown(context.Background()))
|
||||
})
|
||||
@@ -568,7 +563,7 @@ func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
|
||||
func TestBatchSpanProcessorForceFlushTimeout(t *testing.T) {
|
||||
tp := basicTracerProvider(t)
|
||||
exp := newIndefiniteExporter(t)
|
||||
bsp := sdktrace.NewBatchSpanProcessor(exp)
|
||||
bsp := NewBatchSpanProcessor(exp)
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
tr := tp.Tracer(t.Name())
|
||||
_, span := tr.Start(context.Background(), "foo")
|
||||
@@ -586,11 +581,10 @@ func TestBatchSpanProcessorForceFlushTimeout(t *testing.T) {
|
||||
func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
exp := tracetest.NewInMemoryExporter()
|
||||
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exp),
|
||||
)
|
||||
var bp testBatchExporter
|
||||
bsp := NewBatchSpanProcessor(&bp)
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
t.Cleanup(func() {
|
||||
assert.NoError(t, tp.Shutdown(context.Background()))
|
||||
})
|
||||
@@ -604,14 +598,14 @@ func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
|
||||
err := tp.ForceFlush(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, exp.GetSpans(), i+1)
|
||||
assert.Len(t, bp.spans, i+1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchSpanProcessorConcurrentSafe(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
var bp testBatchExporter
|
||||
bsp := sdktrace.NewBatchSpanProcessor(&bp)
|
||||
bsp := NewBatchSpanProcessor(&bp)
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(bsp)
|
||||
tr := tp.Tracer(t.Name())
|
||||
@@ -650,62 +644,3 @@ func TestBatchSpanProcessorConcurrentSafe(t *testing.T) {
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkSpanProcessorOnEnd(b *testing.B) {
|
||||
for _, bb := range []struct {
|
||||
batchSize int
|
||||
spansCount int
|
||||
}{
|
||||
{batchSize: 10, spansCount: 10},
|
||||
{batchSize: 10, spansCount: 100},
|
||||
{batchSize: 100, spansCount: 10},
|
||||
{batchSize: 100, spansCount: 100},
|
||||
} {
|
||||
b.Run(fmt.Sprintf("batch: %d, spans: %d", bb.batchSize, bb.spansCount), func(b *testing.B) {
|
||||
bsp := sdktrace.NewBatchSpanProcessor(
|
||||
tracetest.NewNoopExporter(),
|
||||
sdktrace.WithMaxExportBatchSize(bb.batchSize),
|
||||
)
|
||||
b.Cleanup(func() {
|
||||
_ = bsp.Shutdown(context.Background())
|
||||
})
|
||||
snap := tracetest.SpanStub{}.Snapshot()
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Ensure the export happens for every run
|
||||
for j := 0; j < bb.spansCount; j++ {
|
||||
bsp.OnEnd(snap)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSpanProcessorVerboseLogging(b *testing.B) {
|
||||
b.Cleanup(func(l logr.Logger) func() {
|
||||
return func() { global.SetLogger(l) }
|
||||
}(global.GetLogger()))
|
||||
global.SetLogger(funcr.New(func(prefix, args string) {}, funcr.Options{Verbosity: 5}))
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(
|
||||
tracetest.NewNoopExporter(),
|
||||
sdktrace.WithMaxExportBatchSize(10),
|
||||
))
|
||||
b.Cleanup(func() {
|
||||
_ = tp.Shutdown(context.Background())
|
||||
})
|
||||
tracer := tp.Tracer("bench")
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < 10; j++ {
|
||||
_, span := tracer.Start(ctx, "bench")
|
||||
span.End()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,8 +9,13 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/logr/funcr"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@@ -325,3 +330,62 @@ func tracer(_ *testing.B, name string, sampler sdktrace.Sampler) trace.Tracer {
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sampler))
|
||||
return tp.Tracer(name)
|
||||
}
|
||||
|
||||
func BenchmarkSpanProcessorOnEnd(b *testing.B) {
|
||||
for _, bb := range []struct {
|
||||
batchSize int
|
||||
spansCount int
|
||||
}{
|
||||
{batchSize: 10, spansCount: 10},
|
||||
{batchSize: 10, spansCount: 100},
|
||||
{batchSize: 100, spansCount: 10},
|
||||
{batchSize: 100, spansCount: 100},
|
||||
} {
|
||||
b.Run(fmt.Sprintf("batch: %d, spans: %d", bb.batchSize, bb.spansCount), func(b *testing.B) {
|
||||
bsp := sdktrace.NewBatchSpanProcessor(
|
||||
tracetest.NewNoopExporter(),
|
||||
sdktrace.WithMaxExportBatchSize(bb.batchSize),
|
||||
)
|
||||
b.Cleanup(func() {
|
||||
_ = bsp.Shutdown(context.Background())
|
||||
})
|
||||
snap := tracetest.SpanStub{}.Snapshot()
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
// Ensure the export happens for every run
|
||||
for j := 0; j < bb.spansCount; j++ {
|
||||
bsp.OnEnd(snap)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSpanProcessorVerboseLogging(b *testing.B) {
|
||||
b.Cleanup(func(l logr.Logger) func() {
|
||||
return func() { global.SetLogger(l) }
|
||||
}(global.GetLogger()))
|
||||
global.SetLogger(funcr.New(func(prefix, args string) {}, funcr.Options{Verbosity: 5}))
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(
|
||||
tracetest.NewNoopExporter(),
|
||||
sdktrace.WithMaxExportBatchSize(10),
|
||||
))
|
||||
b.Cleanup(func() {
|
||||
_ = tp.Shutdown(context.Background())
|
||||
})
|
||||
tracer := tp.Tracer("bench")
|
||||
ctx := context.Background()
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < 10; j++ {
|
||||
_, span := tracer.Start(ctx, "bench")
|
||||
span.End()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package trace_test
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -11,27 +11,19 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
var (
|
||||
tid, _ = trace.TraceIDFromHex("01020304050607080102040810203040")
|
||||
sid, _ = trace.SpanIDFromHex("0102040810203040")
|
||||
)
|
||||
|
||||
type testExporter struct {
|
||||
spans []sdktrace.ReadOnlySpan
|
||||
type simpleTestExporter struct {
|
||||
spans []ReadOnlySpan
|
||||
shutdown bool
|
||||
}
|
||||
|
||||
func (t *testExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
|
||||
func (t *simpleTestExporter) ExportSpans(ctx context.Context, spans []ReadOnlySpan) error {
|
||||
t.spans = append(t.spans, spans...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *testExporter) Shutdown(ctx context.Context) error {
|
||||
func (t *simpleTestExporter) Shutdown(ctx context.Context) error {
|
||||
t.shutdown = true
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -42,39 +34,27 @@ func (t *testExporter) Shutdown(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
var _ sdktrace.SpanExporter = (*testExporter)(nil)
|
||||
var _ SpanExporter = (*simpleTestExporter)(nil)
|
||||
|
||||
func TestNewSimpleSpanProcessor(t *testing.T) {
|
||||
if ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{}); ssp == nil {
|
||||
if ssp := NewSimpleSpanProcessor(&simpleTestExporter{}); ssp == nil {
|
||||
t.Error("failed to create new SimpleSpanProcessor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) {
|
||||
if ssp := sdktrace.NewSimpleSpanProcessor(nil); ssp == nil {
|
||||
if ssp := NewSimpleSpanProcessor(nil); ssp == nil {
|
||||
t.Error("failed to create new SimpleSpanProcessor with nil exporter")
|
||||
}
|
||||
}
|
||||
|
||||
func startSpan(tp trace.TracerProvider) trace.Span {
|
||||
tr := tp.Tracer("SimpleSpanProcessor")
|
||||
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
||||
TraceID: tid,
|
||||
SpanID: sid,
|
||||
TraceFlags: 0x1,
|
||||
})
|
||||
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
|
||||
_, span := tr.Start(ctx, "OnEnd")
|
||||
return span
|
||||
}
|
||||
|
||||
func TestSimpleSpanProcessorOnEnd(t *testing.T) {
|
||||
tp := basicTracerProvider(t)
|
||||
te := testExporter{}
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(&te)
|
||||
te := simpleTestExporter{}
|
||||
ssp := NewSimpleSpanProcessor(&te)
|
||||
|
||||
tp.RegisterSpanProcessor(ssp)
|
||||
startSpan(tp).End()
|
||||
startSpan(tp, "TestSimpleSpanProcessorOnEnd").End()
|
||||
|
||||
wantTraceID := tid
|
||||
gotTraceID := te.spans[0].SpanContext().TraceID()
|
||||
@@ -84,13 +64,13 @@ func TestSimpleSpanProcessorOnEnd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
||||
exporter := &testExporter{}
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
||||
exporter := &simpleTestExporter{}
|
||||
ssp := NewSimpleSpanProcessor(exporter)
|
||||
|
||||
// Ensure we can export a span before we test we cannot after shutdown.
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(ssp)
|
||||
startSpan(tp).End()
|
||||
startSpan(tp, "TestSimpleSpanProcessorShutdown").End()
|
||||
nExported := len(exporter.spans)
|
||||
if nExported != 1 {
|
||||
t.Error("failed to verify span export")
|
||||
@@ -103,15 +83,15 @@ func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
||||
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
|
||||
}
|
||||
|
||||
startSpan(tp).End()
|
||||
startSpan(tp, "TestSimpleSpanProcessorShutdown").End()
|
||||
if len(exporter.spans) > nExported {
|
||||
t.Error("exported span to shutdown exporter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe(t *testing.T) {
|
||||
exporter := &testExporter{}
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
||||
exporter := &simpleTestExporter{}
|
||||
ssp := NewSimpleSpanProcessor(exporter)
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(ssp)
|
||||
|
||||
@@ -126,7 +106,7 @@ func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe(t *testing.T) {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
startSpan(tp).End()
|
||||
startSpan(tp, "TestSimpleSpanProcessorShutdownOnEndConcurrentSafe").End()
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -143,8 +123,8 @@ func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSimpleSpanProcessorShutdownOnEndConcurrentSafe2(t *testing.T) {
|
||||
exporter := &testExporter{}
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
||||
exporter := &simpleTestExporter{}
|
||||
ssp := NewSimpleSpanProcessor(exporter)
|
||||
tp := basicTracerProvider(t)
|
||||
tp.RegisterSpanProcessor(ssp)
|
||||
|
||||
@@ -173,7 +153,7 @@ func TestSimpleSpanProcessorShutdownHonorsContextDeadline(t *testing.T) {
|
||||
defer cancel()
|
||||
<-ctx.Done()
|
||||
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
||||
ssp := NewSimpleSpanProcessor(&simpleTestExporter{})
|
||||
if got, want := ssp.Shutdown(ctx), context.DeadlineExceeded; !errors.Is(got, want) {
|
||||
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
||||
}
|
||||
@@ -183,7 +163,7 @@ func TestSimpleSpanProcessorShutdownHonorsContextCancel(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
||||
ssp := NewSimpleSpanProcessor(&simpleTestExporter{})
|
||||
if got, want := ssp.Shutdown(ctx), context.Canceled; !errors.Is(got, want) {
|
||||
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
||||
}
|
||||
|
@@ -1,25 +1,24 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package trace_test
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type testSpanProcessor struct {
|
||||
name string
|
||||
spansStarted []sdktrace.ReadWriteSpan
|
||||
spansEnded []sdktrace.ReadOnlySpan
|
||||
spansStarted []ReadWriteSpan
|
||||
spansEnded []ReadOnlySpan
|
||||
shutdownCount int
|
||||
}
|
||||
|
||||
func (t *testSpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWriteSpan) {
|
||||
func (t *testSpanProcessor) OnStart(parent context.Context, s ReadWriteSpan) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
@@ -46,7 +45,7 @@ func (t *testSpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWrite
|
||||
t.spansStarted = append(t.spansStarted, s)
|
||||
}
|
||||
|
||||
func (t *testSpanProcessor) OnEnd(s sdktrace.ReadOnlySpan) {
|
||||
func (t *testSpanProcessor) OnEnd(s ReadOnlySpan) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
|
@@ -1,19 +1,17 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package trace_test
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
func basicTracerProvider(t *testing.T) *sdktrace.TracerProvider {
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
func basicTracerProvider(t *testing.T) *TracerProvider {
|
||||
tp := NewTracerProvider(WithSampler(AlwaysSample()))
|
||||
t.Cleanup(func() {
|
||||
assert.NoError(t, tp.Shutdown(context.Background()))
|
||||
})
|
||||
|
Reference in New Issue
Block a user