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

tracing: fix corruption of structured recording buffer #75099

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/util/tracing/crdbspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ func (s *crdbSpan) getStructuredRecording(includeDetachedChildren bool) Recordin
return Recording{res}
}

// recordFinishedChildren adds `children` to the receiver's recording.
// recordFinishedChildren adds children to s' recording.
//
// s takes ownership of children; the caller is not allowed to use them anymore.
func (s *crdbSpan) recordFinishedChildren(children []tracingpb.RecordedSpan) {
if len(children) == 0 {
return
Expand All @@ -360,6 +362,7 @@ func (s *crdbSpan) recordFinishedChildren(children []tracingpb.RecordedSpan) {
s.recordFinishedChildrenLocked(children)
}

// s takes ownership of children; the caller is not allowed to use them anymore.
func (s *crdbSpan) recordFinishedChildrenLocked(children []tracingpb.RecordedSpan) {
if len(children) == 0 {
return
Expand All @@ -375,9 +378,10 @@ func (s *crdbSpan) recordFinishedChildrenLocked(children []tracingpb.RecordedSpa

s.mu.recording.finishedChildren = append(s.mu.recording.finishedChildren, children...)
} else {
for _, c := range children {
for _, e := range c.StructuredRecords {
s.recordInternalLocked(&e, &s.mu.recording.structured)
for ci := range children {
child := &children[ci]
for i := range child.StructuredRecords {
s.recordInternalLocked(&child.StructuredRecords[i], &s.mu.recording.structured)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/tracing/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ func TestImportRemoteSpans(t *testing.T) {
}
}

func TestImportRemoteSpansMaintainsRightByteSize(t *testing.T) {
tr1 := NewTracer()

child := tr1.StartSpan("child", WithRecording(RecordingStructured))
child.RecordStructured(&types.Int32Value{Value: 42})
child.RecordStructured(&types.StringValue{Value: "test"})

root := tr1.StartSpan("root", WithRecording(RecordingStructured))
root.ImportRemoteSpans(child.GetRecording(RecordingStructured))
c := root.i.crdb
c.mu.Lock()
buf := c.mu.recording.structured
sz := 0
for i := 0; i < buf.Len(); i++ {
sz += buf.Get(i).(memorySizable).MemorySize()
}
c.mu.Unlock()
require.NotZero(t, buf.size)
require.Equal(t, buf.size, int64(sz))
}

func TestSpanRecordStructured(t *testing.T) {
tr := NewTracer()
sp := tr.StartSpan("root", WithRecording(RecordingStructured))
Expand Down