Skip to content

Commit

Permalink
chore: avoid doing multiple locks while adding links
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi-impossiblecloud committed Oct 7, 2024
1 parent 2cf7e8d commit 4d740bd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,14 +698,20 @@ func (s *recordingSpan) Resource() *resource.Resource {
}

func (s *recordingSpan) AddLink(link trace.Link) {
if !s.IsRecording() {
if s == nil {
return
}
if !link.SpanContext.IsValid() && len(link.Attributes) == 0 &&
link.SpanContext.TraceState().Len() == 0 {
return
}

s.mu.Lock()
defer s.mu.Unlock()
if !s.isRecording() {
return
}

l := Link{SpanContext: link.SpanContext, Attributes: link.Attributes}

// Discard attributes over limit.
Expand All @@ -719,9 +725,7 @@ func (s *recordingSpan) AddLink(link trace.Link) {
l.Attributes = l.Attributes[:limit]
}

s.mu.Lock()
s.links.add(l)
s.mu.Unlock()
}

// DroppedAttributes returns the number of attributes dropped by the span
Expand Down

0 comments on commit 4d740bd

Please sign in to comment.