-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
tailer.go
142 lines (121 loc) · 3.17 KB
/
tailer.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
package targets
import (
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/hpcloud/tail"
"github.com/prometheus/common/model"
"github.com/grafana/loki/pkg/promtail/api"
"github.com/grafana/loki/pkg/promtail/positions"
)
type tailer struct {
logger log.Logger
handler api.EntryHandler
positions *positions.Positions
path string
tail *tail.Tail
quit chan struct{}
done chan struct{}
}
func newTailer(logger log.Logger, handler api.EntryHandler, positions *positions.Positions, path string) (*tailer, error) {
// Simple check to make sure the file we are tailing doesn't
// have a position already saved which is past the end of the file.
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
pos, err := positions.Get(path)
if err != nil {
return nil, err
}
if fi.Size() < pos {
positions.Remove(path)
}
tail, err := tail.TailFile(path, tail.Config{
Follow: true,
Poll: true,
ReOpen: true,
Location: &tail.SeekInfo{
Offset: pos,
Whence: 0,
},
})
if err != nil {
return nil, err
}
tailer := &tailer{
logger: logger,
handler: api.AddLabelsMiddleware(model.LabelSet{FilenameLabel: model.LabelValue(path)}).Wrap(handler),
positions: positions,
path: path,
tail: tail,
quit: make(chan struct{}),
done: make(chan struct{}),
}
go tailer.run()
filesActive.Add(1.)
return tailer, nil
}
func (t *tailer) run() {
level.Info(t.logger).Log("msg", "start tailing file", "path", t.path)
positionSyncPeriod := t.positions.SyncPeriod()
positionWait := time.NewTicker(positionSyncPeriod)
defer func() {
positionWait.Stop()
close(t.done)
}()
for {
select {
case <-positionWait.C:
err := t.markPosition()
if err != nil {
level.Error(t.logger).Log("msg", "error getting tail position", "path", t.path, "error", err)
continue
}
case line, ok := <-t.tail.Lines:
if !ok {
return
}
if line.Err != nil {
level.Error(t.logger).Log("msg", "error reading line", "path", t.path, "error", line.Err)
}
readLines.WithLabelValues(t.path).Inc()
logLengthHistogram.WithLabelValues(t.path).Observe(float64(len(line.Text)))
if err := t.handler.Handle(model.LabelSet{}, line.Time, line.Text); err != nil {
level.Error(t.logger).Log("msg", "error handling line", "path", t.path, "error", err)
}
case <-t.quit:
return
}
}
}
func (t *tailer) markPosition() error {
pos, err := t.tail.Tell()
if err != nil {
return err
}
readBytes.WithLabelValues(t.path).Set(float64(pos))
t.positions.Put(t.path, pos)
return nil
}
func (t *tailer) stop() error {
// Save the current position before shutting down tailer
err := t.markPosition()
if err != nil {
level.Error(t.logger).Log("msg", "error getting tail position", "path", t.path, "error", err)
}
err = t.tail.Stop()
close(t.quit)
<-t.done
filesActive.Add(-1.)
// When we stop tailing the file, also un-export metrics related to the file
readBytes.DeleteLabelValues(t.path)
totalBytes.DeleteLabelValues(t.path)
logLengthHistogram.DeleteLabelValues(t.path)
level.Info(t.logger).Log("msg", "stopped tailing file", "path", t.path)
return err
}
func (t *tailer) cleanup() {
t.positions.Remove(t.path)
}