record is the shared implementation. The sink (if any) is called outside the lock to avoid re-entrancy from sinks that take their own lock. Returns the new event's ID, or 0 if recording is disabled.
(correlatedID uint64, source string, level Level, message, payload string)
| 115 | // the lock to avoid re-entrancy from sinks that take their own lock. |
| 116 | // Returns the new event's ID, or 0 if recording is disabled. |
| 117 | func record(correlatedID uint64, source string, level Level, message, payload string) uint64 { |
| 118 | mu.Lock() |
| 119 | if !enabled { |
| 120 | mu.Unlock() |
| 121 | return 0 |
| 122 | } |
| 123 | if len(events) >= ringCapacity { |
| 124 | // Drop the oldest. A real ring would use a head index; for ~500 |
| 125 | // events the slice shift is fine and keeps Snapshot trivial. |
| 126 | events = events[1:] |
| 127 | } |
| 128 | e := Event{ |
| 129 | ID: nextID.Add(1), |
| 130 | CorrelatedID: correlatedID, |
| 131 | Time: time.Now(), |
| 132 | Source: source, |
| 133 | Level: level, |
| 134 | Message: message, |
| 135 | Payload: payload, |
| 136 | } |
| 137 | events = append(events, e) |
| 138 | cb := sink |
| 139 | mu.Unlock() |
| 140 | if cb != nil { |
| 141 | cb(e) |
| 142 | } |
| 143 | return e.ID |
| 144 | } |
| 145 | |
| 146 | // Pair returns the event linked to id, in either direction. If the given |
| 147 | // event has a CorrelatedID, that's the request it's paired with. Otherwise |