diff --git a/sdk/trace/provider.go b/sdk/trace/provider.go index 83e1fe5e8d37..aa7c14f9da5a 100644 --- a/sdk/trace/provider.go +++ b/sdk/trace/provider.go @@ -31,11 +31,13 @@ const ( defaultTracerName = "go.opentelemetry.io/otel/sdk/tracer" ) -// TODO (MrAlias): unify this API option design: -// https://github.com/open-telemetry/opentelemetry-go/issues/536 - -// TracerProviderConfig -type TracerProviderConfig struct { +// 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. @@ -51,8 +53,6 @@ type TracerProviderConfig struct { resource *resource.Resource } -type TracerProviderOption func(*TracerProviderConfig) - type TracerProvider struct { mu sync.Mutex namedTracer map[instrumentation.Library]*tracer @@ -76,10 +76,10 @@ var _ trace.TracerProvider = &TracerProvider{} // The passed opts are used to override these default values and configure the // returned TracerProvider appropriately. func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider { - o := &TracerProviderConfig{} + o := &tracerProviderConfig{} for _, opt := range opts { - opt(o) + opt.apply(o) } ensureValidTracerProviderConfig(o) @@ -234,6 +234,16 @@ func (p *TracerProvider) Shutdown(ctx context.Context) error { return nil } +type TracerProviderOption interface { + apply(*tracerProviderConfig) +} + +type traceProviderOptionFunc func(*tracerProviderConfig) + +func (fn traceProviderOptionFunc) apply(cfg *tracerProviderConfig) { + fn(cfg) +} + // WithSyncer registers the exporter with the TracerProvider using a // SimpleSpanProcessor. // @@ -254,9 +264,9 @@ func WithBatcher(e SpanExporter, opts ...BatchSpanProcessorOption) TracerProvide // WithSpanProcessor registers the SpanProcessor with a TracerProvider. func WithSpanProcessor(sp SpanProcessor) TracerProviderOption { - return func(opts *TracerProviderConfig) { + return traceProviderOptionFunc(func(opts *tracerProviderConfig) { opts.processors = append(opts.processors, sp) - } + }) } // WithResource returns a TracerProviderOption that will configure the @@ -267,9 +277,9 @@ func WithSpanProcessor(sp SpanProcessor) TracerProviderOption { // If this option is not used, the TracerProvider will use the // resource.Default() Resource by default. func WithResource(r *resource.Resource) TracerProviderOption { - return func(opts *TracerProviderConfig) { + return traceProviderOptionFunc(func(opts *tracerProviderConfig) { opts.resource = resource.Merge(resource.Environment(), r) - } + }) } // WithIDGenerator returns a TracerProviderOption that will configure the @@ -280,11 +290,11 @@ func WithResource(r *resource.Resource) TracerProviderOption { // If this option is not used, the TracerProvider will use a random number // IDGenerator by default. func WithIDGenerator(g IDGenerator) TracerProviderOption { - return func(opts *TracerProviderConfig) { + return traceProviderOptionFunc(func(opts *tracerProviderConfig) { if g != nil { opts.idGenerator = g } - } + }) } // WithSampler returns a TracerProviderOption that will configure the Sampler @@ -295,11 +305,11 @@ func WithIDGenerator(g IDGenerator) TracerProviderOption { // If this option is not used, the TracerProvider will use a // ParentBased(AlwaysSample) Sampler by default. func WithSampler(s Sampler) TracerProviderOption { - return func(opts *TracerProviderConfig) { + return traceProviderOptionFunc(func(opts *tracerProviderConfig) { if s != nil { opts.sampler = s } - } + }) } // WithSpanLimits returns a TracerProviderOption that will configure the @@ -310,13 +320,13 @@ func WithSampler(s Sampler) TracerProviderOption { // If this option is not used, the TracerProvider will use the default // SpanLimits. func WithSpanLimits(sl SpanLimits) TracerProviderOption { - return func(opts *TracerProviderConfig) { + return traceProviderOptionFunc(func(opts *tracerProviderConfig) { opts.spanLimits = sl - } + }) } // ensureValidTracerProviderConfig ensures that given TracerProviderConfig is valid. -func ensureValidTracerProviderConfig(cfg *TracerProviderConfig) { +func ensureValidTracerProviderConfig(cfg *tracerProviderConfig) { if cfg.sampler == nil { cfg.sampler = ParentBased(AlwaysSample()) }