// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package trace // import "go.opentelemetry.io/otel/sdk/trace"

import (
	
	
	
	
	

	
	
	
	
	
	
	
	semconv 
	
	
	
	
)

const (
	defaultTracerName = "go.opentelemetry.io/otel/sdk/tracer"
	selfObsScopeName  = "go.opentelemetry.io/otel/sdk/trace"
)

// tracerProviderConfig.
type tracerProviderConfig struct {
	// processors contains collection of SpanProcessors that are processing pipeline
	// for spans in the trace signal.
	// SpanProcessors registered with a TracerProvider and are called at the start
	// and end of a Span's lifecycle, and are called in the order they are
	// registered.
	processors []SpanProcessor

	// sampler is the default sampler used when creating new spans.
	sampler Sampler

	// idGenerator is used to generate all Span and Trace IDs when needed.
	idGenerator IDGenerator

	// spanLimits defines the attribute, event, and link limits for spans.
	spanLimits SpanLimits

	// resource contains attributes representing an entity that produces telemetry.
	resource *resource.Resource
}

// MarshalLog is the marshaling function used by the logging system to represent this Provider.
func ( tracerProviderConfig) () any {
	return struct {
		  []SpanProcessor
		     string
		 string
		      SpanLimits
		        *resource.Resource
	}{
		:  .processors,
		:     fmt.Sprintf("%T", .sampler),
		: fmt.Sprintf("%T", .idGenerator),
		:      .spanLimits,
		:        .resource,
	}
}

// TracerProvider is an OpenTelemetry TracerProvider. It provides Tracers to
// instrumentation so it can trace operational flow through a system.
type TracerProvider struct {
	embedded.TracerProvider

	mu             sync.Mutex
	namedTracer    map[instrumentation.Scope]*tracer
	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.
	sampler     Sampler
	idGenerator IDGenerator
	spanLimits  SpanLimits
	resource    *resource.Resource
}

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.
//
// The passed opts are used to override these default values and configure the
// returned TracerProvider appropriately.
func ( ...TracerProviderOption) *TracerProvider {
	 := tracerProviderConfig{
		spanLimits: NewSpanLimits(),
	}
	 = applyTracerProviderEnvConfigs()

	for ,  := range  {
		 = .apply()
	}

	 = ensureValidTracerProviderConfig()

	 := &TracerProvider{
		namedTracer: make(map[instrumentation.Scope]*tracer),
		sampler:     .sampler,
		idGenerator: .idGenerator,
		spanLimits:  .spanLimits,
		resource:    .resource,
	}
	global.Info("TracerProvider created", "config", )

	 := make(spanProcessorStates, 0, len(.processors))
	for ,  := range .processors {
		 = append(, newSpanProcessorState())
	}
	.spanProcessors.Store(&)

	return 
}

// Tracer returns a Tracer with the given name and options. If a Tracer for
// the given name and options does not exist it is created, otherwise the
// existing Tracer is returned.
//
// If name is empty, DefaultTracerName is used instead.
//
// This method is safe to be called concurrently.
func ( *TracerProvider) ( string,  ...trace.TracerOption) trace.Tracer {
	// This check happens before the mutex is acquired to avoid deadlocking if Tracer() is called from within Shutdown().
	if .isShutdown.Load() {
		return noop.NewTracerProvider().Tracer(, ...)
	}
	 := trace.NewTracerConfig(...)
	if  == "" {
		 = defaultTracerName
	}
	 := instrumentation.Scope{
		Name:       ,
		Version:    .InstrumentationVersion(),
		SchemaURL:  .SchemaURL(),
		Attributes: .InstrumentationAttributes(),
	}

	,  := func() (trace.Tracer, bool) {
		.mu.Lock()
		defer .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 .isShutdown.Load() {
			return noop.NewTracerProvider().Tracer(, ...), true
		}
		,  := .namedTracer[]
		if ! {
			 = &tracer{
				provider:                 ,
				instrumentationScope:     ,
				selfObservabilityEnabled: x.SelfObservability.Enabled(),
			}
			if .selfObservabilityEnabled {
				var  error
				.spanLiveMetric, .spanStartedMetric,  = newInst()
				if  != nil {
					 := "failed to create self-observability metrics for tracer: %w"
					 := fmt.Errorf(, )
					otel.Handle()
				}
			}
			.namedTracer[] = 
		}
		return , 
	}()
	if ! {
		// 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",
			,
			"version",
			.Version,
			"schemaURL",
			.SchemaURL,
			"attributes",
			.Attributes,
		)
	}
	return 
}

func newInst() (otelconv.SDKSpanLive, otelconv.SDKSpanStarted, error) {
	 := otel.GetMeterProvider().Meter(
		selfObsScopeName,
		metric.WithInstrumentationVersion(sdk.Version()),
		metric.WithSchemaURL(semconv.SchemaURL),
	)

	var  error
	,  := otelconv.NewSDKSpanLive()
	 = errors.Join(, )

	,  := otelconv.NewSDKSpanStarted()
	 = errors.Join(, )

	return , , 
}

// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors.
func ( *TracerProvider) ( SpanProcessor) {
	// This check prevents calls during a shutdown.
	if .isShutdown.Load() {
		return
	}
	.mu.Lock()
	defer .mu.Unlock()
	// This check prevents calls after a shutdown.
	if .isShutdown.Load() {
		return
	}

	 := .getSpanProcessors()
	 := make(spanProcessorStates, 0, len()+1)
	 = append(, ...)
	 = append(, newSpanProcessorState())
	.spanProcessors.Store(&)
}

// UnregisterSpanProcessor removes the given SpanProcessor from the list of SpanProcessors.
func ( *TracerProvider) ( SpanProcessor) {
	// This check prevents calls during a shutdown.
	if .isShutdown.Load() {
		return
	}
	.mu.Lock()
	defer .mu.Unlock()
	// This check prevents calls after a shutdown.
	if .isShutdown.Load() {
		return
	}
	 := .getSpanProcessors()
	if len() == 0 {
		return
	}
	 := make(spanProcessorStates, len())
	copy(, )

	// stop the span processor if it is started and remove it from the list
	var  *spanProcessorState
	var  int
	for ,  := range  {
		if .sp ==  {
			 = 
			 = 
		}
	}
	if  != nil {
		.state.Do(func() {
			if  := .Shutdown(context.Background());  != nil {
				otel.Handle()
			}
		})
	}
	if len() > 1 {
		copy([:], [+1:])
	}
	[len()-1] = nil
	 = [:len()-1]

	.spanProcessors.Store(&)
}

// ForceFlush immediately exports all spans that have not yet been exported for
// all the registered span processors.
func ( *TracerProvider) ( context.Context) error {
	 := .getSpanProcessors()
	if len() == 0 {
		return nil
	}

	for ,  := range  {
		select {
		case <-.Done():
			return .Err()
		default:
		}

		if  := .sp.ForceFlush();  != nil {
			return 
		}
	}
	return nil
}

// 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 ( *TracerProvider) ( context.Context) error {
	// This check prevents deadlocks in case of recursive shutdown.
	if .isShutdown.Load() {
		return nil
	}
	.mu.Lock()
	defer .mu.Unlock()
	// This check prevents calls after a shutdown has already been done concurrently.
	if !.isShutdown.CompareAndSwap(false, true) { // did toggle?
		return nil
	}

	var  error
	for ,  := range .getSpanProcessors() {
		select {
		case <-.Done():
			return .Err()
		default:
		}

		var  error
		.state.Do(func() {
			 = .sp.Shutdown()
		})
		if  != nil {
			if  == nil {
				 = 
			} else {
				// Poor man's list of errors
				 = fmt.Errorf("%w; %w", , )
			}
		}
	}
	.spanProcessors.Store(&spanProcessorStates{})
	return 
}

func ( *TracerProvider) () spanProcessorStates {
	return *(.spanProcessors.Load())
}

// TracerProviderOption configures a TracerProvider.
type TracerProviderOption interface {
	apply(tracerProviderConfig) tracerProviderConfig
}

type traceProviderOptionFunc func(tracerProviderConfig) tracerProviderConfig

func ( traceProviderOptionFunc) ( tracerProviderConfig) tracerProviderConfig {
	return ()
}

// WithSyncer registers the exporter with the TracerProvider using a
// SimpleSpanProcessor.
//
// This is not recommended for production use. The synchronous nature of the
// SimpleSpanProcessor that will wrap the exporter make it good for testing,
// debugging, or showing examples of other feature, but it will be slow and
// have a high computation resource usage overhead. The WithBatcher option is
// recommended for production use instead.
func ( SpanExporter) TracerProviderOption {
	return WithSpanProcessor(NewSimpleSpanProcessor())
}

// WithBatcher registers the exporter with the TracerProvider using a
// BatchSpanProcessor configured with the passed opts.
func ( SpanExporter,  ...BatchSpanProcessorOption) TracerProviderOption {
	return WithSpanProcessor(NewBatchSpanProcessor(, ...))
}

// WithSpanProcessor registers the SpanProcessor with a TracerProvider.
func ( SpanProcessor) TracerProviderOption {
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		.processors = append(.processors, )
		return 
	})
}

// WithResource returns a TracerProviderOption that will configure the
// Resource r as a TracerProvider's Resource. The configured Resource is
// referenced by all the Tracers the TracerProvider creates. It represents the
// entity producing telemetry.
//
// If this option is not used, the TracerProvider will use the
// resource.Default() Resource by default.
func ( *resource.Resource) TracerProviderOption {
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		var  error
		.resource,  = resource.Merge(resource.Environment(), )
		if  != nil {
			otel.Handle()
		}
		return 
	})
}

// WithIDGenerator returns a TracerProviderOption that will configure the
// IDGenerator g as a TracerProvider's IDGenerator. The configured IDGenerator
// is used by the Tracers the TracerProvider creates to generate new Span and
// Trace IDs.
//
// If this option is not used, the TracerProvider will use a random number
// IDGenerator by default.
func ( IDGenerator) TracerProviderOption {
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		if  != nil {
			.idGenerator = 
		}
		return 
	})
}

// WithSampler returns a TracerProviderOption that will configure the Sampler
// s as a TracerProvider's Sampler. The configured Sampler is used by the
// Tracers the TracerProvider creates to make their sampling decisions for the
// Spans they create.
//
// This option overrides the Sampler configured through the OTEL_TRACES_SAMPLER
// and OTEL_TRACES_SAMPLER_ARG environment variables. If this option is not used
// and the sampler is not configured through environment variables or the environment
// contains invalid/unsupported configuration, the TracerProvider will use a
// ParentBased(AlwaysSample) Sampler by default.
func ( Sampler) TracerProviderOption {
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		if  != nil {
			.sampler = 
		}
		return 
	})
}

// WithSpanLimits returns a TracerProviderOption that configures a
// TracerProvider to use the SpanLimits sl. These SpanLimits bound any Span
// created by a Tracer from the TracerProvider.
//
// If any field of sl is zero or negative it will be replaced with the default
// value for that field.
//
// If this or WithRawSpanLimits are not provided, the TracerProvider will use
// the limits defined by environment variables, or the defaults if unset.
// Refer to the NewSpanLimits documentation for information about this
// relationship.
//
// Deprecated: Use WithRawSpanLimits instead which allows setting unlimited
// and zero limits. This option will be kept until the next major version
// incremented release.
func ( SpanLimits) TracerProviderOption {
	if .AttributeValueLengthLimit <= 0 {
		.AttributeValueLengthLimit = DefaultAttributeValueLengthLimit
	}
	if .AttributeCountLimit <= 0 {
		.AttributeCountLimit = DefaultAttributeCountLimit
	}
	if .EventCountLimit <= 0 {
		.EventCountLimit = DefaultEventCountLimit
	}
	if .AttributePerEventCountLimit <= 0 {
		.AttributePerEventCountLimit = DefaultAttributePerEventCountLimit
	}
	if .LinkCountLimit <= 0 {
		.LinkCountLimit = DefaultLinkCountLimit
	}
	if .AttributePerLinkCountLimit <= 0 {
		.AttributePerLinkCountLimit = DefaultAttributePerLinkCountLimit
	}
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		.spanLimits = 
		return 
	})
}

// WithRawSpanLimits returns a TracerProviderOption that configures a
// TracerProvider to use these limits. These limits bound any Span created by
// a Tracer from the TracerProvider.
//
// The limits will be used as-is. Zero or negative values will not be changed
// to the default value like WithSpanLimits does. Setting a limit to zero will
// effectively disable the related resource it limits and setting to a
// negative value will mean that resource is unlimited. Consequentially, this
// means that the zero-value SpanLimits will disable all span resources.
// Because of this, limits should be constructed using NewSpanLimits and
// updated accordingly.
//
// If this or WithSpanLimits are not provided, the TracerProvider will use the
// limits defined by environment variables, or the defaults if unset. Refer to
// the NewSpanLimits documentation for information about this relationship.
func ( SpanLimits) TracerProviderOption {
	return traceProviderOptionFunc(func( tracerProviderConfig) tracerProviderConfig {
		.spanLimits = 
		return 
	})
}

func applyTracerProviderEnvConfigs( tracerProviderConfig) tracerProviderConfig {
	for ,  := range tracerProviderOptionsFromEnv() {
		 = .apply()
	}

	return 
}

func tracerProviderOptionsFromEnv() []TracerProviderOption {
	var  []TracerProviderOption

	,  := samplerFromEnv()
	if  != nil {
		otel.Handle()
	}

	if  != nil {
		 = append(, WithSampler())
	}

	return 
}

// ensureValidTracerProviderConfig ensures that given TracerProviderConfig is valid.
func ensureValidTracerProviderConfig( tracerProviderConfig) tracerProviderConfig {
	if .sampler == nil {
		.sampler = ParentBased(AlwaysSample())
	}
	if .idGenerator == nil {
		.idGenerator = defaultIDGenerator()
	}
	if .resource == nil {
		.resource = resource.Default()
	}
	return 
}