Closed flc1125 closed 15 hours ago
Can you provide a use-case for this? All of our uses have not needed this functionality.
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.
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 82.1%. Comparing base (
e98ef1b
) to head (1c7d242
). Report is 1 commits behind head on main.
In the same package,
InMemoryExporter
has a Reset method. So applying the same toSpanRecorder
sounds sensible.
We have also a Reset
here: https://pkg.go.dev/go.opentelemetry.io/otel/log/logtest#Recorder.Reset
We probably need a Reset method to reuse it for testing.
I'm not in favor of adding additional surface area to the API based on possibilities. I would like there to be valid use cases instead of arguments about symmetry and hypothetical that motivate us.
We probably need a Reset method to reuse it for testing.
I'm not in favor of adding additional surface area to the API based on possibilities. I would like there to be valid use cases instead of arguments about symmetry and hypothetical that motivate us.
I'll mention the background for adding this feature:
When I was writing the unit tests for otelgin, initially I wanted to implement logic similar to the following:
package main
import (
"testing"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func TestOtel(t *testing.T) {
imsb := tracetest.NewInMemoryExporter()
otel.SetTracerProvider(trace.NewTracerProvider(
trace.WithSyncer(imsb),
))
t.Run("test1", func(t *testing.T) {
defer imsb.Reset()
// do something...
// check imsb...
})
t.Run("test2", func(t *testing.T) {
defer imsb.Reset()
// do something...
// check imsb...
})
}
Because, I hope that imsb
and otel.SetTracerProvider
only need to be set once for convenience in subsequent repeated use.
However, I found that a large amount of test logic uses SpanRecorder
, based on the "herd mentality", I also adjusted to use SpanRecorder
. But I discovered that it does not support the Reset()
method.
Therefore, this PR came into being.
I think the tracetest
package should be used for unit testing, since InMemoryExporter
has it, and logtest
has it too. I feel that SpanExporter
could also have it.
When I was writing the unit tests for otelgin, initially I wanted to implement logic similar to the following:
package main import ( "testing" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace/tracetest" ) func TestOtel(t *testing.T) { imsb := tracetest.NewInMemoryExporter() otel.SetTracerProvider(trace.NewTracerProvider( trace.WithSyncer(imsb), )) t.Run("test1", func(t *testing.T) { defer imsb.Reset() // do something... // check imsb... }) t.Run("test2", func(t *testing.T) { defer imsb.Reset() // do something... // check imsb... }) }
Thanks for the use-case.
I would recommend not using the global in testing, but that is a review for another PR. :wink:
We probably need a Reset method to reuse it for testing. Just like
InMemoryExporter
.https://github.com/open-telemetry/opentelemetry-go/blob/b99d2b81783dd3d27201393fc0e741a6fb4a8d6b/sdk/trace/tracetest/exporter.go#L61-L65
ex:
output: