Skip to content

Commit

Permalink
Update internal/global/trace testing (#2111)
Browse files Browse the repository at this point in the history
* Update internal/global/trace testing

Use existing testing types instead of the oteltest package.

* Make precommit
  • Loading branch information
MrAlias authored Jul 22, 2021
1 parent 7f10ef7 commit db81d4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.15
require (
github.com/google/go-cmp v0.5.6
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/otel/oteltest v1.0.0-RC1
go.opentelemetry.io/otel/trace v1.0.0-RC1
)

Expand Down
76 changes: 44 additions & 32 deletions internal/global/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,61 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/oteltest"
"go.opentelemetry.io/otel/trace"
)

func TestTraceWithSDK(t *testing.T) {
type fnTracerProvider struct {
tracer func(string, ...trace.TracerOption) trace.Tracer
}

func (fn fnTracerProvider) Tracer(instrumentationName string, opts ...trace.TracerOption) trace.Tracer {
return fn.tracer(instrumentationName, opts...)
}

type fnTracer struct {
start func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
}

func (fn fnTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return fn.start(ctx, spanName, opts...)
}

func TestTraceProviderDelegation(t *testing.T) {
global.ResetForTest()

// Map of tracers to expected span names.
expected := map[string][]string{
"pre": {"span2"},
"post": {"span3"},
"fromSpan": {"span4"},
}

ctx := context.Background()
gtp := otel.GetTracerProvider()
tracer1 := gtp.Tracer("pre")
// This is started before an SDK was registered and should be dropped.
_, span1 := tracer1.Start(ctx, "span1")

sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
otel.SetTracerProvider(tp)
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
spans, ok := expected[name]
assert.Truef(t, ok, "invalid tracer: %s", name)
return fnTracer{
start: func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
if ok {
if len(spans) == 0 {
t.Errorf("unexpected span: %s", spanName)
} else {
var want string
want, spans = spans[0], spans[1:]
assert.Equal(t, want, spanName)
}
}
return trace.NewNoopTracerProvider().Tracer(name).Start(ctx, spanName)
},
}
},
})

// This span was started before initialization, it is expected to be dropped.
span1.End()
Expand All @@ -56,33 +95,6 @@ func TestTraceWithSDK(t *testing.T) {
// The noop-span should still provide access to a usable TracerProvider.
_, span4 := span1.TracerProvider().Tracer("fromSpan").Start(ctx, "span4")
span4.End()

filterNames := func(spans []*oteltest.Span) []string {
names := make([]string, len(spans))
for i := range spans {
names[i] = spans[i].Name()
}
return names
}
expected := []string{"span2", "span3", "span4"}
assert.ElementsMatch(t, expected, filterNames(sr.Started()))
assert.ElementsMatch(t, expected, filterNames(sr.Completed()))
}

type fnTracerProvider struct {
tracer func(string, ...trace.TracerOption) trace.Tracer
}

func (fn fnTracerProvider) Tracer(instrumentationName string, opts ...trace.TracerOption) trace.Tracer {
return fn.tracer(instrumentationName, opts...)
}

type fnTracer struct {
start func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
}

func (fn fnTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return fn.start(ctx, spanName, opts...)
}

func TestTraceProviderDelegates(t *testing.T) {
Expand Down

0 comments on commit db81d4a

Please sign in to comment.