1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-30 21:20:04 +02:00

Make oteltest.SpanRecorder into a concrete type (#1542)

* Make oteltest.SpanRecorder into a concrete time

* Fixes

* Fix PR #

* Re-run

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Punya Biswal 2021-02-17 10:31:59 -05:00 committed by GitHub
parent 7d0e3e52b6
commit 0b1a1c7237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 28 deletions

View File

@ -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)

View File

@ -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"))

View File

@ -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)

View File

@ -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))

View File

@ -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"

View File

@ -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