Skip to content

Commit

Permalink
Handle nil testSpanProcessor (#2400)
Browse files Browse the repository at this point in the history
Remove nil check on return from NewTestSpanProcessor as it can never be
nil, addressing #2396. Also, add nil checks for testSpanProcessor
methods to prevent panics.
  • Loading branch information
MrAlias committed Nov 16, 2021
1 parent d962ec0 commit 3811e34
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions sdk/trace/span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type testSpanProcessor struct {
}

func (t *testSpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWriteSpan) {
if t == nil {
return
}
psc := trace.SpanContextFromContext(parent)
kv := []attribute.KeyValue{
{
Expand All @@ -55,15 +58,24 @@ func (t *testSpanProcessor) OnStart(parent context.Context, s sdktrace.ReadWrite
}

func (t *testSpanProcessor) OnEnd(s sdktrace.ReadOnlySpan) {
if t == nil {
return
}
t.spansEnded = append(t.spansEnded, s)
}

func (t *testSpanProcessor) Shutdown(_ context.Context) error {
if t == nil {
return nil
}
t.shutdownCount++
return nil
}

func (t *testSpanProcessor) ForceFlush(context.Context) error {
if t == nil {
return nil
}
return nil
}

Expand Down Expand Up @@ -205,9 +217,6 @@ func TestSpanProcessorShutdown(t *testing.T) {
name := "Increment shutdown counter of a span processor"
tp := basicTracerProvider(t)
sp := NewTestSpanProcessor("sp")
if sp == nil {
t.Fatalf("Error creating new instance of TestSpanProcessor\n")
}
tp.RegisterSpanProcessor(sp)

wantCount := 1
Expand All @@ -226,9 +235,6 @@ func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
name := "Increment shutdown counter after first UnregisterSpanProcessor call"
tp := basicTracerProvider(t)
sp := NewTestSpanProcessor("sp")
if sp == nil {
t.Fatalf("Error creating new instance of TestSpanProcessor\n")
}

wantCount := 1

Expand Down

0 comments on commit 3811e34

Please sign in to comment.