Skip to content

Commit

Permalink
tracer: Refactor TracerProviderConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed May 13, 2021
1 parent 0b7590c commit 969e907
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.
//
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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())
}
Expand Down

0 comments on commit 969e907

Please sign in to comment.