You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-06-25 00:16:49 +02:00
switch atomic.Value to atomic.Pointer for spanProcessorStates (#3926)
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
@ -75,7 +75,7 @@ func (cfg tracerProviderConfig) MarshalLog() interface{} {
|
|||||||
type TracerProvider struct {
|
type TracerProvider struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
namedTracer map[instrumentation.Scope]*tracer
|
namedTracer map[instrumentation.Scope]*tracer
|
||||||
spanProcessors atomic.Value
|
spanProcessors atomic.Pointer[spanProcessorStates]
|
||||||
isShutdown bool
|
isShutdown bool
|
||||||
|
|
||||||
// These fields are not protected by the lock mu. They are assumed to be
|
// These fields are not protected by the lock mu. They are assumed to be
|
||||||
@ -123,7 +123,7 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
|
|||||||
for _, sp := range o.processors {
|
for _, sp := range o.processors {
|
||||||
spss = append(spss, newSpanProcessorState(sp))
|
spss = append(spss, newSpanProcessorState(sp))
|
||||||
}
|
}
|
||||||
tp.spanProcessors.Store(spss)
|
tp.spanProcessors.Store(&spss)
|
||||||
|
|
||||||
return tp
|
return tp
|
||||||
}
|
}
|
||||||
@ -168,9 +168,9 @@ func (p *TracerProvider) RegisterSpanProcessor(sp SpanProcessor) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
newSPS := spanProcessorStates{}
|
newSPS := spanProcessorStates{}
|
||||||
newSPS = append(newSPS, p.spanProcessors.Load().(spanProcessorStates)...)
|
newSPS = append(newSPS, *(p.spanProcessors.Load())...)
|
||||||
newSPS = append(newSPS, newSpanProcessorState(sp))
|
newSPS = append(newSPS, newSpanProcessorState(sp))
|
||||||
p.spanProcessors.Store(newSPS)
|
p.spanProcessors.Store(&newSPS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors.
|
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors.
|
||||||
@ -180,7 +180,7 @@ func (p *TracerProvider) UnregisterSpanProcessor(sp SpanProcessor) {
|
|||||||
if p.isShutdown {
|
if p.isShutdown {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
old := p.spanProcessors.Load().(spanProcessorStates)
|
old := *(p.spanProcessors.Load())
|
||||||
if len(old) == 0 {
|
if len(old) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -209,13 +209,13 @@ func (p *TracerProvider) UnregisterSpanProcessor(sp SpanProcessor) {
|
|||||||
spss[len(spss)-1] = nil
|
spss[len(spss)-1] = nil
|
||||||
spss = spss[:len(spss)-1]
|
spss = spss[:len(spss)-1]
|
||||||
|
|
||||||
p.spanProcessors.Store(spss)
|
p.spanProcessors.Store(&spss)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForceFlush immediately exports all spans that have not yet been exported for
|
// ForceFlush immediately exports all spans that have not yet been exported for
|
||||||
// all the registered span processors.
|
// all the registered span processors.
|
||||||
func (p *TracerProvider) ForceFlush(ctx context.Context) error {
|
func (p *TracerProvider) ForceFlush(ctx context.Context) error {
|
||||||
spss := p.spanProcessors.Load().(spanProcessorStates)
|
spss := *(p.spanProcessors.Load())
|
||||||
if len(spss) == 0 {
|
if len(spss) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -243,7 +243,7 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p.isShutdown = true
|
p.isShutdown = true
|
||||||
spss := p.spanProcessors.Load().(spanProcessorStates)
|
spss := *(p.spanProcessors.Load())
|
||||||
|
|
||||||
var retErr error
|
var retErr error
|
||||||
for _, sps := range spss {
|
for _, sps := range spss {
|
||||||
@ -266,7 +266,7 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.spanProcessors.Store(spanProcessorStates{})
|
p.spanProcessors.Store(&spanProcessorStates{})
|
||||||
return retErr
|
return retErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ func TestRegisterAfterShutdownWithoutProcessors(t *testing.T) {
|
|||||||
|
|
||||||
sp := &basicSpanProcessor{}
|
sp := &basicSpanProcessor{}
|
||||||
stp.RegisterSpanProcessor(sp) // no-op
|
stp.RegisterSpanProcessor(sp) // no-op
|
||||||
assert.Empty(t, stp.spanProcessors.Load().(spanProcessorStates))
|
assert.Empty(t, stp.spanProcessors.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRegisterAfterShutdownWithProcessors(t *testing.T) {
|
func TestRegisterAfterShutdownWithProcessors(t *testing.T) {
|
||||||
@ -146,11 +146,11 @@ func TestRegisterAfterShutdownWithProcessors(t *testing.T) {
|
|||||||
err := stp.Shutdown(context.Background())
|
err := stp.Shutdown(context.Background())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, stp.isShutdown)
|
assert.True(t, stp.isShutdown)
|
||||||
assert.Empty(t, stp.spanProcessors.Load().(spanProcessorStates))
|
assert.Empty(t, stp.spanProcessors.Load())
|
||||||
|
|
||||||
sp2 := &basicSpanProcessor{}
|
sp2 := &basicSpanProcessor{}
|
||||||
stp.RegisterSpanProcessor(sp2) // no-op
|
stp.RegisterSpanProcessor(sp2) // no-op
|
||||||
assert.Empty(t, stp.spanProcessors.Load().(spanProcessorStates))
|
assert.Empty(t, stp.spanProcessors.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
|
func TestTracerProviderSamplerConfigFromEnv(t *testing.T) {
|
||||||
|
@ -410,7 +410,7 @@ func (s *recordingSpan) End(options ...trace.SpanEndOption) {
|
|||||||
}
|
}
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
sps := s.tracer.provider.spanProcessors.Load().(spanProcessorStates)
|
sps := *(s.tracer.provider.spanProcessors.Load())
|
||||||
if len(sps) == 0 {
|
if len(sps) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanS
|
|||||||
|
|
||||||
s := tr.newSpan(ctx, name, &config)
|
s := tr.newSpan(ctx, name, &config)
|
||||||
if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
|
if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
|
||||||
sps := tr.provider.spanProcessors.Load().(spanProcessorStates)
|
sps := *(tr.provider.spanProcessors.Load())
|
||||||
for _, sp := range sps {
|
for _, sp := range sps {
|
||||||
sp.sp.OnStart(ctx, rw)
|
sp.sp.OnStart(ctx, rw)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user