Skip to content

Commit

Permalink
Update Tracer configuration.
Browse files Browse the repository at this point in the history
Conform to API option design outlined in #536.

Add tests to validate new TracerConfigure function

Drop the `instrumentation` prefix.
  • Loading branch information
MrAlias committed Aug 28, 2020
1 parent 1c3626e commit 96eac89
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 18 deletions.
52 changes: 34 additions & 18 deletions api/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
57 changes: 57 additions & 0 deletions api/trace/traceprovider_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 96eac89

Please sign in to comment.