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

Lock only when needed, remove duplicate code #1242

Merged
merged 1 commit into from
Nov 25, 2020
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
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