Skip to content

Commit

Permalink
chore: avoid doing multiple locks on SetAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi-impossiblecloud committed Oct 4, 2024
1 parent 48156c1 commit f6fd877
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ func (s *recordingSpan) IsRecording() bool {
s.mu.Lock()
defer s.mu.Unlock()

return s.isRecording()
}

// isRecording returns if this span is being recorded. If this span has ended
// this will return false.
// This is done without acquiring a lock.
func (s *recordingSpan) isRecording() bool {
if s == nil {
return false
}
return s.endTime.IsZero()
}

Expand All @@ -182,11 +192,15 @@ func (s *recordingSpan) IsRecording() bool {
// included in the set status when the code is for an error. If this span is
// not being recorded than this method does nothing.
func (s *recordingSpan) SetStatus(code codes.Code, description string) {
if !s.IsRecording() {
if s == nil {
return
}

s.mu.Lock()
defer s.mu.Unlock()
if !s.isRecording() {
return
}
if s.status.Code > code {
return
}
Expand All @@ -210,12 +224,15 @@ func (s *recordingSpan) SetStatus(code codes.Code, description string) {
// attributes the span is configured to have, the last added attributes will
// be dropped.
func (s *recordingSpan) SetAttributes(attributes ...attribute.KeyValue) {
if !s.IsRecording() || len(attributes) == 0 {
if s == nil {
return
}

s.mu.Lock()
defer s.mu.Unlock()
if !s.isRecording() || len(attributes) == 0 {
return
}

limit := s.tracer.provider.spanLimits.AttributeCountLimit
if limit == 0 {
Expand Down Expand Up @@ -523,12 +540,15 @@ func (s *recordingSpan) addEvent(name string, o ...trace.EventOption) {
// SetName sets the name of this span. If this span is not being recorded than
// this method does nothing.
func (s *recordingSpan) SetName(name string) {
if !s.IsRecording() {
if s == nil {
return
}

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

Expand Down Expand Up @@ -760,12 +780,16 @@ func (s *recordingSpan) snapshot() ReadOnlySpan {
}

func (s *recordingSpan) addChild() {
if !s.IsRecording() {
if s == nil {
return
}

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

func (*recordingSpan) private() {}
Expand Down

0 comments on commit f6fd877

Please sign in to comment.