Skip to content

Commit

Permalink
removed the lock completely in finishedOne()
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbayar committed Dec 16, 2021
1 parent 3573402 commit b333743
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
25 changes: 6 additions & 19 deletions ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ func (t *trace) finishedOne(s *span) {
t.root.setMetric(keySamplingPriority, *t.priority)
t.locked = true
}
if len(t.spans) > 0 && s == t.spans[0] {
// first span in chunk finished, lock down the tags
for k, v := range t.tags {
s.setMeta(k, v)
}
}
if len(t.spans) != t.finished {
return
}
Expand All @@ -309,24 +315,5 @@ func (t *trace) finishedOne(s *span) {
}
return
}
t.populateTraceTags(s)
tr.pushTrace(t.spans)
}

// populateTraceTags puts trace tags in Meta of the first span.
func (t *trace) populateTraceTags(s *span) {
if len(t.tags) == 0 {
return
}
if t.spans[0] != s {
t.spans[0].Lock()
defer t.spans[0].Unlock()
}
if t.spans[0].Meta == nil {
t.spans[0].Meta = t.tags
return
}
for k, v := range t.tags {
t.spans[0].Meta[k] = v
}
}
15 changes: 15 additions & 0 deletions ddtrace/tracer/spancontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ func TestAsyncSpanRace(t *testing.T) {
}
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-done:
root.Finish()
for i := 0; i < 500; i++ {
for range root.(*span).Meta {
// this range simulates iterating over the meta map
// as we do when encoding msgpack upon flushing.
}
}
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
select {
Expand Down

0 comments on commit b333743

Please sign in to comment.