Skip to content

feat(trace): add concurrent-safe Reset method to SpanRecorder #5994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 25, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- Add `Reset` method to `SpanRecorder` in `go.opentelemetry.io/otel/sdk/trace/tracetest`. (#5994)

### Changed

- The default global API now supports full auto-instrumentation from the `go.opentelemetry.io/auto` package.
Expand Down
13 changes: 13 additions & 0 deletions sdk/trace/tracetest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan {
return dst
}

// Reset clears the recorded spans.
//
// This method is safe to be called concurrently.
func (sr *SpanRecorder) Reset() {
sr.startedMu.Lock()
sr.endedMu.Lock()
defer sr.startedMu.Unlock()
defer sr.endedMu.Unlock()

sr.started = nil
sr.ended = nil
}

// Ended returns a copy of all ended spans that have been recorded.
//
// This method is safe to be called concurrently.
Expand Down
24 changes: 24 additions & 0 deletions sdk/trace/tracetest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,27 @@ func TestStartingConcurrentSafe(t *testing.T) {

assert.Len(t, sr.Started(), 2)
}

func TestResetConcurrentSafe(t *testing.T) {
sr := NewSpanRecorder()
ctx := context.Background()

runConcurrently(
func() { sr.OnStart(ctx, new(rwSpan)) },
func() { sr.OnStart(ctx, new(rwSpan)) },
func() { sr.OnEnd(new(roSpan)) },
func() { sr.OnEnd(new(roSpan)) },
)

assert.Len(t, sr.Started(), 2)
assert.Len(t, sr.Ended(), 2)

runConcurrently(
func() { sr.Reset() },
func() { sr.Reset() },
func() { sr.Reset() },
)

assert.Empty(t, sr.Started())
assert.Empty(t, sr.Ended())
}
Loading