This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
/
rollup.go
172 lines (138 loc) · 3.4 KB
/
rollup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package logging
import (
"sync"
"time"
"github.com/efritz/glock"
)
// FieldRollup is a field assigned to the last message in a
// window. Its value is equal to the number of messages in the
// window before it was flushed.
const FieldRollup = "rollup-multiplicity"
type (
rollupShim struct {
logger Logger
clock glock.Clock
windowDuration time.Duration
windows map[string]*logWindow
mutex sync.RWMutex
}
logWindow struct {
stashed *logMessage
start time.Time
count int
mutex sync.RWMutex
}
)
//
// Shim
var _ logShim = &rollupShim{}
// NewRollupAdapter returns a logger with functionality to throttle similar messages.
// Messages begin a roll-up when a second messages with an identical format string is
// seen in the same window period. All remaining messages logged within that period
// are captured and emitted as a single message at the end of the window period. The
// fields and args are equal to the first rolled-up message.
func NewRollupAdapter(logger Logger, windowDuration time.Duration) Logger {
return adaptShim(newRollupShim(logger, glock.NewRealClock(), windowDuration))
}
func newRollupShim(logger Logger, clock glock.Clock, windowDuration time.Duration) *rollupShim {
return &rollupShim{
logger: logger,
clock: clock,
windowDuration: windowDuration,
windows: map[string]*logWindow{},
}
}
func (s *rollupShim) WithFields(fields Fields) logShim {
if len(fields) == 0 {
return s
}
return newRollupShim(
s.logger.WithFields(fields),
s.clock,
s.windowDuration,
)
}
func (s *rollupShim) LogWithFields(level LogLevel, fields Fields, format string, args ...interface{}) {
fields = addCaller(fields)
if s.getWindow(format).record(s.logger, s.clock, s.windowDuration, level, fields, format, args...) {
// Not rolling up, log immediately
s.logger.LogWithFields(level, fields, format, args...)
}
}
func (s *rollupShim) getWindow(format string) *logWindow {
s.mutex.RLock()
if window, ok := s.windows[format]; ok {
s.mutex.RUnlock()
return window
}
s.mutex.RUnlock()
s.mutex.Lock()
defer s.mutex.Unlock()
if window, ok := s.windows[format]; ok {
return window
}
window := &logWindow{}
s.windows[format] = window
return window
}
func (s *rollupShim) Sync() error {
for _, window := range s.windows {
window.flush(s.logger)
}
return s.logger.Sync()
}
//
// Log Window
func (w *logWindow) record(
logger Logger,
clock glock.Clock,
windowDuration time.Duration,
level LogLevel,
fields Fields,
format string,
args ...interface{},
) bool {
w.mutex.Lock()
defer w.mutex.Unlock()
now := clock.Now()
if remaining := windowDuration - now.Sub(w.start); w.start != (time.Time{}) && remaining > 0 {
w.count++
if w.count == 1 {
ch := clock.After(remaining)
go func() {
<-ch
w.flush(logger)
}()
}
return false
}
w.flushLocked(logger)
w.count = 0
w.start = now
w.stashed = &logMessage{
level: level,
fields: fields,
format: format,
args: args,
}
return true
}
func (w *logWindow) flush(logger Logger) {
w.mutex.Lock()
w.flushLocked(logger)
w.mutex.Unlock()
}
func (w *logWindow) flushLocked(logger Logger) {
if w.stashed == nil || w.count <= 1 {
return
}
// Set replay field on message
w.stashed.fields[FieldRollup] = w.count
logger.LogWithFields(
w.stashed.level,
w.stashed.fields,
w.stashed.format,
w.stashed.args...,
)
w.stashed = nil
}