Skip to content

Commit

Permalink
Make oteltest.SpanRecorder into a concrete type (open-telemetry#1542)
Browse files Browse the repository at this point in the history
* Make oteltest.SpanRecorder into a concrete time

* Fixes

* Fix PR #

* Re-run

Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
2 people authored and ldelossa committed Mar 5, 2021
1 parent bceda6c commit f108a44
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Changed

- Replaced interface `oteltest.SpanRecorder` with its existing implementation
`StandardSpanRecorder` (#1542).

### Added

- Added `resource.Default()` for use with meter and tracer providers. (#1507)
Expand Down
12 changes: 6 additions & 6 deletions bridge/opencensus/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestMixedAPIs(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
tracer := tp.Tracer("mixedapitracer")
octrace.DefaultTracer = NewTracer(tracer)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestMixedAPIs(t *testing.T) {
}

func TestStartOptions(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
octrace.DefaultTracer = NewTracer(tp.Tracer("startoptionstracer"))

Expand All @@ -94,7 +94,7 @@ func TestStartOptions(t *testing.T) {
}

func TestStartSpanWithRemoteParent(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
tracer := tp.Tracer("remoteparent")
octrace.DefaultTracer = NewTracer(tracer)
Expand All @@ -117,7 +117,7 @@ func TestStartSpanWithRemoteParent(t *testing.T) {
}

func TestToFromContext(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
tracer := tp.Tracer("tofromcontext")
octrace.DefaultTracer = NewTracer(tracer)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestToFromContext(t *testing.T) {
}

func TestIsRecordingEvents(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
octrace.DefaultTracer = NewTracer(tp.Tracer("isrecordingevents"))

Expand All @@ -170,7 +170,7 @@ func TestIsRecordingEvents(t *testing.T) {
}

func TestSetThings(t *testing.T) {
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
octrace.DefaultTracer = NewTracer(tp.Tracer("setthings"))

Expand Down
2 changes: 1 addition & 1 deletion internal/global/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestTraceWithSDK(t *testing.T) {
// This is started before an SDK was registered and should be dropped.
_, span1 := tracer1.Start(ctx, "span1")

sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
otel.SetTracerProvider(tp)

Expand Down
27 changes: 9 additions & 18 deletions oteltest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type config struct {
SpanContextFunc func(context.Context) trace.SpanContext

// SpanRecorder keeps track of spans.
SpanRecorder SpanRecorder
SpanRecorder *SpanRecorder
}

func newConfig(opts ...Option) config {
Expand Down Expand Up @@ -80,7 +80,7 @@ func WithSpanContextFunc(f func(context.Context) trace.SpanContext) Option {
}

type spanRecorderOption struct {
SpanRecorder SpanRecorder
SpanRecorder *SpanRecorder
}

func (o spanRecorderOption) Apply(c *config) {
Expand All @@ -89,22 +89,13 @@ func (o spanRecorderOption) Apply(c *config) {

// WithSpanRecorder sets the SpanRecorder to use with the TracerProvider for
// testing.
func WithSpanRecorder(sr SpanRecorder) Option {
func WithSpanRecorder(sr *SpanRecorder) Option {
return spanRecorderOption{sr}
}

// SpanRecorder performs operations to record a span as it starts and ends.
type SpanRecorder interface {
// OnStart is called by the Tracer when it starts a Span.
OnStart(span *Span)
// OnEnd is called by the Span when it ends.
OnEnd(span *Span)
}

// StandardSpanRecorder is a SpanRecorder that records all started and ended
// spans in an ordered recording. StandardSpanRecorder is designed to be
// concurrent safe and can by used by multiple goroutines.
type StandardSpanRecorder struct {
// It is designed to be concurrent safe and can by used by multiple goroutines.
type SpanRecorder struct {
startedMu sync.RWMutex
started []*Span

Expand All @@ -113,21 +104,21 @@ type StandardSpanRecorder struct {
}

// OnStart records span as started.
func (ssr *StandardSpanRecorder) OnStart(span *Span) {
func (ssr *SpanRecorder) OnStart(span *Span) {
ssr.startedMu.Lock()
defer ssr.startedMu.Unlock()
ssr.started = append(ssr.started, span)
}

// OnEnd records span as completed.
func (ssr *StandardSpanRecorder) OnEnd(span *Span) {
func (ssr *SpanRecorder) OnEnd(span *Span) {
ssr.doneMu.Lock()
defer ssr.doneMu.Unlock()
ssr.done = append(ssr.done, span)
}

// Started returns a copy of all started Spans in the order they were started.
func (ssr *StandardSpanRecorder) Started() []*Span {
func (ssr *SpanRecorder) Started() []*Span {
ssr.startedMu.RLock()
defer ssr.startedMu.RUnlock()
started := make([]*Span, len(ssr.started))
Expand All @@ -138,7 +129,7 @@ func (ssr *StandardSpanRecorder) Started() []*Span {
}

// Completed returns a copy of all ended Spans in the order they were ended.
func (ssr *StandardSpanRecorder) Completed() []*Span {
func (ssr *SpanRecorder) Completed() []*Span {
ssr.doneMu.RLock()
defer ssr.doneMu.RUnlock()
done := make([]*Span, len(ssr.done))
Expand Down
2 changes: 1 addition & 1 deletion oteltest/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ introspection of their state and history. Additionally, a SpanRecorder can be
provided to the TracerProvider to record all Spans started and ended by the
testing structures.
sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
*/
package oteltest // import "go.opentelemetry.io/otel/oteltest"
4 changes: 2 additions & 2 deletions oteltest/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func testTracedSpan(t *testing.T, fn func(tracer trace.Tracer, name string) (tra

e := matchers.NewExpecter(t)

sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
subject := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)).Tracer(t.Name())
subject.Start(context.Background(), "span1")

Expand All @@ -315,7 +315,7 @@ func testTracedSpan(t *testing.T, fn func(tracer trace.Tracer, name string) (tra

e := matchers.NewExpecter(t)

sr := new(oteltest.StandardSpanRecorder)
sr := new(oteltest.SpanRecorder)
subject := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)).Tracer(t.Name())

numSpans := 2
Expand Down

0 comments on commit f108a44

Please sign in to comment.