From 3811e3449f4582d0d7d578698c809214b334ced4 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 16 Nov 2021 08:57:10 -0800 Subject: [PATCH] Handle nil testSpanProcessor (#2400) 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. --- sdk/trace/span_processor_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/sdk/trace/span_processor_test.go b/sdk/trace/span_processor_test.go index 83f74902a58..cb8c2e4f522 100644 --- a/sdk/trace/span_processor_test.go +++ b/sdk/trace/span_processor_test.go @@ -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{ { @@ -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 } @@ -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 @@ -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