-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogParser.go
230 lines (173 loc) · 4.4 KB
/
LogParser.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package logparser
import (
"fmt"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
)
type LogLine struct {
TimeStamp time.Time
Text string
Filename string
}
func (l LogLine)String() string {
return fmt.Sprintf("(%v) : [%v] %v", l.Filename, l.TimeStamp, l.Text)
}
type LogParser interface {
TypeName() string
Parse(filename string) ([]LogLine, error)
}
type LogFileGroup struct {
FileGlob string
ParserName string
}
type indexedFile struct {
filename string
parser string
timeRange TimeRange
}
type LogProcessor struct {
parsers map[string]LogParser
indexedFiles []indexedFile
}
func (l *LogProcessor)Index(groups []LogFileGroup) error {
for _, group := range groups {
matches, err := filepath.Glob(group.FileGlob)
if err != nil {
return fmt.Errorf("whilie attempting to glob for '%v': %v", group.FileGlob, err)
}
parser, ok := l.parsers[group.ParserName]
if !ok {
return fmt.Errorf("cannot find parser by name '%v'", group.ParserName)
}
for _, filename := range matches {
logrus.WithFields(logrus.Fields{
"filename": filename,
"parser": parser.TypeName(),
}).Infof("Processing file")
logLines, err := parser.Parse(filename)
if err != nil {
logrus.WithFields(logrus.Fields{
"filename": filename,
"parser": parser.TypeName(),
"error": err,
}).Error("Error while parsing file")
continue
}
if len(logLines) == 0 {
logrus.WithFields(logrus.Fields{
"filename": filename,
"parser": parser.TypeName(),
}).Warn("Zero log lines returned")
continue
}
startTime := logLines[0].TimeStamp
endTime := logLines[len(logLines) - 1].TimeStamp
parsedFile := indexedFile{
filename: filename,
parser: parser.TypeName(),
timeRange: TimeRange{startTime, endTime },
}
l.indexedFiles = append(l.indexedFiles, parsedFile)
logrus.WithFields(logrus.Fields{
"filename": filename,
"fileCount": len(l.indexedFiles),
}).Infof("Added file")
//
}
}
return nil
}
func (l *LogProcessor)FindLines(t TimeRange) ([]LogLine, error) {
allLines := make([]LogLine, 0)
for _, f := range l.indexedFiles {
logrus.WithFields(logrus.Fields{
"filename": f.filename,
"timeRange": f.timeRange,
"parser": f.parser,
}).Info("Looking at file")
if f.timeRange.Intersects(t) {
logrus.WithFields(logrus.Fields{
"filename": f.filename,
"timeRange": f.timeRange,
"parser": f.parser,
}).Info("File matches time range")
parser, ok := l.parsers[f.parser]
if !ok {
return nil, fmt.Errorf("cannot find parser by name '%v'", f.parser)
}
lines, err := parser.Parse(f.filename)
if err != nil {
logrus.WithFields(logrus.Fields{
"filename": f.filename,
"parser": parser.TypeName(),
"error": err,
}).Error("Error while parsing file")
continue
}
for _, line := range lines {
if t.Includes(line.TimeStamp) {
allLines = append(allLines, line)
}
}
}
}
return allLines, nil
}
func MakeLogProcessor(parsers []LogParser) *LogProcessor {
parserMap := make(map[string]LogParser)
for _, parser := range parsers {
parserMap[parser.TypeName()] = parser
}
return &LogProcessor{
parsers: parserMap,
indexedFiles: make([]indexedFile, 0),
}
}
type FindRequest struct {
TimeRange TimeRange
ResponseChan chan<- FindResponse
}
type FindResponse struct {
Lines []LogLine
Error error
}
func MakeLogProcessorService(parsers []LogParser, groups []LogFileGroup) chan<- FindRequest {
requestsChan := make(chan FindRequest)
go func(){
lp := MakeLogProcessor(parsers)
err := lp.Index(groups)
if err != nil {
logrus.WithFields(logrus.Fields{
"fileGroups": groups,
"error": err,
}).Error("Error while indexing log file groups")
close(requestsChan)
return
}
for req := range requestsChan {
lines, err := lp.FindLines(req.TimeRange)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
"timerange": req.TimeRange,
}).Error("Error while finding in time range")
}
response := FindResponse{
Lines: lines,
Error: err,
}
req.ResponseChan <- response
}
}()
return requestsChan
}
func FindWithService(serviceChan chan<- FindRequest, timeRange TimeRange) ([]LogLine, error) {
responseChan := make(chan FindResponse)
serviceChan <- FindRequest{
TimeRange: timeRange,
ResponseChan: responseChan,
}
response := <- responseChan
return response.Lines, response.Error
}