-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add a SpanRecorder to the sdk/trace/tracetest #2132
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
906eec7
Add a SpanRecorder to the sdk/trace/tracetest
MrAlias 8ab8a65
Merge branch 'main' into sr-tracetest
MrAlias 612d6e6
Add changes to changelog
MrAlias 4a8dbc5
Run make precommit
MrAlias 8755d8d
Rename tests
MrAlias 8d1e977
Merge branch 'main' into sr-tracetest
MrAlias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| ) | ||
|
|
||
| // SpanRecorder records started and ended spans. | ||
| type SpanRecorder struct { | ||
| started []sdktrace.ReadWriteSpan | ||
| ended []sdktrace.ReadOnlySpan | ||
| } | ||
|
|
||
| var _ sdktrace.SpanProcessor = (*SpanRecorder)(nil) | ||
|
|
||
| func NewSpanRecorder() *SpanRecorder { | ||
| return new(SpanRecorder) | ||
| } | ||
|
|
||
| // OnStart records started spans. | ||
| func (sr *SpanRecorder) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) { | ||
| sr.started = append(sr.started, s) | ||
| } | ||
|
|
||
| // OnEnd records completed spans. | ||
| func (sr *SpanRecorder) OnEnd(s sdktrace.ReadOnlySpan) { | ||
| sr.ended = append(sr.ended, s) | ||
| } | ||
|
|
||
| // Shutdown does nothing. | ||
| func (sr *SpanRecorder) Shutdown(context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| // ForceFlush does nothing. | ||
| func (sr *SpanRecorder) ForceFlush(context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| // Started returns a copy of all started spans that have been recorded. | ||
| func (sr *SpanRecorder) Started() []sdktrace.ReadWriteSpan { | ||
| dst := make([]sdktrace.ReadWriteSpan, len(sr.started)) | ||
| copy(dst, sr.started) | ||
| return dst | ||
| } | ||
|
|
||
| // Ended returns a copy of all ended spans that have been recorded. | ||
| func (sr *SpanRecorder) Ended() []sdktrace.ReadOnlySpan { | ||
| dst := make([]sdktrace.ReadOnlySpan, len(sr.ended)) | ||
| copy(dst, sr.ended) | ||
| return dst | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package tracetest | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| ) | ||
|
|
||
| type rwSpan struct { | ||
| sdktrace.ReadWriteSpan | ||
| } | ||
|
|
||
| func TestSpanRecorderOnStartAppends(t *testing.T) { | ||
| s0, s1 := new(rwSpan), new(rwSpan) | ||
| ctx := context.Background() | ||
| sr := new(SpanRecorder) | ||
|
|
||
| assert.Len(t, sr.started, 0) | ||
| sr.OnStart(ctx, s0) | ||
| assert.Len(t, sr.started, 1) | ||
| sr.OnStart(ctx, s1) | ||
| assert.Len(t, sr.started, 2) | ||
|
|
||
| // Ensure order correct. | ||
| started := sr.Started() | ||
| assert.Same(t, s0, started[0]) | ||
| assert.Same(t, s1, started[1]) | ||
| } | ||
|
|
||
| type roSpan struct { | ||
| sdktrace.ReadOnlySpan | ||
| } | ||
|
|
||
| func TestSpanRecorderOnEndAppends(t *testing.T) { | ||
| s0, s1 := new(roSpan), new(roSpan) | ||
| sr := new(SpanRecorder) | ||
|
|
||
| assert.Len(t, sr.ended, 0) | ||
| sr.OnEnd(s0) | ||
| assert.Len(t, sr.ended, 1) | ||
| sr.OnEnd(s1) | ||
| assert.Len(t, sr.ended, 2) | ||
|
|
||
| // Ensure order correct. | ||
| ended := sr.Ended() | ||
| assert.Same(t, s0, ended[0]) | ||
| assert.Same(t, s1, ended[1]) | ||
| } | ||
|
|
||
| func TestSpanRecorderShutdownNoError(t *testing.T) { | ||
| ctx := context.Background() | ||
| assert.NoError(t, new(SpanRecorder).Shutdown(ctx)) | ||
|
|
||
| var c context.CancelFunc | ||
| ctx, c = context.WithCancel(ctx) | ||
| c() | ||
| assert.NoError(t, new(SpanRecorder).Shutdown(ctx)) | ||
| } | ||
|
|
||
| func TestSpanRecorderForceFlushNoError(t *testing.T) { | ||
| ctx := context.Background() | ||
| assert.NoError(t, new(SpanRecorder).ForceFlush(ctx)) | ||
|
|
||
| var c context.CancelFunc | ||
| ctx, c = context.WithCancel(ctx) | ||
| c() | ||
| assert.NoError(t, new(SpanRecorder).ForceFlush(ctx)) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.