-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
filetarget.go
440 lines (383 loc) · 11.7 KB
/
filetarget.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
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
package file
import (
"flag"
"os"
"path/filepath"
"time"
"github.com/bmatcuk/doublestar"
"github.com/fsnotify/fsnotify"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/grafana/loki/clients/pkg/promtail/api"
"github.com/grafana/loki/clients/pkg/promtail/client"
"github.com/grafana/loki/clients/pkg/promtail/positions"
"github.com/grafana/loki/clients/pkg/promtail/targets/target"
)
const (
FilenameLabel = "filename"
)
// Config describes behavior for Target
type Config struct {
SyncPeriod time.Duration `mapstructure:"sync_period" yaml:"sync_period"`
Stdin bool `mapstructure:"stdin" yaml:"stdin"`
}
// RegisterFlags with prefix registers flags where every name is prefixed by
// prefix. If prefix is a non-empty string, prefix should end with a period.
func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
f.DurationVar(&cfg.SyncPeriod, prefix+"target.sync-period", 10*time.Second, "Period to resync directories being watched and files being tailed.")
f.BoolVar(&cfg.Stdin, prefix+"stdin", false, "Set to true to pipe logs to promtail.")
}
// RegisterFlags register flags.
func (cfg *Config) RegisterFlags(flags *flag.FlagSet) {
cfg.RegisterFlagsWithPrefix("", flags)
}
type fileTargetEventType string
const (
fileTargetEventWatchStart fileTargetEventType = "WATCH_START"
fileTargetEventWatchStop fileTargetEventType = "WATCH_STOP"
)
type fileTargetEvent struct {
path string
eventType fileTargetEventType
}
// FileTarget describes a particular set of logs.
// nolint:revive
type FileTarget struct {
metrics *Metrics
logger log.Logger
handler api.EntryHandler
positions positions.Positions
labels model.LabelSet
discoveredLabels model.LabelSet
fileEventWatcher chan fsnotify.Event
targetEventHandler chan fileTargetEvent
watches map[string]struct{}
path string
pathExclude string
quit chan struct{}
done chan struct{}
readers map[string]Reader
targetConfig *Config
encoding string
}
// NewFileTarget create a new FileTarget.
func NewFileTarget(
metrics *Metrics,
logger log.Logger,
handler api.EntryHandler,
positions positions.Positions,
path string,
pathExclude string,
labels model.LabelSet,
discoveredLabels model.LabelSet,
targetConfig *Config,
fileEventWatcher chan fsnotify.Event,
targetEventHandler chan fileTargetEvent,
encoding string,
) (*FileTarget, error) {
t := &FileTarget{
logger: logger,
metrics: metrics,
path: path,
pathExclude: pathExclude,
labels: labels,
discoveredLabels: discoveredLabels,
handler: api.AddLabelsMiddleware(labels).Wrap(handler),
positions: positions,
quit: make(chan struct{}),
done: make(chan struct{}),
readers: map[string]Reader{},
targetConfig: targetConfig,
fileEventWatcher: fileEventWatcher,
targetEventHandler: targetEventHandler,
encoding: encoding,
}
go t.run()
return t, nil
}
// Ready if at least one file is being tailed
func (t *FileTarget) Ready() bool {
return len(t.readers) > 0
}
// Stop the target.
func (t *FileTarget) Stop() {
close(t.quit)
<-t.done
t.handler.Stop()
}
// Type implements a Target
func (t *FileTarget) Type() target.TargetType {
return target.FileTargetType
}
// DiscoveredLabels implements a Target
func (t *FileTarget) DiscoveredLabels() model.LabelSet {
return t.discoveredLabels
}
// Labels implements a Target
func (t *FileTarget) Labels() model.LabelSet {
return t.labels
}
// Details implements a Target
func (t *FileTarget) Details() interface{} {
files := map[string]int64{}
for fileName := range t.readers {
files[fileName], _ = t.positions.Get(fileName)
}
return files
}
func (t *FileTarget) run() {
defer func() {
for _, v := range t.readers {
v.Stop()
}
level.Info(t.logger).Log("msg", "filetarget: watcher closed, tailer stopped, positions saved", "path", t.path)
close(t.done)
}()
err := t.sync()
if err != nil {
level.Error(t.logger).Log("msg", "error running sync function", "error", err)
}
ticker := time.NewTicker(t.targetConfig.SyncPeriod)
defer ticker.Stop()
for {
select {
case event, ok := <-t.fileEventWatcher:
if !ok {
// fileEventWatcher has been closed
return
}
switch event.Op {
case fsnotify.Create:
t.startTailing([]string{event.Name})
default:
// No-op we only care about Create events
}
case <-ticker.C:
err := t.sync()
if err != nil {
level.Error(t.logger).Log("msg", "error running sync function", "error", err)
}
case <-t.quit:
return
}
}
}
func (t *FileTarget) sync() error {
var matches, matchesExcluded []string
if fi, err := os.Stat(t.path); err == nil && !fi.IsDir() {
// if the path points to a file that exists, then it we can skip the Glob search
matches = []string{t.path}
} else {
// Gets current list of files to tail.
matches, err = doublestar.Glob(t.path)
if err != nil {
return errors.Wrap(err, "filetarget.sync.filepath.Glob")
}
}
if fi, err := os.Stat(t.pathExclude); err == nil && !fi.IsDir() {
matchesExcluded = []string{t.pathExclude}
} else {
matchesExcluded, err = doublestar.Glob(t.pathExclude)
if err != nil {
return errors.Wrap(err, "filetarget.sync.filepathexclude.Glob")
}
}
for i := 0; i < len(matchesExcluded); i++ {
for j := 0; j < len(matches); j++ {
if matchesExcluded[i] == matches[j] {
// exclude this specific match
matches = append(matches[:j], matches[j+1:]...)
}
}
}
if len(matches) == 0 {
level.Debug(t.logger).Log("msg", "no files matched requested path, nothing will be tailed", "path", t.path, "pathExclude", t.pathExclude)
}
// Gets absolute path for each pattern.
for i := 0; i < len(matches); i++ {
if !filepath.IsAbs(matches[i]) {
path, err := filepath.Abs(matches[i])
if err != nil {
return errors.Wrap(err, "filetarget.sync.filepath.Abs")
}
matches[i] = path
}
}
// Record the size of all the files matched by the Glob pattern.
t.reportSize(matches)
// Get the current unique set of dirs to watch.
dirs := map[string]struct{}{}
for _, p := range matches {
dirs[filepath.Dir(p)] = struct{}{}
}
// Add any directories which are not already being watched.
toStartWatching := missing(t.watches, dirs)
t.startWatching(toStartWatching)
// Remove any directories which no longer need watching.
toStopWatching := missing(dirs, t.watches)
t.stopWatching(toStopWatching)
// fsnotify.Watcher doesn't allow us to see what is currently being watched so we have to track it ourselves.
t.watches = dirs
// Check if any running tailers have stopped because of errors and remove them from the running list
// (They will be restarted in startTailing)
t.pruneStoppedTailers()
// Start tailing all of the matched files if not already doing so.
t.startTailing(matches)
// Stop tailing any files which no longer exist
toStopTailing := toStopTailing(matches, t.readers)
t.stopTailingAndRemovePosition(toStopTailing)
return nil
}
func (t *FileTarget) startWatching(dirs map[string]struct{}) {
for dir := range dirs {
if _, ok := t.watches[dir]; ok {
continue
}
level.Info(t.logger).Log("msg", "watching new directory", "directory", dir)
t.targetEventHandler <- fileTargetEvent{
path: dir,
eventType: fileTargetEventWatchStart,
}
}
}
func (t *FileTarget) stopWatching(dirs map[string]struct{}) {
for dir := range dirs {
if _, ok := t.watches[dir]; !ok {
continue
}
level.Info(t.logger).Log("msg", "removing directory from watcher", "directory", dir)
t.targetEventHandler <- fileTargetEvent{
path: dir,
eventType: fileTargetEventWatchStop,
}
}
}
func (t *FileTarget) startTailing(ps []string) {
for _, p := range ps {
if _, ok := t.readers[p]; ok {
continue
}
fi, err := os.Stat(p)
if err != nil {
level.Error(t.logger).Log("msg", "failed to tail file, stat failed", "error", err, "filename", p)
t.metrics.totalBytes.DeleteLabelValues(p)
continue
}
if fi.IsDir() {
level.Info(t.logger).Log("msg", "failed to tail file", "error", "file is a directory", "filename", p)
t.metrics.totalBytes.DeleteLabelValues(p)
continue
}
var reader Reader
if isCompressed(p) {
level.Debug(t.logger).Log("msg", "reading from compressed file", "filename", p)
decompressor, err := newDecompressor(t.metrics, t.logger, t.handler, t.positions, p, t.encoding)
if err != nil {
level.Error(t.logger).Log("msg", "failed to start decompressor", "error", err, "filename", p)
continue
}
reader = decompressor
} else {
level.Debug(t.logger).Log("msg", "tailing new file", "filename", p)
tailer, err := newTailer(t.metrics, t.logger, t.handler, t.positions, p, t.encoding)
if err != nil {
level.Error(t.logger).Log("msg", "failed to start tailer", "error", err, "filename", p)
continue
}
reader = tailer
}
t.readers[p] = reader
}
}
func isCompressed(p string) bool {
ext := filepath.Ext(p)
for format := range supportedCompressedFormats() {
if ext == format {
return true
}
}
return false
}
// stopTailingAndRemovePosition will stop the tailer and remove the positions entry.
// Call this when a file no longer exists and you want to remove all traces of it.
func (t *FileTarget) stopTailingAndRemovePosition(ps []string) {
for _, p := range ps {
if reader, ok := t.readers[p]; ok {
reader.Stop()
t.positions.Remove(reader.Path())
delete(t.readers, p)
}
if h, ok := t.handler.(api.InstrumentedEntryHandler); ok {
h.UnregisterLatencyMetric(prometheus.Labels{client.LatencyLabel: p})
}
}
}
// pruneStoppedTailers removes any tailers which have stopped running from
// the list of active tailers. This allows them to be restarted if there were errors.
func (t *FileTarget) pruneStoppedTailers() {
toRemove := make([]string, 0, len(t.readers))
for k, t := range t.readers {
if !t.IsRunning() {
toRemove = append(toRemove, k)
}
}
for _, tr := range toRemove {
delete(t.readers, tr)
}
}
func toStopTailing(nt []string, et map[string]Reader) []string {
// Make a set of all existing tails
existingTails := make(map[string]struct{}, len(et))
for file := range et {
existingTails[file] = struct{}{}
}
// Make a set of what we are about to start tailing
newTails := make(map[string]struct{}, len(nt))
for _, p := range nt {
newTails[p] = struct{}{}
}
// Find the tails in our existing which are not in the new, these need to be stopped!
ts := missing(newTails, existingTails)
ta := make([]string, len(ts))
i := 0
for t := range ts {
ta[i] = t
i++
}
return ta
}
func (t *FileTarget) reportSize(ms []string) {
for _, m := range ms {
// Ask the tailer to update the size if a tailer exists, this keeps position and size metrics in sync
if reader, ok := t.readers[m]; ok {
err := reader.MarkPositionAndSize()
if err != nil {
level.Warn(t.logger).Log("msg", "failed to get file size from tailer, ", "file", m, "error", err)
return
}
} else {
// Must be a new file, just directly read the size of it
fi, err := os.Stat(m)
if err != nil {
// If the file was deleted between when the glob match and here,
// we just ignore recording a size for it,
// the tail code will also check if the file exists before creating a tailer.
return
}
t.metrics.totalBytes.WithLabelValues(m).Set(float64(fi.Size()))
}
}
}
// Returns the elements from set b which are missing from set a
func missing(as map[string]struct{}, bs map[string]struct{}) map[string]struct{} {
c := map[string]struct{}{}
for a := range bs {
if _, ok := as[a]; !ok {
c[a] = struct{}{}
}
}
return c
}