@@ -179,7 +179,8 @@ func (s *recordingSpan) IsRecording() bool {
179
179
180
180
// isRecording returns if this span is being recorded. If this span has ended
181
181
// this will return false.
182
- // This is done without acquiring a lock.
182
+ //
183
+ // This method assumes s.mu.Lock is held by the caller.
183
184
func (s * recordingSpan ) isRecording () bool {
184
185
if s == nil {
185
186
return false
@@ -408,9 +409,10 @@ func (s *recordingSpan) End(options ...trace.SpanEndOption) {
408
409
// the span's duration in case some operation below takes a while.
409
410
et := monotonicEndTime (s .startTime )
410
411
411
- // Do relative expensive check now that we have an end time and see if we
412
- // need to do any more processing.
413
- if ! s .IsRecording () {
412
+ // Lock the span now that we have an end time and see if we need to do any more processing.
413
+ s .mu .Lock ()
414
+ if ! s .isRecording () {
415
+ s .mu .Unlock ()
414
416
return
415
417
}
416
418
@@ -435,10 +437,11 @@ func (s *recordingSpan) End(options ...trace.SpanEndOption) {
435
437
}
436
438
437
439
if s .executionTracerTaskEnd != nil {
440
+ s .mu .Unlock ()
438
441
s .executionTracerTaskEnd ()
442
+ s .mu .Lock ()
439
443
}
440
444
441
- s .mu .Lock ()
442
445
// Setting endTime to non-zero marks the span as ended and not recording.
443
446
if config .Timestamp ().IsZero () {
444
447
s .endTime = et
@@ -472,7 +475,13 @@ func monotonicEndTime(start time.Time) time.Time {
472
475
// does not change the Span status. If this span is not being recorded or err is nil
473
476
// than this method does nothing.
474
477
func (s * recordingSpan ) RecordError (err error , opts ... trace.EventOption ) {
475
- if s == nil || err == nil || ! s .IsRecording () {
478
+ if s == nil || err == nil {
479
+ return
480
+ }
481
+
482
+ s .mu .Lock ()
483
+ defer s .mu .Unlock ()
484
+ if ! s .isRecording () {
476
485
return
477
486
}
478
487
@@ -508,14 +517,23 @@ func recordStackTrace() string {
508
517
}
509
518
510
519
// AddEvent adds an event with the provided name and options. If this span is
511
- // not being recorded than this method does nothing.
520
+ // not being recorded then this method does nothing.
512
521
func (s * recordingSpan ) AddEvent (name string , o ... trace.EventOption ) {
513
- if ! s .IsRecording () {
522
+ if s == nil {
523
+ return
524
+ }
525
+
526
+ s .mu .Lock ()
527
+ defer s .mu .Unlock ()
528
+ if ! s .isRecording () {
514
529
return
515
530
}
516
531
s .addEvent (name , o ... )
517
532
}
518
533
534
+ // addEvent adds an event with the provided name and options.
535
+ //
536
+ // This method assumes s.mu.Lock is held by the caller.
519
537
func (s * recordingSpan ) addEvent (name string , o ... trace.EventOption ) {
520
538
c := trace .NewEventConfig (o ... )
521
539
e := Event {Name : name , Attributes : c .Attributes (), Time : c .Timestamp ()}
@@ -532,9 +550,7 @@ func (s *recordingSpan) addEvent(name string, o ...trace.EventOption) {
532
550
e .Attributes = e .Attributes [:limit ]
533
551
}
534
552
535
- s .mu .Lock ()
536
553
s .events .add (e )
537
- s .mu .Unlock ()
538
554
}
539
555
540
556
// SetName sets the name of this span. If this span is not being recorded than
@@ -682,14 +698,20 @@ func (s *recordingSpan) Resource() *resource.Resource {
682
698
}
683
699
684
700
func (s * recordingSpan ) AddLink (link trace.Link ) {
685
- if ! s . IsRecording () {
701
+ if s == nil {
686
702
return
687
703
}
688
704
if ! link .SpanContext .IsValid () && len (link .Attributes ) == 0 &&
689
705
link .SpanContext .TraceState ().Len () == 0 {
690
706
return
691
707
}
692
708
709
+ s .mu .Lock ()
710
+ defer s .mu .Unlock ()
711
+ if ! s .isRecording () {
712
+ return
713
+ }
714
+
693
715
l := Link {SpanContext : link .SpanContext , Attributes : link .Attributes }
694
716
695
717
// Discard attributes over limit.
@@ -703,9 +725,7 @@ func (s *recordingSpan) AddLink(link trace.Link) {
703
725
l .Attributes = l .Attributes [:limit ]
704
726
}
705
727
706
- s .mu .Lock ()
707
728
s .links .add (l )
708
- s .mu .Unlock ()
709
729
}
710
730
711
731
// DroppedAttributes returns the number of attributes dropped by the span
0 commit comments