Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added a new `Link` type under the SDK `otel/sdk/trace` package that counts the number of attributes that were dropped for surpassing the `AttributePerLinkCountLimit` configured in the Span's `SpanLimits`.
This new type replaces the equal-named API `Link` type found in the `otel/trace` package for most usages within the SDK.
For example, instances of this type are now returned by the `Links()` function of `ReadOnlySpan`s provided in places like the `OnEnd` function of `SpanProcessor` implementations. (#2118)
- Added the `SpanRecorder` type to the `go.opentelemetry.io/otel/skd/trace/tracetest` package.
This type can be used with the default SDK as a `SpanProcessor` during testing. (#2132)

### Changed

Expand Down
67 changes: 67 additions & 0 deletions sdk/trace/tracetest/recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"

import (
"context"

sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// SpanRecorder records started and ended spans.
type SpanRecorder struct {
started []sdktrace.ReadWriteSpan
ended []sdktrace.ReadOnlySpan
}

var _ sdktrace.SpanProcessor = (*SpanRecorder)(nil)

func NewSpanRecorder() *SpanRecorder {
return new(SpanRecorder)
}

// OnStart records started spans.
func (sr *SpanRecorder) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) {
sr.started = append(sr.started, s)
}

// OnEnd records completed spans.
func (sr *SpanRecorder) OnEnd(s sdktrace.ReadOnlySpan) {
sr.ended = append(sr.ended, s)
}

// Shutdown does nothing.
func (sr *SpanRecorder) Shutdown(context.Context) error {
return nil
}

// ForceFlush does nothing.
func (sr *SpanRecorder) ForceFlush(context.Context) error {
return nil
}

// Started returns a copy of all started spans that have been recorded.
func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan {
Comment thread
MrAlias marked this conversation as resolved.
dst := make([]sdktrace.ReadWriteSpan, len(sr.started))
copy(dst, sr.started)
return dst
}

// Ended returns a copy of all ended spans that have been recorded.
func (sr *SpanRecorder) Ended() []sdktrace.ReadOnlySpan {
dst := make([]sdktrace.ReadOnlySpan, len(sr.ended))
copy(dst, sr.ended)
return dst
}
85 changes: 85 additions & 0 deletions sdk/trace/tracetest/recorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 tracetest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

type rwSpan struct {
sdktrace.ReadWriteSpan
}

func TestSpanRecorderOnStartAppends(t *testing.T) {
s0, s1 := new(rwSpan), new(rwSpan)
ctx := context.Background()
sr := new(SpanRecorder)

assert.Len(t, sr.started, 0)
sr.OnStart(ctx, s0)
assert.Len(t, sr.started, 1)
sr.OnStart(ctx, s1)
assert.Len(t, sr.started, 2)

// Ensure order correct.
started := sr.Started()
assert.Same(t, s0, started[0])
assert.Same(t, s1, started[1])
}

type roSpan struct {
sdktrace.ReadOnlySpan
}

func TestSpanRecorderOnEndAppends(t *testing.T) {
s0, s1 := new(roSpan), new(roSpan)
sr := new(SpanRecorder)

assert.Len(t, sr.ended, 0)
sr.OnEnd(s0)
assert.Len(t, sr.ended, 1)
sr.OnEnd(s1)
assert.Len(t, sr.ended, 2)

// Ensure order correct.
ended := sr.Ended()
assert.Same(t, s0, ended[0])
assert.Same(t, s1, ended[1])
}

func TestSpanRecorderShutdownNoError(t *testing.T) {
ctx := context.Background()
assert.NoError(t, new(SpanRecorder).Shutdown(ctx))

var c context.CancelFunc
ctx, c = context.WithCancel(ctx)
c()
assert.NoError(t, new(SpanRecorder).Shutdown(ctx))
}

func TestSpanRecorderForceFlushNoError(t *testing.T) {
ctx := context.Background()
assert.NoError(t, new(SpanRecorder).ForceFlush(ctx))

var c context.CancelFunc
ctx, c = context.WithCancel(ctx)
c()
assert.NoError(t, new(SpanRecorder).ForceFlush(ctx))
}