-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathtail.go
More file actions
567 lines (478 loc) · 13.7 KB
/
tail.go
File metadata and controls
567 lines (478 loc) · 13.7 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//go:generate ../../../tools/readme_config_includer/generator
//go:build !solaris
package tail
import (
"bytes"
"context"
_ "embed"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/dimchansky/utfbom"
"github.com/influxdata/tail"
"github.com/pborman/ansi"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/common/encoding"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers"
)
//go:embed sample.conf
var sampleConfig string
var (
once sync.Once
offsets = make(map[string]int64)
offsetsMutex = new(sync.Mutex)
)
type Tail struct {
Files []string `toml:"files"`
FromBeginning bool `toml:"from_beginning" deprecated:"1.34.0;1.40.0;use 'initial_read_offset' with value 'beginning' instead"`
InitialReadOffset string `toml:"initial_read_offset"`
Pipe bool `toml:"pipe"`
WatchMethod string `toml:"watch_method"`
MaxUndeliveredLines int `toml:"max_undelivered_lines"`
CharacterEncoding string `toml:"character_encoding"`
PathTag string `toml:"path_tag"`
Filters []string `toml:"filters"`
filterColors bool
Log telegraf.Logger `toml:"-"`
tailers map[string]*tail.Tail
tailersMutex sync.RWMutex
offsets map[string]int64
parserFunc telegraf.ParserFunc
wg sync.WaitGroup
acc telegraf.TrackingAccumulator
MultilineConfig multilineConfig `toml:"multiline"`
multiline *multiline
ctx context.Context
cancel context.CancelFunc
sem semaphore
decoder *encoding.Decoder
nomatch map[string]bool
}
type empty struct{}
type semaphore chan empty
func (*Tail) SampleConfig() string {
return sampleConfig
}
func (t *Tail) SetParserFunc(fn telegraf.ParserFunc) {
t.parserFunc = fn
}
func (t *Tail) Init() error {
// Backward compatibility setting
if t.InitialReadOffset == "" {
if t.FromBeginning {
t.InitialReadOffset = "beginning"
} else {
t.InitialReadOffset = "saved-or-end"
}
}
// Check settings
switch t.InitialReadOffset {
case "":
t.InitialReadOffset = "saved-or-end"
case "beginning", "end", "saved-or-end", "saved-or-beginning":
default:
return fmt.Errorf("invalid 'initial_read_offset' setting %q", t.InitialReadOffset)
}
if t.MaxUndeliveredLines == 0 {
return errors.New("max_undelivered_lines must be positive")
}
t.sem = make(semaphore, t.MaxUndeliveredLines)
for _, filter := range t.Filters {
if filter == "ansi_color" {
t.filterColors = true
}
}
// init offsets
t.offsets = make(map[string]int64)
dec, err := encoding.NewDecoder(t.CharacterEncoding)
if err != nil {
return fmt.Errorf("creating decoder failed: %w", err)
}
t.decoder = dec
// Initialize the map to keep track of patterns that did not produce any
// matching files to warn about potential permission issues only once.
t.nomatch = make(map[string]bool)
return nil
}
func (t *Tail) Start(acc telegraf.Accumulator) error {
t.acc = acc.WithTracking(t.MaxUndeliveredLines)
t.ctx, t.cancel = context.WithCancel(context.Background())
t.wg.Add(1)
go func() {
defer t.wg.Done()
for {
select {
case <-t.ctx.Done():
return
case <-t.acc.Delivered():
<-t.sem
}
}
}()
var err error
t.multiline, err = t.MultilineConfig.newMultiline()
if err != nil {
return err
}
t.tailers = make(map[string]*tail.Tail)
err = t.tailNewFiles()
if err != nil {
return err
}
// assumption that once Start is called, all parallel plugins have already been initialized
offsetsMutex.Lock()
offsets = make(map[string]int64)
offsetsMutex.Unlock()
return err
}
func (t *Tail) getSeekInfo(file string) (*tail.SeekInfo, error) {
// Pipes do not support seeking
if t.Pipe {
return nil, nil
}
// Determine the actual position for continuing
switch t.InitialReadOffset {
case "beginning":
return &tail.SeekInfo{Whence: 0, Offset: 0}, nil
case "end":
return &tail.SeekInfo{Whence: 2, Offset: 0}, nil
case "", "saved-or-end":
if offset, ok := t.offsets[file]; ok {
t.Log.Debugf("Using offset %d for %q", offset, file)
return &tail.SeekInfo{Whence: 0, Offset: offset}, nil
}
return &tail.SeekInfo{Whence: 2, Offset: 0}, nil
case "saved-or-beginning":
if offset, ok := t.offsets[file]; ok {
t.Log.Debugf("Using offset %d for %q", offset, file)
return &tail.SeekInfo{Whence: 0, Offset: offset}, nil
}
return &tail.SeekInfo{Whence: 0, Offset: 0}, nil
default:
return nil, errors.New("invalid 'initial_read_offset' setting")
}
}
func (t *Tail) GetState() interface{} {
return t.offsets
}
func (t *Tail) SetState(state interface{}) error {
offsetsState, ok := state.(map[string]int64)
if !ok {
return errors.New("state has to be of type 'map[string]int64'")
}
for k, v := range offsetsState {
t.offsets[k] = v
}
return nil
}
func (t *Tail) Gather(_ telegraf.Accumulator) error {
return t.tailNewFiles()
}
func (t *Tail) Stop() {
t.tailersMutex.Lock()
defer t.tailersMutex.Unlock()
for filename, tailer := range t.tailers {
if !t.Pipe {
// store offset for resume
offset, err := tailer.Tell()
if err == nil {
t.Log.Debugf("Recording offset %d for %q", offset, tailer.Filename)
t.offsets[tailer.Filename] = offset
} else {
t.Log.Errorf("Recording offset for %q: %v", tailer.Filename, err)
}
}
err := tailer.Stop()
if err != nil {
t.Log.Errorf("Stopping tail on %q: %v", tailer.Filename, err)
}
// Explicitly delete the tailer from the map to avoid memory leaks
delete(t.tailers, filename)
}
t.cancel()
t.wg.Wait()
// persist offsets
offsetsMutex.Lock()
for k, v := range t.offsets {
offsets[k] = v
}
offsetsMutex.Unlock()
}
func (t *Tail) tailNewFiles() error {
var poll bool
if t.WatchMethod == "poll" {
poll = true
}
// Track files that we're currently processing
currentFiles := make(map[string]bool)
// Create a "tailer" for each file
for _, filepath := range t.Files {
g, err := globpath.Compile(filepath)
if err != nil {
t.Log.Errorf("Glob %q failed to compile: %v", filepath, err)
continue
}
// Work around an issue in the doublestar library that requires read
// permissions on the directory to glob files (see
// https://github.com/bmatcuk/doublestar/issues/103).
// However, if the given file does not require globbing we can try to
// keep the file directly.
matches := g.Match()
if len(matches) == 0 {
if _, err := os.Lstat(filepath); err == nil {
matches = append(matches, filepath)
} else if !t.nomatch[filepath] && strings.ContainsAny(filepath, "*?[") {
// Read permissions are required to expand wildcards so find all
// directories followed by wildcards and check if they are readable
parts := strings.Split(filepath, string(os.PathSeparator))
for i, e := range parts {
if e == "" || !strings.ContainsAny(e, "*?[") {
continue
}
partialPath := strings.Join(parts[:i], string(os.PathSeparator))
if stat, err := os.Stat(partialPath); err != nil || !stat.IsDir() {
break
}
if _, err := os.ReadDir(partialPath); errors.Is(err, os.ErrPermission) {
t.Log.Warnf(
"Directory %q is not readable but is followed by wildcards,"+
"make sure you set read permissions or globbing will not work!", partialPath,
)
break
}
}
t.nomatch[filepath] = true
}
}
for _, file := range matches {
// Mark this file as currently being processed
currentFiles[file] = true
// Check if we're already tailing this file
t.tailersMutex.RLock()
_, alreadyTailing := t.tailers[file]
t.tailersMutex.RUnlock()
if alreadyTailing {
// already tailing this file
continue
}
seek, err := t.getSeekInfo(file)
if err != nil {
return err
}
tailer, err := tail.TailFile(file,
tail.Config{
ReOpen: true,
Follow: true,
Location: seek,
MustExist: true,
Poll: poll,
Pipe: t.Pipe,
Logger: tail.DiscardingLogger,
OpenReaderFunc: func(rd io.Reader) io.Reader {
r, _ := utfbom.Skip(t.decoder.Reader(rd))
return r
},
})
if err != nil {
t.Log.Debugf("Failed to open file (%s): %v", file, err)
continue
}
t.Log.Debugf("Tail added for %q", file)
parser, err := t.parserFunc()
if err != nil {
t.Log.Errorf("Creating parser: %v", err)
continue
}
// create a goroutine for each "tailer"
t.wg.Add(1)
// Store the tailer in the map before starting the goroutine
t.tailersMutex.Lock()
t.tailers[tailer.Filename] = tailer
t.tailersMutex.Unlock()
go func(tl *tail.Tail) {
defer t.wg.Done()
t.receiver(parser, tl)
t.Log.Debugf("Tail removed for %q", tl.Filename)
if err := tl.Err(); err != nil {
if strings.HasSuffix(err.Error(), "permission denied") {
t.Log.Errorf("Deleting tailer for %q due to: %v", tl.Filename, err)
t.tailersMutex.Lock()
delete(t.tailers, tl.Filename)
t.tailersMutex.Unlock()
} else {
t.Log.Errorf("Tailing %q: %v", tl.Filename, err)
}
}
}(tailer)
}
}
// Clean up tailers for files that are no longer being monitored
return t.cleanupUnusedTailers(currentFiles)
}
// cleanupUnusedTailers stops and removes tailers for files that are no longer being monitored.
// It uses defer to ensure the mutex is always unlocked, even if errors occur.
func (t *Tail) cleanupUnusedTailers(currentFiles map[string]bool) error {
t.tailersMutex.Lock()
defer t.tailersMutex.Unlock()
for file, tailer := range t.tailers {
if !currentFiles[file] {
// This file is no longer in our glob pattern matches
// We need to stop tailing it and remove it from our list
t.Log.Debugf("Removing tailer for %q as it's no longer in the glob pattern", file)
// Stop the tailer first to avoid race conditions
// This ensures the tail goroutine is no longer running when we call Tell()
if err := tailer.Stop(); err != nil {
t.Log.Errorf("Stopping tail on %q: %v", tailer.Filename, err)
}
// Now it's safe to get and save the offset since the tailer is stopped
if !t.Pipe {
offset, err := tailer.Tell()
if err == nil {
t.Log.Debugf("Recording offset %d for %q", offset, tailer.Filename)
t.offsets[tailer.Filename] = offset
} else {
// This can happen if the file was already removed or closed
t.Log.Debugf("Could not get offset for %q: %v", tailer.Filename, err)
}
}
// Remove from our map
delete(t.tailers, file)
}
}
return nil
}
func parseLine(parser telegraf.Parser, line string) ([]telegraf.Metric, error) {
m, err := parser.Parse([]byte(line))
if err != nil {
if errors.Is(err, parsers.ErrEOF) {
return nil, nil
}
return nil, err
}
return m, err
}
// receiver is launched as a goroutine to continuously watch a tailed logfile
// for changes, parse any incoming messages, and add to the accumulator.
func (t *Tail) receiver(parser telegraf.Parser, tailer *tail.Tail) {
// holds the individual lines of multi-line log entries.
var buffer bytes.Buffer
var timer *time.Timer
var timeout <-chan time.Time
// The multiline mode requires a timer in order to flush the multiline buffer
// if no new lines are incoming.
if t.multiline.isEnabled() {
timer = time.NewTimer(time.Duration(*t.MultilineConfig.Timeout))
timeout = timer.C
}
channelOpen := true
tailerOpen := true
var line *tail.Line
for {
line = nil
if timer != nil {
timer.Reset(time.Duration(*t.MultilineConfig.Timeout))
}
select {
case <-t.ctx.Done():
channelOpen = false
case line, tailerOpen = <-tailer.Lines:
if !tailerOpen {
channelOpen = false
}
case <-timeout:
}
var text string
if line != nil {
// Fix up files with Windows line endings.
text = strings.TrimRight(line.Text, "\r")
if t.multiline.isEnabled() {
if text = t.multiline.processLine(text, &buffer); text == "" {
continue
}
}
}
if line == nil || !channelOpen || !tailerOpen {
if text += flush(&buffer); text == "" {
if !channelOpen {
return
}
continue
}
}
if line != nil && line.Err != nil {
t.Log.Errorf("Tailing %q: %v", tailer.Filename, line.Err)
continue
}
if t.filterColors {
out, err := ansi.Strip([]byte(text))
if err != nil {
t.Log.Errorf("Cannot strip ansi colors from %s: %s", text, err)
}
text = string(out)
}
metrics, err := parseLine(parser, text)
if err != nil {
t.Log.Errorf("Malformed log line in %q: [%q]: %v",
tailer.Filename, text, err)
continue
}
if len(metrics) == 0 {
once.Do(func() {
t.Log.Debug(internal.NoMetricsCreatedMsg)
})
}
if t.PathTag != "" {
for _, metric := range metrics {
metric.AddTag(t.PathTag, tailer.Filename)
}
}
// try writing out metric first without blocking
select {
case t.sem <- empty{}:
t.acc.AddTrackingMetricGroup(metrics)
if t.ctx.Err() != nil {
return // exit!
}
continue // next loop
default:
// no room. switch to blocking write.
}
// Block until plugin is stopping or room is available to add metrics.
select {
case <-t.ctx.Done():
return
// Tail is trying to close so drain the sem to allow the receiver
// to exit. This condition is hit when the tailer may have hit the
// maximum undelivered lines and is trying to close.
case <-tailer.Dying():
<-t.sem
case t.sem <- empty{}:
t.acc.AddTrackingMetricGroup(metrics)
}
}
}
func newTail() *Tail {
offsetsMutex.Lock()
offsetsCopy := make(map[string]int64, len(offsets))
for k, v := range offsets {
offsetsCopy[k] = v
}
offsetsMutex.Unlock()
return &Tail{
MaxUndeliveredLines: 1000,
offsets: offsetsCopy,
PathTag: "path",
}
}
func init() {
inputs.Add("tail", func() telegraf.Input {
return newTail()
})
}