1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-27 22:49:15 +02:00

SDK: Multiple Unregister should not trigger multiple shutdown call (#176)

* SDK: Multiple Unregister should not trigger multiple shutdown call

* fix a data race: waitGroup.Add() should be outside of a goroutine
This commit is contained in:
Artem Kartasov
2019-10-09 13:31:29 +07:00
committed by rghetia
parent be8fb0b4e2
commit ffbef6a007
3 changed files with 12 additions and 9 deletions

View File

@@ -104,9 +104,9 @@ func NewBatchSpanProcessor(e export.SpanBatcher, opts ...BatchSpanProcessorOptio
//Start timer to export metrics //Start timer to export metrics
ticker := time.NewTicker(bsp.o.ScheduledDelayMillis) ticker := time.NewTicker(bsp.o.ScheduledDelayMillis)
bsp.stopWait.Add(1)
go func(ctx context.Context) { go func(ctx context.Context) {
defer ticker.Stop() defer ticker.Stop()
bsp.stopWait.Add(1)
for { for {
select { select {
case <-bsp.stopCh: case <-bsp.stopCh:

View File

@@ -38,7 +38,7 @@ type SpanProcessor interface {
Shutdown() Shutdown()
} }
type spanProcessorMap map[SpanProcessor]struct{} type spanProcessorMap map[SpanProcessor]*sync.Once
var ( var (
mu sync.Mutex mu sync.Mutex
@@ -56,7 +56,7 @@ func RegisterSpanProcessor(e SpanProcessor) {
new[k] = v new[k] = v
} }
} }
new[e] = struct{}{} new[e] = &sync.Once{}
spanProcessors.Store(new) spanProcessors.Store(new)
} }
@@ -71,7 +71,11 @@ func UnregisterSpanProcessor(s SpanProcessor) {
new[k] = v new[k] = v
} }
} }
if stopOnce, ok := new[s]; ok && stopOnce != nil {
stopOnce.Do(func() {
s.Shutdown()
})
}
delete(new, s) delete(new, s)
spanProcessors.Store(new) spanProcessors.Store(new)
s.Shutdown()
} }

View File

@@ -117,7 +117,7 @@ func TestSpanProcessorShutdown(t *testing.T) {
t.Fatalf("Error creating new instance of TestSpanProcessor\n") t.Fatalf("Error creating new instance of TestSpanProcessor\n")
} }
wantCount := sp.shutdownCount + 1 wantCount := 1
sp.Shutdown() sp.Shutdown()
gotCount := sp.shutdownCount gotCount := sp.shutdownCount
@@ -127,13 +127,13 @@ func TestSpanProcessorShutdown(t *testing.T) {
} }
func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) { func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
name := "Increment shutdown counter after each UnregisterSpanProcessor call" name := "Increment shutdown counter after first UnregisterSpanProcessor call"
sp := NewTestSpanProcessor() sp := NewTestSpanProcessor()
if sp == nil { if sp == nil {
t.Fatalf("Error creating new instance of TestSpanProcessor\n") t.Fatalf("Error creating new instance of TestSpanProcessor\n")
} }
wantCount := sp.shutdownCount + 1 wantCount := 1
sdktrace.RegisterSpanProcessor(sp) sdktrace.RegisterSpanProcessor(sp)
sdktrace.UnregisterSpanProcessor(sp) sdktrace.UnregisterSpanProcessor(sp)
@@ -143,8 +143,7 @@ func TestMultipleUnregisterSpanProcessorCalls(t *testing.T) {
t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount) t.Errorf("%s: wrong counter: got %d, want %d\n", name, gotCount, wantCount)
} }
// Multiple UnregisterSpanProcessor triggers multiple Shutdown calls. // Multiple UnregisterSpanProcessor should not trigger multiple Shutdown calls.
wantCount = wantCount + 1
sdktrace.UnregisterSpanProcessor(sp) sdktrace.UnregisterSpanProcessor(sp)
gotCount = sp.shutdownCount gotCount = sp.shutdownCount