summaryrefslogtreecommitdiff
path: root/vendor/go.opentelemetry.io/otel/sdk/trace/provider.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/go.opentelemetry.io/otel/sdk/trace/provider.go')
-rw-r--r--vendor/go.opentelemetry.io/otel/sdk/trace/provider.go99
1 files changed, 69 insertions, 30 deletions
diff --git a/vendor/go.opentelemetry.io/otel/sdk/trace/provider.go b/vendor/go.opentelemetry.io/otel/sdk/trace/provider.go
index 201c17817..0a018c14d 100644
--- a/vendor/go.opentelemetry.io/otel/sdk/trace/provider.go
+++ b/vendor/go.opentelemetry.io/otel/sdk/trace/provider.go
@@ -75,8 +75,9 @@ func (cfg tracerProviderConfig) MarshalLog() interface{} {
type TracerProvider struct {
mu sync.Mutex
namedTracer map[instrumentation.Scope]*tracer
- spanProcessors atomic.Value
- isShutdown bool
+ spanProcessors atomic.Pointer[spanProcessorStates]
+
+ isShutdown atomic.Bool
// These fields are not protected by the lock mu. They are assumed to be
// immutable after creation of the TracerProvider.
@@ -119,11 +120,11 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
}
global.Info("TracerProvider created", "config", o)
- spss := spanProcessorStates{}
+ spss := make(spanProcessorStates, 0, len(o.processors))
for _, sp := range o.processors {
spss = append(spss, newSpanProcessorState(sp))
}
- tp.spanProcessors.Store(spss)
+ tp.spanProcessors.Store(&spss)
return tp
}
@@ -136,10 +137,11 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
//
// This method is safe to be called concurrently.
func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
+ // This check happens before the mutex is acquired to avoid deadlocking if Tracer() is called from within Shutdown().
+ if p.isShutdown.Load() {
+ return trace.NewNoopTracerProvider().Tracer(name, opts...)
+ }
c := trace.NewTracerConfig(opts...)
-
- p.mu.Lock()
- defer p.mu.Unlock()
if name == "" {
name = defaultTracerName
}
@@ -148,44 +150,74 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
}
- t, ok := p.namedTracer[is]
- if !ok {
- t = &tracer{
- provider: p,
- instrumentationScope: is,
+
+ t, ok := func() (trace.Tracer, bool) {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ // Must check the flag after acquiring the mutex to avoid returning a valid tracer if Shutdown() ran
+ // after the first check above but before we acquired the mutex.
+ if p.isShutdown.Load() {
+ return trace.NewNoopTracerProvider().Tracer(name, opts...), true
+ }
+ t, ok := p.namedTracer[is]
+ if !ok {
+ t = &tracer{
+ provider: p,
+ instrumentationScope: is,
+ }
+ p.namedTracer[is] = t
}
- p.namedTracer[is] = t
- global.Info("Tracer created", "name", name, "version", c.InstrumentationVersion(), "schemaURL", c.SchemaURL())
+ return t, ok
+ }()
+ if !ok {
+ // This code is outside the mutex to not hold the lock while calling third party logging code:
+ // - That code may do slow things like I/O, which would prolong the duration the lock is held,
+ // slowing down all tracing consumers.
+ // - Logging code may be instrumented with tracing and deadlock because it could try
+ // acquiring the same non-reentrant mutex.
+ global.Info("Tracer created", "name", name, "version", is.Version, "schemaURL", is.SchemaURL)
}
return t
}
// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors.
func (p *TracerProvider) RegisterSpanProcessor(sp SpanProcessor) {
+ // This check prevents calls during a shutdown.
+ if p.isShutdown.Load() {
+ return
+ }
p.mu.Lock()
defer p.mu.Unlock()
- if p.isShutdown {
+ // This check prevents calls after a shutdown.
+ if p.isShutdown.Load() {
return
}
- newSPS := spanProcessorStates{}
- newSPS = append(newSPS, p.spanProcessors.Load().(spanProcessorStates)...)
+
+ current := p.getSpanProcessors()
+ newSPS := make(spanProcessorStates, 0, len(current)+1)
+ newSPS = append(newSPS, current...)
newSPS = append(newSPS, newSpanProcessorState(sp))
- p.spanProcessors.Store(newSPS)
+ p.spanProcessors.Store(&newSPS)
}
// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors.
func (p *TracerProvider) UnregisterSpanProcessor(sp SpanProcessor) {
+ // This check prevents calls during a shutdown.
+ if p.isShutdown.Load() {
+ return
+ }
p.mu.Lock()
defer p.mu.Unlock()
- if p.isShutdown {
+ // This check prevents calls after a shutdown.
+ if p.isShutdown.Load() {
return
}
- old := p.spanProcessors.Load().(spanProcessorStates)
+ old := p.getSpanProcessors()
if len(old) == 0 {
return
}
- spss := spanProcessorStates{}
- spss = append(spss, old...)
+ spss := make(spanProcessorStates, len(old))
+ copy(spss, old)
// stop the span processor if it is started and remove it from the list
var stopOnce *spanProcessorState
@@ -209,13 +241,13 @@ func (p *TracerProvider) UnregisterSpanProcessor(sp SpanProcessor) {
spss[len(spss)-1] = nil
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
// all the registered span processors.
func (p *TracerProvider) ForceFlush(ctx context.Context) error {
- spss := p.spanProcessors.Load().(spanProcessorStates)
+ spss := p.getSpanProcessors()
if len(spss) == 0 {
return nil
}
@@ -236,18 +268,21 @@ func (p *TracerProvider) ForceFlush(ctx context.Context) error {
// Shutdown shuts down TracerProvider. All registered span processors are shut down
// in the order they were registered and any held computational resources are released.
+// After Shutdown is called, all methods are no-ops.
func (p *TracerProvider) Shutdown(ctx context.Context) error {
- spss := p.spanProcessors.Load().(spanProcessorStates)
- if len(spss) == 0 {
+ // This check prevents deadlocks in case of recursive shutdown.
+ if p.isShutdown.Load() {
return nil
}
-
p.mu.Lock()
defer p.mu.Unlock()
- p.isShutdown = true
+ // This check prevents calls after a shutdown has already been done concurrently.
+ if !p.isShutdown.CompareAndSwap(false, true) { // did toggle?
+ return nil
+ }
var retErr error
- for _, sps := range spss {
+ for _, sps := range p.getSpanProcessors() {
select {
case <-ctx.Done():
return ctx.Err()
@@ -267,10 +302,14 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error {
}
}
}
- p.spanProcessors.Store(spanProcessorStates{})
+ p.spanProcessors.Store(&spanProcessorStates{})
return retErr
}
+func (p *TracerProvider) getSpanProcessors() spanProcessorStates {
+ return *(p.spanProcessors.Load())
+}
+
// TracerProviderOption configures a TracerProvider.
type TracerProviderOption interface {
apply(tracerProviderConfig) tracerProviderConfig