1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +02:00

TracerProvider allows calling Tracer() while it's shutting down (#3924)

This commit is contained in:
Mikhail Mazurskiy
2023-03-28 11:05:44 +11:00
committed by GitHub
parent ae90c4402e
commit c4940f3b43
3 changed files with 91 additions and 24 deletions

View File

@@ -46,11 +46,38 @@ func (t *basicSpanProcessor) ForceFlush(context.Context) error {
return nil
}
type shutdownSpanProcessor struct {
shutdown func(context.Context) error
}
func (t *shutdownSpanProcessor) Shutdown(ctx context.Context) error {
return t.shutdown(ctx)
}
func (t *shutdownSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
func (t *shutdownSpanProcessor) OnEnd(ReadOnlySpan) {}
func (t *shutdownSpanProcessor) ForceFlush(context.Context) error {
return nil
}
func TestShutdownCallsTracerMethod(t *testing.T) {
stp := NewTracerProvider()
sp := &shutdownSpanProcessor{
shutdown: func(ctx context.Context) error {
_ = stp.Tracer("abc") // must not deadlock
return nil
},
}
stp.RegisterSpanProcessor(sp)
assert.NoError(t, stp.Shutdown(context.Background()))
assert.True(t, stp.isShutdown.Load())
}
func TestForceFlushAndShutdownTraceProviderWithoutProcessor(t *testing.T) {
stp := NewTracerProvider()
assert.NoError(t, stp.ForceFlush(context.Background()))
assert.NoError(t, stp.Shutdown(context.Background()))
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
}
func TestShutdownTraceProvider(t *testing.T) {
@@ -61,7 +88,7 @@ func TestShutdownTraceProvider(t *testing.T) {
assert.NoError(t, stp.ForceFlush(context.Background()))
assert.True(t, sp.flushed, "error ForceFlush basicSpanProcessor")
assert.NoError(t, stp.Shutdown(context.Background()))
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
assert.True(t, sp.closed, "error Shutdown basicSpanProcessor")
}
@@ -76,7 +103,7 @@ func TestFailedProcessorShutdown(t *testing.T) {
err := stp.Shutdown(context.Background())
assert.Error(t, err)
assert.Equal(t, err, spErr)
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
}
func TestFailedProcessorsShutdown(t *testing.T) {
@@ -97,7 +124,7 @@ func TestFailedProcessorsShutdown(t *testing.T) {
assert.EqualError(t, err, "basic span processor shutdown failure1; basic span processor shutdown failure2")
assert.True(t, sp1.closed)
assert.True(t, sp2.closed)
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
}
func TestFailedProcessorShutdownInUnregister(t *testing.T) {
@@ -114,7 +141,7 @@ func TestFailedProcessorShutdownInUnregister(t *testing.T) {
err := stp.Shutdown(context.Background())
assert.NoError(t, err)
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
}
func TestSchemaURL(t *testing.T) {
@@ -131,7 +158,7 @@ func TestRegisterAfterShutdownWithoutProcessors(t *testing.T) {
stp := NewTracerProvider()
err := stp.Shutdown(context.Background())
assert.NoError(t, err)
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
sp := &basicSpanProcessor{}
stp.RegisterSpanProcessor(sp) // no-op
@@ -145,7 +172,7 @@ func TestRegisterAfterShutdownWithProcessors(t *testing.T) {
stp.RegisterSpanProcessor(sp1)
err := stp.Shutdown(context.Background())
assert.NoError(t, err)
assert.True(t, stp.isShutdown)
assert.True(t, stp.isShutdown.Load())
assert.Empty(t, stp.spanProcessors.Load())
sp2 := &basicSpanProcessor{}