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

Commit ff2df8e

Browse files
authored
Lock only when needed, remove duplicate code (#1242)
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
1 parent 380f407 commit ff2df8e

1 file changed

Lines changed: 8 additions & 30 deletions

File tree

trace/trace.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -419,42 +419,20 @@ func (s *span) AddAttributes(attributes ...Attribute) {
419419
s.mu.Unlock()
420420
}
421421

422-
// copyAttributes copies a slice of Attributes into a map.
423-
func copyAttributes(m map[string]interface{}, attributes []Attribute) {
424-
for _, a := range attributes {
425-
m[a.key] = a.value
426-
}
427-
}
428-
429-
func (s *span) lazyPrintfInternal(attributes []Attribute, format string, a ...interface{}) {
430-
now := time.Now()
431-
msg := fmt.Sprintf(format, a...)
432-
var m map[string]interface{}
433-
s.mu.Lock()
434-
if len(attributes) != 0 {
435-
m = make(map[string]interface{}, len(attributes))
436-
copyAttributes(m, attributes)
437-
}
438-
s.annotations.add(Annotation{
439-
Time: now,
440-
Message: msg,
441-
Attributes: m,
442-
})
443-
s.mu.Unlock()
444-
}
445-
446422
func (s *span) printStringInternal(attributes []Attribute, str string) {
447423
now := time.Now()
448-
var a map[string]interface{}
449-
s.mu.Lock()
424+
var am map[string]interface{}
450425
if len(attributes) != 0 {
451-
a = make(map[string]interface{}, len(attributes))
452-
copyAttributes(a, attributes)
426+
am = make(map[string]interface{}, len(attributes))
427+
for _, attr := range attributes {
428+
am[attr.key] = attr.value
429+
}
453430
}
431+
s.mu.Lock()
454432
s.annotations.add(Annotation{
455433
Time: now,
456434
Message: str,
457-
Attributes: a,
435+
Attributes: am,
458436
})
459437
s.mu.Unlock()
460438
}
@@ -473,7 +451,7 @@ func (s *span) Annotatef(attributes []Attribute, format string, a ...interface{}
473451
if !s.IsRecordingEvents() {
474452
return
475453
}
476-
s.lazyPrintfInternal(attributes, format, a...)
454+
s.printStringInternal(attributes, fmt.Sprintf(format, a...))
477455
}
478456

479457
// AddMessageSendEvent adds a message send event to the span.

0 commit comments

Comments
 (0)