@@ -333,7 +333,8 @@ func New(family, title string) Trace {
333
333
tr .ref ()
334
334
tr .Family , tr .Title = family , title
335
335
tr .Start = time .Now ()
336
- tr .events = make ([]event , 0 , maxEventsPerTrace )
336
+ tr .maxEvents = maxEventsPerTrace
337
+ tr .events = tr .eventsBuf [:0 ]
337
338
338
339
activeMu .RLock ()
339
340
s := activeTraces [tr .Family ]
@@ -650,8 +651,8 @@ type event struct {
650
651
Elapsed time.Duration // since previous event in trace
651
652
NewDay bool // whether this event is on a different day to the previous event
652
653
Recyclable bool // whether this event was passed via LazyLog
653
- What interface {} // string or fmt.Stringer
654
654
Sensitive bool // whether this event contains sensitive information
655
+ What interface {} // string or fmt.Stringer
655
656
}
656
657
657
658
// WhenString returns a string representation of the elapsed time of the event.
@@ -692,14 +693,17 @@ type trace struct {
692
693
IsError bool
693
694
694
695
// Append-only sequence of events (modulo discards).
695
- mu sync.RWMutex
696
- events []event
696
+ mu sync.RWMutex
697
+ events []event
698
+ maxEvents int
697
699
698
700
refs int32 // how many buckets this is in
699
701
recycler func (interface {})
700
702
disc discarded // scratch space to avoid allocation
701
703
702
704
finishStack []byte // where finish was called, if DebugUseAfterFinish is set
705
+
706
+ eventsBuf [4 ]event // preallocated buffer in case we only log a few events
703
707
}
704
708
705
709
func (tr * trace ) reset () {
@@ -711,11 +715,15 @@ func (tr *trace) reset() {
711
715
tr .traceID = 0
712
716
tr .spanID = 0
713
717
tr .IsError = false
718
+ tr .maxEvents = 0
714
719
tr .events = nil
715
720
tr .refs = 0
716
721
tr .recycler = nil
717
722
tr .disc = 0
718
723
tr .finishStack = nil
724
+ for i := range tr .eventsBuf {
725
+ tr .eventsBuf [i ] = event {}
726
+ }
719
727
}
720
728
721
729
// delta returns the elapsed time since the last event or the trace start,
@@ -753,11 +761,11 @@ func (tr *trace) addEvent(x interface{}, recyclable, sensitive bool) {
753
761
e := event {When : time .Now (), What : x , Recyclable : recyclable , Sensitive : sensitive }
754
762
tr .mu .Lock ()
755
763
e .Elapsed , e .NewDay = tr .delta (e .When )
756
- if len (tr .events ) < cap ( tr .events ) {
764
+ if len (tr .events ) < tr .maxEvents {
757
765
tr .events = append (tr .events , e )
758
766
} else {
759
767
// Discard the middle events.
760
- di := int ((cap ( tr .events ) - 1 ) / 2 )
768
+ di := int ((tr .maxEvents - 1 ) / 2 )
761
769
if d , ok := tr .events [di ].What .(* discarded ); ok {
762
770
(* d )++
763
771
} else {
@@ -777,7 +785,7 @@ func (tr *trace) addEvent(x interface{}, recyclable, sensitive bool) {
777
785
go tr .recycler (tr .events [di + 1 ].What )
778
786
}
779
787
copy (tr .events [di + 1 :], tr .events [di + 2 :])
780
- tr .events [cap ( tr .events ) - 1 ] = e
788
+ tr .events [tr .maxEvents - 1 ] = e
781
789
}
782
790
tr .mu .Unlock ()
783
791
}
@@ -803,7 +811,7 @@ func (tr *trace) SetTraceInfo(traceID, spanID uint64) {
803
811
func (tr * trace ) SetMaxEvents (m int ) {
804
812
// Always keep at least three events: first, discarded count, last.
805
813
if len (tr .events ) == 0 && m > 3 {
806
- tr .events = make ([] event , 0 , m )
814
+ tr .maxEvents = m
807
815
}
808
816
}
809
817
0 commit comments