diff --git a/api/trace/api.go b/api/trace/api.go index 19528d43bda..4695bd9ba4e 100644 --- a/api/trace/api.go +++ b/api/trace/api.go @@ -24,30 +24,46 @@ import ( type Provider interface { // Tracer creates an implementation of the Tracer interface. - // The instrumentationName must be the name of the library providing - // instrumentation. This name may be the same as the instrumented code - // only if that code provides built-in instrumentation. If the - // instrumentationName is empty, then a implementation defined default - // name will be used instead. - Tracer(instrumentationName string, opts ...TracerOption) Tracer + // The passed name must be the package name of the instrumentation + // library, this is the library that provids instrumentation. It should + // not be the name of the library being instrumented unless that code + // itself is providing built-in instrumentation. If the name is empty, + // then an implementation defined default name will be used instead. + Tracer(name string, options ...TracerOption) Tracer } -// TODO (MrAlias): unify this API option design: -// https://github.com/open-telemetry/opentelemetry-go/issues/536 - -// TracerConfig contains options for a Tracer. +// TracerConfig is a group of options for a Tracer. type TracerConfig struct { - InstrumentationVersion string + // Version is the version of the instrumentation library. + Version string } -// TracerOption configures a TracerConfig option. -type TracerOption func(*TracerConfig) - -// WithInstrumentationVersion sets the instrumentation version for a Tracer. -func WithInstrumentationVersion(version string) TracerOption { - return func(c *TracerConfig) { - c.InstrumentationVersion = version +// TracerConfigure applies all the options to a returned TracerConfig. +// The default value for all the fields of the returned TracerConfig are the +// default zero value of the type. Also, this does not perform any validation +// on the returned TracerConfig (e.g. no uniqueness checking or bounding of +// data), instead it is left to the implementations of the SDK to perform this +// action. +func TracerConfigure(options []TracerOption) *TracerConfig { + config := new(TracerConfig) + for _, option := range options { + option.Apply(config) } + return config +} + +// TracerOption applies an options to a TracerConfig. +type TracerOption interface { + Apply(*TracerConfig) +} + +type versionTracerOption string + +func (o versionTracerOption) Apply(c *TracerConfig) { c.Version = string(o) } + +// WithVersion sets the instrumentation version for a Tracer. +func WithVersion(version string) TracerOption { + return versionTracerOption(version) } type Tracer interface { diff --git a/api/trace/traceprovider_test.go b/api/trace/traceprovider_test.go new file mode 100644 index 00000000000..766017409a5 --- /dev/null +++ b/api/trace/traceprovider_test.go @@ -0,0 +1,57 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package trace + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTracerConfigure(t *testing.T) { + v1 := "semver:0.0.1" + v2 := "semver:1.0.0" + tests := []struct { + options []TracerOption + expected *TracerConfig + }{ + { + // No non-zero-values should be set. + []TracerOption{}, + new(TracerConfig), + }, + { + []TracerOption{ + WithVersion(v1), + }, + &TracerConfig{ + Version: v1, + }, + }, + { + []TracerOption{ + // Multiple calls should overwrite. + WithVersion(v1), + WithVersion(v2), + }, + &TracerConfig{ + Version: v2, + }, + }, + } + for _, test := range tests { + assert.Equal(t, test.expected, TracerConfigure(test.options)) + } +}