|
|
@@ -333,7 +333,8 @@ func New(family, title string) Trace {
|
|
|
tr.ref()
|
|
|
tr.Family, tr.Title = family, title
|
|
|
tr.Start = time.Now()
|
|
|
- tr.events = make([]event, 0, maxEventsPerTrace)
|
|
|
+ tr.maxEvents = maxEventsPerTrace
|
|
|
+ tr.events = tr.eventsBuf[:0]
|
|
|
|
|
|
activeMu.RLock()
|
|
|
s := activeTraces[tr.Family]
|
|
|
@@ -650,8 +651,8 @@ type event struct {
|
|
|
Elapsed time.Duration // since previous event in trace
|
|
|
NewDay bool // whether this event is on a different day to the previous event
|
|
|
Recyclable bool // whether this event was passed via LazyLog
|
|
|
- What interface{} // string or fmt.Stringer
|
|
|
Sensitive bool // whether this event contains sensitive information
|
|
|
+ What interface{} // string or fmt.Stringer
|
|
|
}
|
|
|
|
|
|
// WhenString returns a string representation of the elapsed time of the event.
|
|
|
@@ -692,14 +693,17 @@ type trace struct {
|
|
|
IsError bool
|
|
|
|
|
|
// Append-only sequence of events (modulo discards).
|
|
|
- mu sync.RWMutex
|
|
|
- events []event
|
|
|
+ mu sync.RWMutex
|
|
|
+ events []event
|
|
|
+ maxEvents int
|
|
|
|
|
|
refs int32 // how many buckets this is in
|
|
|
recycler func(interface{})
|
|
|
disc discarded // scratch space to avoid allocation
|
|
|
|
|
|
finishStack []byte // where finish was called, if DebugUseAfterFinish is set
|
|
|
+
|
|
|
+ eventsBuf [4]event // preallocated buffer in case we only log a few events
|
|
|
}
|
|
|
|
|
|
func (tr *trace) reset() {
|
|
|
@@ -711,11 +715,15 @@ func (tr *trace) reset() {
|
|
|
tr.traceID = 0
|
|
|
tr.spanID = 0
|
|
|
tr.IsError = false
|
|
|
+ tr.maxEvents = 0
|
|
|
tr.events = nil
|
|
|
tr.refs = 0
|
|
|
tr.recycler = nil
|
|
|
tr.disc = 0
|
|
|
tr.finishStack = nil
|
|
|
+ for i := range tr.eventsBuf {
|
|
|
+ tr.eventsBuf[i] = event{}
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
e := event{When: time.Now(), What: x, Recyclable: recyclable, Sensitive: sensitive}
|
|
|
tr.mu.Lock()
|
|
|
e.Elapsed, e.NewDay = tr.delta(e.When)
|
|
|
- if len(tr.events) < cap(tr.events) {
|
|
|
+ if len(tr.events) < tr.maxEvents {
|
|
|
tr.events = append(tr.events, e)
|
|
|
} else {
|
|
|
// Discard the middle events.
|
|
|
- di := int((cap(tr.events) - 1) / 2)
|
|
|
+ di := int((tr.maxEvents - 1) / 2)
|
|
|
if d, ok := tr.events[di].What.(*discarded); ok {
|
|
|
(*d)++
|
|
|
} else {
|
|
|
@@ -777,7 +785,7 @@ func (tr *trace) addEvent(x interface{}, recyclable, sensitive bool) {
|
|
|
go tr.recycler(tr.events[di+1].What)
|
|
|
}
|
|
|
copy(tr.events[di+1:], tr.events[di+2:])
|
|
|
- tr.events[cap(tr.events)-1] = e
|
|
|
+ tr.events[tr.maxEvents-1] = e
|
|
|
}
|
|
|
tr.mu.Unlock()
|
|
|
}
|
|
|
@@ -803,7 +811,7 @@ func (tr *trace) SetTraceInfo(traceID, spanID uint64) {
|
|
|
func (tr *trace) SetMaxEvents(m int) {
|
|
|
// Always keep at least three events: first, discarded count, last.
|
|
|
if len(tr.events) == 0 && m > 3 {
|
|
|
- tr.events = make([]event, 0, m)
|
|
|
+ tr.maxEvents = m
|
|
|
}
|
|
|
}
|
|
|
|