Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Lock only when needed, remove duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Nov 23, 2020
1 parent 380f407 commit 8859502
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,42 +419,20 @@ func (s *span) AddAttributes(attributes ...Attribute) {
s.mu.Unlock()
}

// copyAttributes copies a slice of Attributes into a map.
func copyAttributes(m map[string]interface{}, attributes []Attribute) {
for _, a := range attributes {
m[a.key] = a.value
}
}

func (s *span) lazyPrintfInternal(attributes []Attribute, format string, a ...interface{}) {
now := time.Now()
msg := fmt.Sprintf(format, a...)
var m map[string]interface{}
s.mu.Lock()
if len(attributes) != 0 {
m = make(map[string]interface{}, len(attributes))
copyAttributes(m, attributes)
}
s.annotations.add(Annotation{
Time: now,
Message: msg,
Attributes: m,
})
s.mu.Unlock()
}

func (s *span) printStringInternal(attributes []Attribute, str string) {
now := time.Now()
var a map[string]interface{}
s.mu.Lock()
var am map[string]interface{}
if len(attributes) != 0 {
a = make(map[string]interface{}, len(attributes))
copyAttributes(a, attributes)
am = make(map[string]interface{}, len(attributes))
for _, attr := range attributes {
am[attr.key] = attr.value
}
}
s.mu.Lock()
s.annotations.add(Annotation{
Time: now,
Message: str,
Attributes: a,
Attributes: am,
})
s.mu.Unlock()
}
Expand All @@ -473,7 +451,7 @@ func (s *span) Annotatef(attributes []Attribute, format string, a ...interface{}
if !s.IsRecordingEvents() {
return
}
s.lazyPrintfInternal(attributes, format, a...)
s.printStringInternal(attributes, fmt.Sprintf(format, a...))
}

// AddMessageSendEvent adds a message send event to the span.
Expand Down

0 comments on commit 8859502

Please sign in to comment.