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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
package fs
import (
"bufio"
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/pool"
"github.com/mimecast/dtail/internal/lcontext"
"github.com/mimecast/dtail/internal/regex"
"github.com/DataDog/zstd"
)
// Used to tail and filter a local log file.
type readFile struct {
// Various statistics (e.g. regex hit percentage, transfer percentage).
stats
// Path of log file to tail.
filePath string
// The glob identifier of the file.
globID string
// Channel to send a server message to the dtail client
serverMessages chan<- string
// Periodically retry reading file.
retry bool
// Can I skip messages when there are too many?
canSkipLines bool
// Seek to the EOF before processing file?
seekEOF bool
}
// String returns the string representation of the readFile
func (f readFile) String() string {
return fmt.Sprintf(
"readFile(filePath:%s,globID:%s,retry:%v,canSkipLines:%v,seekEOF:%v)",
f.filePath,
f.globID,
f.retry,
f.canSkipLines,
f.seekEOF)
}
// FilePath returns the full file path.
func (f readFile) FilePath() string {
return f.filePath
}
// Retry reading the file on error?
func (f readFile) Retry() bool {
return f.retry
}
// Start tailing a log file.
func (f readFile) Start(ctx context.Context, ltx lcontext.LContext,
lines chan<- line.Line, re regex.Regex) error {
reader, fd, err := f.makeReader()
if fd != nil {
defer fd.Close()
}
if err != nil {
return err
}
rawLines := make(chan *bytes.Buffer, 100)
truncate := make(chan struct{})
readCtx, readCancel := context.WithCancel(ctx)
var filterWg sync.WaitGroup
filterWg.Add(1)
go f.periodicTruncateCheck(ctx, truncate)
go func() {
f.filter(ctx, ltx, rawLines, lines, re)
filterWg.Done()
// If the filter stopped, make the reader stop too, no need to read
// more data if there is nothing more the filter wants to filter for!
// E.g. it could be that we only want to filter N matches but not more.
readCancel()
}()
err = f.read(readCtx, fd, reader, rawLines, truncate)
close(rawLines)
// Filter may sends some data still. So wait until it is done here.
filterWg.Wait()
return err
}
func (f readFile) makeReader() (*bufio.Reader, *os.File, error) {
if f.filePath == "" && f.globID == "-" {
return f.makePipeReader()
}
return f.makeFileReader()
}
func (f readFile) makeFileReader() (*bufio.Reader, *os.File, error) {
var reader *bufio.Reader
fd, err := os.Open(f.filePath)
if err != nil {
return reader, fd, err
}
if f.seekEOF {
fd.Seek(0, io.SeekEnd)
}
reader, err = f.makeCompressedFileReader(fd)
if err != nil {
return reader, fd, err
}
return reader, fd, nil
}
func (f readFile) makePipeReader() (*bufio.Reader, *os.File, error) {
return bufio.NewReader(os.Stdin), nil, nil
}
func (f readFile) periodicTruncateCheck(ctx context.Context, truncate chan struct{}) {
for {
select {
case <-time.After(time.Second * 3):
select {
case truncate <- struct{}{}:
case <-ctx.Done():
}
case <-ctx.Done():
return
}
}
}
func (f readFile) makeCompressedFileReader(fd *os.File) (reader *bufio.Reader, err error) {
switch {
case strings.HasSuffix(f.FilePath(), ".gz"):
fallthrough
case strings.HasSuffix(f.FilePath(), ".gzip"):
dlog.Common.Info(f.FilePath(), "Detected gzip compression format")
var gzipReader *gzip.Reader
gzipReader, err = gzip.NewReader(fd)
if err != nil {
return
}
reader = bufio.NewReader(gzipReader)
case strings.HasSuffix(f.FilePath(), ".zst"):
dlog.Common.Info(f.FilePath(), "Detected zstd compression format")
reader = bufio.NewReader(zstd.NewReader(fd))
default:
reader = bufio.NewReader(fd)
}
return
}
func (f readFile) read(ctx context.Context, fd *os.File, reader *bufio.Reader,
rawLines chan *bytes.Buffer, truncate <-chan struct{}) error {
var offset uint64
lineLengthThreshold := 1024 * 1024 // 1mb
warnedAboutLongLine := false
message := pool.BytesBuffer.Get().(*bytes.Buffer)
for {
b, err := reader.ReadByte()
if err != nil {
if err != io.EOF {
return err
}
select {
case <-truncate:
if isTruncated, err := f.truncated(fd); isTruncated {
return err
}
case <-ctx.Done():
return nil
default:
}
if !f.seekEOF {
dlog.Common.Info(f.FilePath(), "End of file reached")
if len(message.Bytes()) > 0 {
select {
case rawLines <- message:
case <-ctx.Done():
}
}
return nil
}
time.Sleep(time.Millisecond * 100)
continue
}
offset++
message.WriteByte(b)
switch b {
case '\n':
select {
case rawLines <- message:
message = pool.BytesBuffer.Get().(*bytes.Buffer)
warnedAboutLongLine = false
case <-ctx.Done():
return nil
}
default:
// TODO: Add integration test with input file having a very long line.
if message.Len() >= lineLengthThreshold {
if !warnedAboutLongLine {
f.serverMessages <- dlog.Common.Warn(f.filePath,
"Long log line, splitting into multiple lines")
warnedAboutLongLine = true
}
select {
case rawLines <- message:
message = pool.BytesBuffer.Get().(*bytes.Buffer)
case <-ctx.Done():
return nil
}
}
}
}
}
// Filter log lines matching a given regular expression.
func (f readFile) filter(ctx context.Context, ltx lcontext.LContext,
rawLines <-chan *bytes.Buffer, lines chan<- line.Line, re regex.Regex) {
// Do we have any kind of local context settings? If so then run the more complex
// filterWithLContext method.
if ltx.Has() {
// We can not skip transmitting any lines to the client with a local
// grep context specified.
f.canSkipLines = false
f.filterWithLContext(ctx, ltx, rawLines, lines, re)
return
}
f.filterWithoutLContext(ctx, rawLines, lines, re)
}
func (f readFile) filterWithoutLContext(ctx context.Context, rawLines <-chan *bytes.Buffer,
lines chan<- line.Line, re regex.Regex) {
for {
select {
case line, ok := <-rawLines:
f.updatePosition()
if !ok {
return
}
if filteredLine, ok := f.transmittable(line, len(lines), cap(lines), re); ok {
select {
case lines <- filteredLine:
case <-ctx.Done():
return
}
}
}
}
}
// Filter log lines matching a given regular expression, however with local grep context.
func (f readFile) filterWithLContext(ctx context.Context, ltx lcontext.LContext,
rawLines <-chan *bytes.Buffer, lines chan<- line.Line, re regex.Regex) {
// Scenario 1: Finish once maxCount hits found
maxCount := ltx.MaxCount
processMaxCount := maxCount > 0
maxReached := false
// Scenario 2: Print prev. N lines when current line matches.
before := ltx.BeforeContext
processBefore := before > 0
var beforeBuf chan *bytes.Buffer
if processBefore {
beforeBuf = make(chan *bytes.Buffer, before)
}
// Screnario 3: Print next N lines when current line matches.
after := 0
processAfter := ltx.AfterContext > 0
for lineBytesBuffer := range rawLines {
f.updatePosition()
if !re.Match(lineBytesBuffer.Bytes()) {
f.updateLineNotMatched()
if processAfter && after > 0 {
after--
myLine := line.Line{
Content: lineBytesBuffer,
SourceID: f.globID,
Count: f.totalLineCount(),
TransmittedPerc: 100,
}
select {
case lines <- myLine:
case <-ctx.Done():
return
}
} else if processBefore {
// Keep last num BeforeContext raw messages.
select {
case beforeBuf <- lineBytesBuffer:
default:
pool.RecycleBytesBuffer(<-beforeBuf)
beforeBuf <- lineBytesBuffer
}
}
continue
}
f.updateLineMatched()
if processAfter {
if maxReached {
return
}
after = ltx.AfterContext
}
if processBefore {
i := uint64(len(beforeBuf))
for {
select {
case lineBytesBuffer := <-beforeBuf:
myLine := line.Line{
Content: lineBytesBuffer,
SourceID: f.globID,
Count: f.totalLineCount() - i,
TransmittedPerc: 100,
}
i--
select {
case lines <- myLine:
case <-ctx.Done():
return
}
default:
// beforeBuf is now empty.
}
if len(beforeBuf) == 0 {
break
}
}
}
line := line.Line{
Content: lineBytesBuffer,
SourceID: f.globID,
Count: f.totalLineCount(),
TransmittedPerc: 100,
}
select {
case lines <- line:
if processMaxCount {
maxCount--
if maxCount == 0 {
if !processAfter || after == 0 {
return
}
// Unfortunatley we have to continue filter, as there might be more lines to print
maxReached = true
}
}
case <-ctx.Done():
return
}
}
}
func (f readFile) transmittable(lineBytesBuffer *bytes.Buffer, length, capacity int,
re regex.Regex) (line.Line, bool) {
var read line.Line
if !re.Match(lineBytesBuffer.Bytes()) {
f.updateLineNotMatched()
f.updateLineNotTransmitted()
return read, false
}
f.updateLineMatched()
// Can we actually send more messages, channel capacity reached?
if f.canSkipLines && length >= capacity {
f.updateLineNotTransmitted()
return read, false
}
f.updateLineTransmitted()
read = line.Line{
Content: lineBytesBuffer,
SourceID: f.globID,
Count: f.totalLineCount(),
TransmittedPerc: f.transmittedPerc(),
}
return read, true
}
// Check wether log file is truncated. Returns nil if not.
func (f readFile) truncated(fd *os.File) (bool, error) {
if fd == nil {
return false, nil
}
dlog.Common.Debug(f.filePath, "File truncation check")
// Can not seek currently open FD.
curPos, err := fd.Seek(0, os.SEEK_CUR)
if err != nil {
return true, err
}
// Can not open file at original path.
pathFd, err := os.Open(f.filePath)
if err != nil {
return true, err
}
defer pathFd.Close()
// Can not seek file at original path.
pathPos, err := pathFd.Seek(0, io.SeekEnd)
if err != nil {
return true, err
}
if curPos > pathPos {
return true, errors.New("File got truncated")
}
return false, nil
}
|