diff --git a/CHANGELOG.md b/CHANGELOG.md index 027d7d74d0c..6d63f2da913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/bridge/opencensus/bridge_test.go b/bridge/opencensus/bridge_test.go index 91b334066e0..917e483098f 100644 --- a/bridge/opencensus/bridge_test.go +++ b/bridge/opencensus/bridge_test.go @@ -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) @@ -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")) @@ -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) @@ -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) @@ -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")) @@ -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")) diff --git a/internal/global/trace_test.go b/internal/global/trace_test.go index 6e53c4594b5..8eca6001f18 100644 --- a/internal/global/trace_test.go +++ b/internal/global/trace_test.go @@ -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) diff --git a/oteltest/config.go b/oteltest/config.go index 5ef080424fc..e147eb03c3a 100644 --- a/oteltest/config.go +++ b/oteltest/config.go @@ -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 { @@ -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) { @@ -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 @@ -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)) @@ -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)) diff --git a/oteltest/doc.go b/oteltest/doc.go index 48ed724a581..8711adc468e 100644 --- a/oteltest/doc.go +++ b/oteltest/doc.go @@ -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" diff --git a/oteltest/tracer_test.go b/oteltest/tracer_test.go index f0e9ce6994a..b4b9e776bc4 100644 --- a/oteltest/tracer_test.go +++ b/oteltest/tracer_test.go @@ -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") @@ -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