From e8eedf9660e895689817318b0c7018e379320e4e Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Sat, 13 Aug 2022 10:36:20 +1000 Subject: [PATCH] Shut down all processors even on error --- sdk/trace/provider.go | 22 ++++++++++++---------- sdk/trace/provider_test.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/sdk/trace/provider.go b/sdk/trace/provider.go index eac69f34d761..d75570916006 100644 --- a/sdk/trace/provider.go +++ b/sdk/trace/provider.go @@ -90,10 +90,10 @@ var _ trace.TracerProvider = &TracerProvider{} // NewTracerProvider returns a new and configured TracerProvider. // // By default the returned TracerProvider is configured with: -// - a ParentBased(AlwaysSample) Sampler -// - a random number IDGenerator -// - the resource.Default() Resource -// - the default SpanLimits. +// - a ParentBased(AlwaysSample) Sampler +// - a random number IDGenerator +// - the resource.Default() Resource +// - the default SpanLimits. // // The passed opts are used to override these default values and configure the // returned TracerProvider appropriately. @@ -241,10 +241,7 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error { if !ok { return fmt.Errorf("failed to load span processors") } - if len(spss) == 0 { - return nil - } - + var retErr error for _, sps := range spss { select { case <-ctx.Done(): @@ -257,10 +254,15 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error { err = sps.sp.Shutdown(ctx) }) if err != nil { - return err + if retErr == nil { + retErr = err + } else { + // Poor man's list of errors + retErr = fmt.Errorf("%v; %v", retErr, err) + } } } - return nil + return retErr } // TracerProviderOption configures a TracerProvider. diff --git a/sdk/trace/provider_test.go b/sdk/trace/provider_test.go index 39caf5a91df5..769e2da5a16a 100644 --- a/sdk/trace/provider_test.go +++ b/sdk/trace/provider_test.go @@ -72,6 +72,28 @@ func TestFailedProcessorShutdown(t *testing.T) { assert.Equal(t, err, spErr) } +func TestFailedProcessorsShutdown(t *testing.T) { + stp := NewTracerProvider() + spErr1 := errors.New("basic span processor shutdown failure1") + spErr2 := errors.New("basic span processor shutdown failure2") + sp1 := &basicSpanProcesor{ + running: true, + injectShutdownError: spErr1, + } + sp2 := &basicSpanProcesor{ + running: true, + injectShutdownError: spErr2, + } + stp.RegisterSpanProcessor(sp1) + stp.RegisterSpanProcessor(sp2) + + err := stp.Shutdown(context.Background()) + assert.Error(t, err) + assert.Equal(t, err, spErr1) + assert.False(t, sp1.running) + assert.False(t, sp2.running) +} + func TestFailedProcessorShutdownInUnregister(t *testing.T) { handler.Reset() stp := NewTracerProvider()