Skip to content

Commit

Permalink
Update trace API TracerOption (open-telemetry#1109)
Browse files Browse the repository at this point in the history
* Update Tracer configuration.

Conform to API option design outlined in open-telemetry#536.

Add tests to validate new TracerConfigure function

Drop the `instrumentation` prefix.

* Stick with instrumentationVersion for now

* Propagate changes

* Add changes to Changelog
  • Loading branch information
MrAlias authored and evantorrie committed Sep 10, 2020
1 parent 08875a3 commit 2fe8b57
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- In the `go.opentelemetry.io/otel/api/trace` package a new `TracerConfigure` function was added to configure a new `TracerConfig`.
This addition was made to conform with our project option conventions. (#1109)

### Changed

- Add reconnecting udp connection type to Jaeger exporter.
This change adds a new optional implementation of the udp conn interface used to detect changes to an agent's host dns record.
It then adopts the new destination address to ensure the exporter doesn't get stuck. This change was ported from jaegertracing/jaeger-client-go#520. (#1063)
- The `go.opentelemetry.io/otel/api/trace` `TracerOption` was changed to an interface to conform to project option conventions. (#1109)

## [0.11.0] - 2020-08-24

Expand Down
34 changes: 25 additions & 9 deletions api/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,38 @@ type Provider interface {
Tracer(instrumentationName string, opts ...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 is the version of the instrumentation library.
InstrumentationVersion string
}

// TracerOption configures a TracerConfig option.
type TracerOption func(*TracerConfig)
// 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 instVersionTracerOption string

func (o instVersionTracerOption) Apply(c *TracerConfig) { c.InstrumentationVersion = string(o) }

// WithInstrumentationVersion sets the instrumentation version for a Tracer.
func WithInstrumentationVersion(version string) TracerOption {
return func(c *TracerConfig) {
c.InstrumentationVersion = version
}
return instVersionTracerOption(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{
WithInstrumentationVersion(v1),
},
&TracerConfig{
InstrumentationVersion: v1,
},
},
{
[]TracerOption{
// Multiple calls should overwrite.
WithInstrumentationVersion(v1),
WithInstrumentationVersion(v2),
},
&TracerConfig{
InstrumentationVersion: v2,
},
},
}
for _, test := range tests {
assert.Equal(t, test.expected, TracerConfigure(test.options))
}
}
5 changes: 1 addition & 4 deletions api/trace/tracetest/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ type instrumentation struct {
}

func (p *Provider) Tracer(instName string, opts ...trace.TracerOption) trace.Tracer {
conf := new(trace.TracerConfig)
for _, o := range opts {
o(conf)
}
conf := trace.TracerConfigure(opts)
inst := instrumentation{
Name: instName,
Version: conf.InstrumentationVersion,
Expand Down
5 changes: 1 addition & 4 deletions sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ func NewProvider(opts ...ProviderOption) (*Provider, error) {
// Tracer with the given name. If a tracer for the given name does not exist,
// it is created first. If the name is empty, DefaultTracerName is used.
func (p *Provider) Tracer(name string, opts ...apitrace.TracerOption) apitrace.Tracer {
c := new(apitrace.TracerConfig)
for _, o := range opts {
o(c)
}
c := apitrace.TracerConfigure(opts)
p.mu.Lock()
defer p.mu.Unlock()
if name == "" {
Expand Down

0 comments on commit 2fe8b57

Please sign in to comment.