Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,16 @@ will likely want to build custom options for the configuration, the `config`
should be exported. Please, include in the documentation for the `config`
how the user can extend the configuration.

It is important that `config` are not shared across package boundaries.
Meaning a `config` from one package should not be directly used by another.
It is important that internal `config` are not shared across package boundaries.
Meaning a `config` from one package should not be directly used by another. The
one exception is the API packages. The configs from the base API, eg.
`go.opentelemetry.io/otel/sdk/trace.TracerConfig` and
`go.opentelemetry.io/otel/metric.InstrumentConfig`, are intended to be consumed
by the SDK therefor it is expected that these are exported.

When a config is exported we want to maintain forward and backward
compatibility, to achieve this all fields should not be exported but should
instead be accessed by methods.

Optionally, it is common to include a `newConfig` function (with the same
naming scheme). This function wraps any defaults setting and looping over
Expand Down
2 changes: 1 addition & 1 deletion internal/global/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T

key := il{
name: name,
version: trace.NewTracerConfig(opts...).InstrumentationVersion,
version: trace.NewTracerConfig(opts...).InstrumentationVersion(),
}

if p.tracers == nil {
Expand Down
3 changes: 0 additions & 3 deletions internal/global/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func TestTraceProviderDelegates(t *testing.T) {
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
called = true
assert.Equal(t, "abc", name)
assert.Equal(t, []trace.TracerOption{trace.WithInstrumentationVersion("xyz")}, opts)
return trace.NewNoopTracerProvider().Tracer("")
},
})
Expand Down Expand Up @@ -131,7 +130,6 @@ func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) {
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
newVal := atomic.AddInt32(&called, 1)
assert.Equal(t, "abc", name)
assert.Equal(t, []trace.TracerOption{trace.WithInstrumentationVersion("xyz")}, opts)
if newVal == 10 {
// Signal the goroutine to finish.
close(quit)
Expand Down Expand Up @@ -175,7 +173,6 @@ func TestTracerDelegatesConcurrentSafe(t *testing.T) {
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
assert.Equal(t, "abc", name)
assert.Equal(t, []trace.TracerOption{trace.WithInstrumentationVersion("xyz")}, opts)
return fnTracer{
start: func(ctx context.Context, spanName string, opts ...trace.SpanOption) (context.Context, trace.Span) {
newVal := atomic.AddInt32(&called, 1)
Expand Down
4 changes: 2 additions & 2 deletions oteltest/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func (p *TracerProvider) Tracer(instName string, opts ...trace.TracerOption) tra

inst := instrumentation{
Name: instName,
Version: conf.InstrumentationVersion,
Version: conf.InstrumentationVersion(),
}
p.tracersMu.Lock()
defer p.tracersMu.Unlock()
t, ok := p.tracers[inst]
if !ok {
t = &Tracer{
Name: instName,
Version: conf.InstrumentationVersion,
Version: conf.InstrumentationVersion(),
config: &p.config,
}
p.tracers[inst] = t
Expand Down
2 changes: 1 addition & 1 deletion sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
}
il := instrumentation.Library{
Name: name,
Version: c.InstrumentationVersion,
Version: c.InstrumentationVersion(),
}
t, ok := p.namedTracer[il]
if !ok {
Expand Down
51 changes: 25 additions & 26 deletions trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,34 @@ import (
"go.opentelemetry.io/otel/attribute"
)

// TracerConfig is a group of options for a Tracer.
// tracerConfig is a group of options for a Tracer.
type TracerConfig struct {
// InstrumentationVersion is the version of the library providing
// instrumentation.
InstrumentationVersion string
instrumentationVersion string
}

// InstrumentationVersion returns the version of the library providing instrumentation.
func (t *TracerConfig) InstrumentationVersion() string {
return t.instrumentationVersion
}

// NewTracerConfig applies all the options to a returned TracerConfig.
func NewTracerConfig(options ...TracerOption) *TracerConfig {
config := new(TracerConfig)
for _, option := range options {
option.ApplyTracer(config)
option.apply(config)
}
return config
}

// TracerOption applies an option to a TracerConfig.
type TracerOption interface {
ApplyTracer(*TracerConfig)
apply(*TracerConfig)
}

// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
type tracerOptionFunc func(*TracerConfig)

func (fn tracerOptionFunc) apply(cfg *TracerConfig) {
fn(cfg)
}

// SpanConfig is a group of options for a Span.
Expand Down Expand Up @@ -84,6 +88,13 @@ type SpanOption interface {
private()
}

type EventConfig struct {
// Attributes describe the associated qualities of a Event.
Attributes []attribute.KeyValue
// Timestamp is a time in a Event was recorded.
Timestamp time.Time
}

// NewEventConfig applies all the EventOptions to a returned SpanConfig. If no
// timestamp option is passed, the returned SpanConfig will have a Timestamp
// set to the call time, otherwise no validation is performed on the returned
Expand Down Expand Up @@ -185,21 +196,9 @@ func WithSpanKind(kind SpanKind) SpanOption {
return spanKindSpanOption(kind)
}

// InstrumentationOption is an interface for applying instrumentation specific
// options.
type InstrumentationOption interface {
TracerOption
}

// WithInstrumentationVersion sets the instrumentation version.
func WithInstrumentationVersion(version string) InstrumentationOption {
return instrumentationVersionOption(version)
func WithInstrumentationVersion(version string) TracerOption {
return tracerOptionFunc(func(cfg *TracerConfig) {
cfg.instrumentationVersion = version
})
}

type instrumentationVersionOption string

func (i instrumentationVersionOption) ApplyTracer(config *TracerConfig) {
config.InstrumentationVersion = string(i)
}

func (instrumentationVersionOption) private() {}
4 changes: 2 additions & 2 deletions trace/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestTracerConfig(t *testing.T) {
WithInstrumentationVersion(v1),
},
&TracerConfig{
InstrumentationVersion: v1,
instrumentationVersion: v1,
},
},
{
Expand All @@ -201,7 +201,7 @@ func TestTracerConfig(t *testing.T) {
WithInstrumentationVersion(v2),
},
&TracerConfig{
InstrumentationVersion: v2,
instrumentationVersion: v2,
},
},
}
Expand Down