-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmultiline.go
178 lines (155 loc) · 3.4 KB
/
multiline.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
173
174
175
176
177
178
package logparser
import (
"context"
"strings"
"sync"
"time"
"unicode/utf8"
)
var (
multilineCollectorLimit = 64 * 1024
)
type Message struct {
Timestamp time.Time
Content string
Level Level
}
type MultilineCollector struct {
Messages chan Message
timeout time.Duration
limit int
ts time.Time
level Level
lines []string
size int
lock sync.Mutex
closed bool
lastReceiveTime time.Time
isFirstLineContainsTimestamp bool
pythonTraceback bool
pythonTracebackExpected bool
}
func NewMultilineCollector(ctx context.Context, timeout time.Duration, limit int) *MultilineCollector {
m := &MultilineCollector{
timeout: timeout,
limit: limit,
Messages: make(chan Message, 1),
}
go m.dispatch(ctx)
return m
}
func (m *MultilineCollector) dispatch(ctx context.Context) {
ticker := time.NewTicker(m.timeout)
defer ticker.Stop()
defer close(m.Messages)
for {
select {
case <-ctx.Done():
m.closed = true
return
case t := <-ticker.C:
m.lock.Lock()
if t.Sub(m.lastReceiveTime) > m.timeout {
m.flushMessage()
}
m.lock.Unlock()
}
}
}
func (m *MultilineCollector) Add(entry LogEntry) {
if !utf8.ValidString(entry.Content) {
return
}
m.lock.Lock()
defer m.lock.Unlock()
entry.Content = strings.TrimSuffix(entry.Content, "\n")
if entry.Content == "" {
if len(m.lines) > 0 {
m.add(entry)
}
return
}
if m.isNextMessage(entry.Content) {
pythonTraceback := m.pythonTraceback
m.flushMessage()
m.pythonTraceback = pythonTraceback
}
m.add(entry)
}
func (m *MultilineCollector) add(entry LogEntry) {
remaining := m.limit - m.size
if remaining <= 0 {
return
}
if len(m.lines) == 0 {
m.ts = entry.Timestamp
m.level = GuessLevel(entry.Content)
if m.level == LevelUnknown && entry.Level != LevelUnknown {
m.level = entry.Level
}
m.isFirstLineContainsTimestamp = containsTimestamp(entry.Content)
}
content := entry.Content
if len(content) > remaining {
content = content[:remaining]
}
m.lines = append(m.lines, content)
m.size += len(content) + 1
m.lastReceiveTime = time.Now()
}
func (m *MultilineCollector) isNextMessage(l string) bool {
if l == "" || l == "}" || strings.HasPrefix(l, "\t") || strings.HasPrefix(l, " ") {
return false
}
if m.isFirstLineContainsTimestamp {
return containsTimestamp(l)
}
if strings.HasPrefix(l, "Caused by: ") {
return false
}
if strings.HasPrefix(l, "for call at") {
return false
}
if strings.HasPrefix(l, "Traceback ") {
m.pythonTraceback = true
if m.pythonTracebackExpected {
m.pythonTracebackExpected = false
return false
}
return len(m.lines) > 0
}
if l == "The above exception was the direct cause of the following exception:" || l == "During handling of the above exception, another exception occurred:" {
m.pythonTracebackExpected = true
return false
}
if m.pythonTraceback {
m.pythonTraceback = false
return false
}
return true
}
func (m *MultilineCollector) flushMessage() {
if m.closed {
return
}
if len(m.lines) == 0 {
return
}
content := strings.TrimSpace(strings.Join(m.lines, "\n"))
msg := Message{
Timestamp: m.ts,
Content: content,
Level: m.level,
}
m.reset()
m.Messages <- msg
}
func (m *MultilineCollector) reset() {
m.ts = time.Time{}
m.level = LevelUnknown
m.lines = m.lines[:0]
m.size = 0
m.isFirstLineContainsTimestamp = false
m.pythonTraceback = false
m.pythonTracebackExpected = false
}