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

Conversation

flc1125
Copy link
Member

@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
Member 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.

Copy link
Member

@dmathieu dmathieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same package, InMemoryExporter has a Reset method.
So applying the same to SpanRecorder sounds sensible.

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.1%. Comparing base (e98ef1b) to head (1c7d242).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #5994   +/-   ##
=====================================
  Coverage   82.1%   82.1%           
=====================================
  Files        273     273           
  Lines      23616   23624    +8     
=====================================
+ Hits       19399   19406    +7     
- Misses      3872    3873    +1     
  Partials     345     345           

see 3 files with indirect coverage changes

@pellared
Copy link
Member

In the same package, InMemoryExporter has a Reset method. So applying the same to SpanRecorder sounds sensible.

We have also a Reset here: https://pkg.go.dev/go.opentelemetry.io/otel/log/logtest#Recorder.Reset

Copy link
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a changelog entry?

sdk/trace/tracetest/recorder.go Outdated Show resolved Hide resolved
Co-authored-by: Robert Pająk <pellared@hotmail.com>
CHANGELOG.md Outdated Show resolved Hide resolved
sdk/trace/tracetest/recorder.go Outdated Show resolved Hide resolved
@MrAlias
Copy link
Contributor

MrAlias commented Nov 22, 2024

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.

@flc1125
Copy link
Member Author

flc1125 commented Nov 23, 2024

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.

@MrAlias
Copy link
Contributor

MrAlias commented Nov 25, 2024

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. 😉

@pellared pellared merged commit 814a413 into open-telemetry:main Nov 25, 2024
30 checks passed
@MrAlias MrAlias added this to the v1.33.0 milestone Dec 11, 2024
@MrAlias MrAlias mentioned this pull request Dec 12, 2024
MrAlias added a commit that referenced this pull request Dec 12, 2024
### Added

- Add `Reset` method to `SpanRecorder` in
`go.opentelemetry.io/otel/sdk/trace/tracetest`. (#5994)
- Add `EnabledInstrument` interface in
`go.opentelemetry.io/otel/sdk/metric/internal/x`. This is an
experimental interface that is implemented by synchronous instruments
provided by `go.opentelemetry.io/otel/sdk/metric`. Users can use it to
avoid performing computationally expensive operations when recording
measurements. It does not fall within the scope of the OpenTelemetry Go
versioning and stability [policy](./VERSIONING.md) and it may be changed
in backwards incompatible ways or removed in feature releases. (#6016)

### Changed

- The default global API now supports full auto-instrumentation from the
`go.opentelemetry.io/auto` package. See that package for more
information. (#5920)
- Propagate non-retryable error messages to client in
`go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#5929)
- Propagate non-retryable error messages to client in
`go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`.
(#5929)
- Propagate non-retryable error messages to client in
`go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`.
(#5929)
- Performance improvements for attribute value `AsStringSlice`,
`AsFloat64Slice`, `AsInt64Slice`, `AsBoolSlice`. (#6011)
- Change `EnabledParameters` to have a `Severity` field instead of a
getter and setter in `go.opentelemetry.io/otel/log`. (#6009)

### Fixed

- Fix inconsistent request body closing in
`go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#5954)
- Fix inconsistent request body closing in
`go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`.
(#5954)
- Fix inconsistent request body closing in
`go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`.
(#5954)
- Fix invalid exemplar keys in
`go.opentelemetry.io/otel/exporters/prometheus`. (#5995)
- Fix attribute value truncation in
`go.opentelemetry.io/otel/sdk/trace`. (#5997)
- Fix attribute value truncation in `go.opentelemetry.io/otel/sdk/log`.
(#6032)
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.

4 participants