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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

flc1125
Copy link

@flc1125 flc1125 commented Nov 21, 2024

We probably need a Reset method to reuse it for testing. Just like InMemoryExporter.

func (imsb *InMemoryExporter) Reset() {
imsb.mu.Lock()
defer imsb.mu.Unlock()
imsb.ss = nil
}

ex:

package main

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"

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

func TestReset(t *testing.T) {
	sr := tracetest.NewSpanRecorder()
	tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
	ctx := context.Background()
	tracer := tp.Tracer("test")

	// test one:
	_, span := tracer.Start(ctx, "one")
	span.End()

	assert.Len(t, sr.Ended(), 1)
	t.Logf("the one span length: %d", len(sr.Ended()))

	// test two:
	sr.Reset() // <=== I don't want the result to be affected by one.
	_, span = tracer.Start(ctx, "two")
	span.End()

	assert.Len(t, sr.Ended(), 1) // <=== I don't want the result to be affected by one.
	t.Logf("the two span length: %d", len(sr.Ended()))
}

output:

=== RUN   TestReset
    main_test.go:24: the one span length: 1
    main_test.go:32: the two span length: 1
--- PASS: TestReset (0.00s)
PASS

Because it is a tool used for testing, I think such operations may occur.

@MrAlias
Copy link
Contributor

MrAlias commented Nov 21, 2024

Can you provide a use-case for this? All of our uses have not needed this functionality.

@flc1125
Copy link
Author

flc1125 commented Nov 22, 2024

Can you provide a use-case for this? All of our uses have not needed this functionality.

I have already added it to the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants