Skip to content

Commit

Permalink
Move trace api package into otel (#1229)
Browse files Browse the repository at this point in the history
* Move trace API to otel

* Move tracetest to oteltest

* Update package documentation

* Remove old api/trace package

* Lint

* Add changes to CHANGELOG

* Add tests for rest of trace API

* Apply suggestions from code review

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Documentation fixes

Includes resolutions for review issues.

* Correct CHANGELOG post release

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
  • Loading branch information
MrAlias and Aneurysm9 authored Oct 9, 2020
1 parent 9ebca88 commit 27c84d6
Show file tree
Hide file tree
Showing 74 changed files with 1,723 additions and 1,671 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Changed

- Move the `go.opentelemetry.io/otel/api/trace` package into `go.opentelemetry.io/otel` with the following changes. (#1229)
- `ID` has been renamed to `TraceID`.
- `IDFromHex` has been renamed to `TraceIDFromHex`.
- `ErrorOption` has been changed to an interface to conform with project design standards which included adding a `NewErrorConfig` function.
- `EmptySpanContext` is removed.
- Move the `go.opentelemetry.io/otel/api/trace/tracetest` package into `go.opentelemetry.io/otel/oteltest`. (#1229)

## [0.13.0] - 2020-10-08

### Added
Expand Down
38 changes: 19 additions & 19 deletions api/apitest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
"time"

"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/internal/matchers"
"go.opentelemetry.io/otel/label"
Expand All @@ -36,7 +36,7 @@ func NewHarness(t *testing.T) *Harness {
}
}

func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
func (h *Harness) TestTracer(subjectFactory func() otel.Tracer) {
h.t.Run("#Start", func(t *testing.T) {
t.Run("propagates the original context", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -76,8 +76,8 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
ctx, span := subject.Start(context.Background(), "test")

e.Expect(span).NotToBeNil()
e.Expect(span.SpanContext()).NotToEqual(trace.EmptySpanContext())
e.Expect(trace.SpanFromContext(ctx)).ToEqual(span)
e.Expect(span.SpanContext()).NotToEqual(otel.SpanContext{})
e.Expect(otel.SpanFromContext(ctx)).ToEqual(span)
})

t.Run("starts spans with unique trace and span IDs", func(t *testing.T) {
Expand All @@ -102,7 +102,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
e := matchers.NewExpecter(t)
subject := subjectFactory()

_, span := subject.Start(context.Background(), "span", trace.WithRecord())
_, span := subject.Start(context.Background(), "span", otel.WithRecord())

e.Expect(span.IsRecording()).ToBeTrue()
})
Expand Down Expand Up @@ -130,7 +130,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()

ctx, parent := subject.Start(context.Background(), "parent")
_, child := subject.Start(ctx, "child", trace.WithNewRoot())
_, child := subject.Start(ctx, "child", otel.WithNewRoot())

psc := parent.SpanContext()
csc := child.SpanContext()
Expand All @@ -146,7 +146,7 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()

_, remoteParent := subject.Start(context.Background(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
parentCtx := otel.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
_, child := subject.Start(parentCtx, "child")

psc := remoteParent.SpanContext()
Expand All @@ -163,8 +163,8 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()

_, remoteParent := subject.Start(context.Background(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
_, child := subject.Start(parentCtx, "child", trace.WithNewRoot())
parentCtx := otel.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
_, child := subject.Start(parentCtx, "child", otel.WithNewRoot())

psc := remoteParent.SpanContext()
csc := child.SpanContext()
Expand All @@ -177,29 +177,29 @@ func (h *Harness) TestTracer(subjectFactory func() trace.Tracer) {
h.testSpan(subjectFactory)
}

func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
var methods = map[string]func(span trace.Span){
"#End": func(span trace.Span) {
func (h *Harness) testSpan(tracerFactory func() otel.Tracer) {
var methods = map[string]func(span otel.Span){
"#End": func(span otel.Span) {
span.End()
},
"#AddEvent": func(span trace.Span) {
"#AddEvent": func(span otel.Span) {
span.AddEvent(context.Background(), "test event")
},
"#AddEventWithTimestamp": func(span trace.Span) {
"#AddEventWithTimestamp": func(span otel.Span) {
span.AddEventWithTimestamp(context.Background(), time.Now(), "test event")
},
"#SetStatus": func(span trace.Span) {
"#SetStatus": func(span otel.Span) {
span.SetStatus(codes.Error, "internal")
},
"#SetName": func(span trace.Span) {
"#SetName": func(span otel.Span) {
span.SetName("new name")
},
"#SetAttributes": func(span trace.Span) {
"#SetAttributes": func(span otel.Span) {
span.SetAttributes(label.String("key1", "value"), label.Int("key2", 123))
},
}
var mechanisms = map[string]func() trace.Span{
"Span created via Tracer#Start": func() trace.Span {
var mechanisms = map[string]func() otel.Span{
"Span created via Tracer#Start": func() otel.Span {
tracer := tracerFactory()
_, subject := tracer.Start(context.Background(), "test")

Expand Down
7 changes: 3 additions & 4 deletions api/global/internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/api/trace"
)

type (
tracerProviderHolder struct {
tp trace.TracerProvider
tp otel.TracerProvider
}

meterProviderHolder struct {
Expand All @@ -47,12 +46,12 @@ var (
)

// TracerProvider is the internal implementation for global.TracerProvider.
func TracerProvider() trace.TracerProvider {
func TracerProvider() otel.TracerProvider {
return globalTracer.Load().(tracerProviderHolder).tp
}

// SetTracerProvider is the internal implementation for global.SetTracerProvider.
func SetTracerProvider(tp trace.TracerProvider) {
func SetTracerProvider(tp otel.TracerProvider) {
delegateTraceOnce.Do(func() {
current := TracerProvider()
if current == tp {
Expand Down
26 changes: 13 additions & 13 deletions api/global/internal/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"context"
"sync"

"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/trace/noop"
)

Expand All @@ -47,12 +47,12 @@ type tracerProvider struct {
mtx sync.Mutex
tracers []*tracer

delegate trace.TracerProvider
delegate otel.TracerProvider
}

// Compile-time guarantee that tracerProvider implements the TracerProvider
// interface.
var _ trace.TracerProvider = &tracerProvider{}
var _ otel.TracerProvider = &tracerProvider{}

// setDelegate configures p to delegate all TracerProvider functionality to
// provider.
Expand All @@ -62,7 +62,7 @@ var _ trace.TracerProvider = &tracerProvider{}
//
// Delegation only happens on the first call to this method. All subsequent
// calls result in no delegation changes.
func (p *tracerProvider) setDelegate(provider trace.TracerProvider) {
func (p *tracerProvider) setDelegate(provider otel.TracerProvider) {
if p.delegate != nil {
return
}
Expand All @@ -79,7 +79,7 @@ func (p *tracerProvider) setDelegate(provider trace.TracerProvider) {
}

// Tracer implements TracerProvider.
func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
func (p *tracerProvider) Tracer(name string, opts ...otel.TracerOption) otel.Tracer {
p.mtx.Lock()
defer p.mtx.Unlock()

Expand All @@ -92,20 +92,20 @@ func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
return t
}

// tracer is a placeholder for a trace.Tracer.
// tracer is a placeholder for a otel.Tracer.
//
// All Tracer functionality is forwarded to a delegate once configured.
// Otherwise, all functionality is forwarded to a NoopTracer.
type tracer struct {
once sync.Once
name string
opts []trace.TracerOption
opts []otel.TracerOption

delegate trace.Tracer
delegate otel.Tracer
}

// Compile-time guarantee that tracer implements the trace.Tracer interface.
var _ trace.Tracer = &tracer{}
// Compile-time guarantee that tracer implements the otel.Tracer interface.
var _ otel.Tracer = &tracer{}

// setDelegate configures t to delegate all Tracer functionality to Tracers
// created by provider.
Expand All @@ -114,13 +114,13 @@ var _ trace.Tracer = &tracer{}
//
// Delegation only happens on the first call to this method. All subsequent
// calls result in no delegation changes.
func (t *tracer) setDelegate(provider trace.TracerProvider) {
func (t *tracer) setDelegate(provider otel.TracerProvider) {
t.once.Do(func() { t.delegate = provider.Tracer(t.name, t.opts...) })
}

// Start implements trace.Tracer by forwarding the call to t.delegate if
// Start implements otel.Tracer by forwarding the call to t.delegate if
// set, otherwise it forwards the call to a NoopTracer.
func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanOption) (context.Context, trace.Span) {
func (t *tracer) Start(ctx context.Context, name string, opts ...otel.SpanOption) (context.Context, otel.Span) {
if t.delegate != nil {
return t.delegate.Start(ctx, name, opts...)
}
Expand Down
8 changes: 4 additions & 4 deletions api/global/internal/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/global/internal"
"go.opentelemetry.io/otel/api/trace/tracetest"
"go.opentelemetry.io/otel/oteltest"
)

func TestTraceWithSDK(t *testing.T) {
Expand All @@ -34,8 +34,8 @@ func TestTraceWithSDK(t *testing.T) {
// This is started before an SDK was registered and should be dropped.
_, span1 := tracer1.Start(ctx, "span1")

sr := new(tracetest.StandardSpanRecorder)
tp := tracetest.NewTracerProvider(tracetest.WithSpanRecorder(sr))
sr := new(oteltest.StandardSpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
global.SetTracerProvider(tp)

// This span was started before initialization, it is expected to be dropped.
Expand All @@ -50,7 +50,7 @@ func TestTraceWithSDK(t *testing.T) {
_, span3 := tracer2.Start(ctx, "span3")
span3.End()

filterNames := func(spans []*tracetest.Span) []string {
filterNames := func(spans []*oteltest.Span) []string {
names := make([]string, len(spans))
for i := range spans {
names[i] = spans[i].Name()
Expand Down
8 changes: 4 additions & 4 deletions api/global/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package global

import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/api/global/internal"
"go.opentelemetry.io/otel/api/trace"
)

// Tracer creates a named tracer that implements Tracer interface.
// If the name is an empty string then provider uses default name.
//
// This is short for TracerProvider().Tracer(name)
func Tracer(name string) trace.Tracer {
func Tracer(name string) otel.Tracer {
return TracerProvider().Tracer(name)
}

Expand All @@ -34,11 +34,11 @@ func Tracer(name string) trace.Tracer {
// tracer := global.TracerProvider().Tracer("example.com/foo")
// or
// tracer := global.Tracer("example.com/foo")
func TracerProvider() trace.TracerProvider {
func TracerProvider() otel.TracerProvider {
return internal.TracerProvider()
}

// SetTracerProvider registers `tp` as the global trace provider.
func SetTracerProvider(tp trace.TracerProvider) {
func SetTracerProvider(tp otel.TracerProvider) {
internal.SetTracerProvider(tp)
}
8 changes: 4 additions & 4 deletions api/global/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ package global_test
import (
"testing"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/internal/trace/noop"
)

type testTracerProvider struct{}

var _ trace.TracerProvider = &testTracerProvider{}
var _ otel.TracerProvider = &testTracerProvider{}

func (*testTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
func (*testTracerProvider) Tracer(_ string, _ ...otel.TracerOption) otel.Tracer {
return noop.Tracer
}

func TestMultipleGlobalTracerProvider(t *testing.T) {
p1 := testTracerProvider{}
p2 := trace.NoopTracerProvider()
p2 := otel.NewNoopTracerProvider()
global.SetTracerProvider(&p1)
global.SetTracerProvider(p2)

Expand Down
Loading

0 comments on commit 27c84d6

Please sign in to comment.