Skip to content
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

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

Merged
merged 11 commits into from
Nov 25, 2024
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.started = nil
sr.startedMu.Unlock()

sr.endedMu.Lock()
defer sr.endedMu.Unlock()
sr.ended = nil
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
}

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