Skip to content

Commit

Permalink
Fix span IsRecording when not sampling (#1750)
Browse files Browse the repository at this point in the history
* Adjust TestRecording fucntion to validate with and without sampling. correct span.isRecording logic to return false when not being sampled

* Changed to TestExectutionTracerTaskEnd to only expect 1 span to increment instead of all 3

* added changelog entry

* Updated CHANGELOG.md

* Remove newline from isRecording()

* Rewrite TestSpanIsRecording to include table based
tests and check for when span is ended immediately

* Update sdk/trace/trace_test.go

Improve readability of test name

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Update sdk/trace/trace_test.go

Improve readability of test name

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

* Update CHANGELOG.md

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Update sdk/trace_test.go test comments

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 30, 2021
1 parent 20c93b0 commit d575865
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- The `Span.IsRecording` implementation from `go.opentelemetry.io/otel/sdk/trace` always returns false when not being sampled. (#1750)

### Changed

- Jaeger exporter was updated to use thrift v0.14.1. (#1712)
Expand Down
3 changes: 2 additions & 1 deletion sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func (s *span) IsRecording() bool {
}
s.mu.Lock()
defer s.mu.Unlock()
return s.endTime.IsZero()

return !s.startTime.IsZero() && s.endTime.IsZero()
}

// SetStatus sets the status of this span in the form of a code and a
Expand Down
41 changes: 33 additions & 8 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,35 @@ func TestSetName(t *testing.T) {
}
}

func TestRecordingIsOn(t *testing.T) {
tp := NewTracerProvider()
_, span := tp.Tracer("Recording on").Start(context.Background(), "StartSpan")
defer span.End()
if span.IsRecording() == false {
t.Error("new span is not recording events")
}
func TestSpanIsRecording(t *testing.T) {
t.Run("while Span active", func(t *testing.T) {
for name, tc := range map[string]struct {
sampler Sampler
want bool
}{
"Always sample, recording on": {sampler: AlwaysSample(), want: true},
"Never sample recording off": {sampler: NeverSample(), want: false},
} {
tp := NewTracerProvider(WithSampler(tc.sampler))
_, span := tp.Tracer(name).Start(context.Background(), "StartSpan")
defer span.End()
got := span.IsRecording()
assert.Equal(t, got, tc.want, name)
}
})

t.Run("after Span end", func(t *testing.T) {
for name, tc := range map[string]Sampler{
"Always Sample": AlwaysSample(),
"Never Sample": NeverSample(),
} {
tp := NewTracerProvider(WithSampler(tc))
_, span := tp.Tracer(name).Start(context.Background(), "StartSpan")
span.End()
got := span.IsRecording()
assert.False(t, got, name)
}
})
}

func TestSampling(t *testing.T) {
Expand Down Expand Up @@ -1014,6 +1036,7 @@ func TestExecutionTracerTaskEnd(t *testing.T) {
s.executionTracerTaskEnd = executionTracerTaskEnd
spans = append(spans, s) // parent not sampled

tp.sampler = AlwaysSample()
_, apiSpan = tr.Start(context.Background(), "foo")
s = apiSpan.(*span)
s.executionTracerTaskEnd = executionTracerTaskEnd
Expand All @@ -1022,7 +1045,9 @@ func TestExecutionTracerTaskEnd(t *testing.T) {
for _, span := range spans {
span.End()
}
if got, want := n, uint64(len(spans)); got != want {
// Only one span should be sampled meaning only one execution of
// executionTracerTaskEnd.
if got, want := n, uint64(1); got != want {
t.Fatalf("Execution tracer task ended for %v spans; want %v", got, want)
}
}
Expand Down

0 comments on commit d575865

Please sign in to comment.