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

[trace] Fix race between CreateAsyncChild and span.Send #39

Merged
merged 1 commit into from
Nov 28, 2018
Merged
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
29 changes: 16 additions & 13 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,13 @@ func (s *Span) GetParent() *Span {
// outlive the current span (and trace). Async spans are not automatically sent
// when their parent finishes, but are otherwise identical to synchronous spans.
func (s *Span) CreateAsyncChild(ctx context.Context) (context.Context, *Span) {
ctx, newSpan := s.CreateChild(ctx)
newSpan.isAsync = true
return ctx, newSpan
return s.createChildSpan(ctx, true)
}

// Span creates a synchronous child of the current span. Spans must finish
// before their parents.
func (s *Span) CreateChild(ctx context.Context) (context.Context, *Span) {
newSpan := newSpan()
newSpan.parent = s
newSpan.parentID = s.spanID
newSpan.trace = s.trace
newSpan.ev = s.trace.builder.NewEvent()
s.childrenLock.Lock()
s.children = append(s.children, newSpan)
s.childrenLock.Unlock()
ctx = PutSpanInContext(ctx, newSpan)
return ctx, newSpan
return s.createChildSpan(ctx, false)
}

// SerializeHeaders returns the trace ID, current span ID as parent ID, and an
Expand Down Expand Up @@ -370,3 +359,17 @@ func (s *Span) send() {
s.ev.SendPresampled()
}
}

func (s *Span) createChildSpan(ctx context.Context, async bool) (context.Context, *Span) {
newSpan := newSpan()
newSpan.parent = s
newSpan.parentID = s.spanID
newSpan.trace = s.trace
newSpan.ev = s.trace.builder.NewEvent()
newSpan.isAsync = async
s.childrenLock.Lock()
s.children = append(s.children, newSpan)
s.childrenLock.Unlock()
ctx = PutSpanInContext(ctx, newSpan)
return ctx, newSpan
}