You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-12-01 23:12:29 +02:00
Refactor configs (#1882)
* trace: Refactor sampling config * tracer: Refactor TracerProviderConfig * Update the changelog * Refactor sdk/metric/controller/basic config * Refactor sdk/metric/processor/basic config * Refactor sdk/resource config * Refactor oteltest config * Refactor exporters/otlp configs * Refactor exporters/stdout config * Refactor exporters/trace/jaeger configs * Refactor exporters/trace/zipkin config * Unexport stdout.NewConfig * Refactor zipkin.go * Refactor provider.go
This commit is contained in:
@@ -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) {
|
||||
opts.processors = append(opts.processors, sp)
|
||||
}
|
||||
return traceProviderOptionFunc(func(cfg *tracerProviderConfig) {
|
||||
cfg.processors = append(cfg.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) {
|
||||
opts.resource = resource.Merge(resource.Environment(), r)
|
||||
}
|
||||
return traceProviderOptionFunc(func(cfg *tracerProviderConfig) {
|
||||
cfg.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(cfg *tracerProviderConfig) {
|
||||
if g != nil {
|
||||
opts.idGenerator = g
|
||||
cfg.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(cfg *tracerProviderConfig) {
|
||||
if s != nil {
|
||||
opts.sampler = s
|
||||
cfg.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) {
|
||||
opts.spanLimits = sl
|
||||
}
|
||||
return traceProviderOptionFunc(func(cfg *tracerProviderConfig) {
|
||||
cfg.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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user