Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promtail: Fix excludepath not evaluated on newly added files #9831

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
##### Fixes
* [11074](https://github.com/grafana/loki/pull/11074) **hainenber** Fix panic in lambda-promtail due to mishandling of empty DROP_LABELS env var.
* [11195](https://github.com/grafana/loki/pull/11195) **canuteson** Generate tsdb_shipper storage_config even if using_boltdb_shipper is false
* [9831](https://github.com/grafana/loki/pull/9831) **sijmenhuizenga**: Fix Promtail excludepath not evaluated on newly added files.
* [11551](https://github.com/grafana/loki/pull/11551) **dannykopping** Do not reflect label names in request metrics' "route" label.

##### Changes
Expand Down
14 changes: 14 additions & 0 deletions clients/pkg/promtail/targets/file/filetarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ func (t *FileTarget) startTailing(ps []string) {
continue
}

if t.pathExclude != "" {
matched, err := doublestar.Match(t.pathExclude, p)
if err != nil {
level.Error(t.logger).Log("msg", "ignoring file, exclude pattern match failed", "error", err, "filename", p, "pathExclude", t.pathExclude)
t.metrics.totalBytes.DeleteLabelValues(p)
continue
}
if matched {
level.Info(t.logger).Log("msg", "ignoring file", "error", "file matches exclude pattern", "filename", p, "pathExclude", t.pathExclude)
t.metrics.totalBytes.DeleteLabelValues(p)
continue
}
}

var reader Reader
if t.decompressCfg != nil && t.decompressCfg.Enabled {
level.Debug(t.logger).Log("msg", "reading from compressed file", "filename", p)
Expand Down
12 changes: 11 additions & 1 deletion clients/pkg/promtail/targets/file/filetarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ func TestHandleFileCreationEvent(t *testing.T) {
positionsFileName := filepath.Join(dirName, "positions.yml")
logDir := filepath.Join(dirName, "log")
logFile := filepath.Join(logDir, "test1.log")
logFileIgnored := filepath.Join(logDir, "test.donot.log")

if err := os.MkdirAll(logDir, 0750); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -511,7 +512,8 @@ func TestHandleFileCreationEvent(t *testing.T) {
}
}()

target, err := NewFileTarget(metrics, logger, client, ps, path, "", nil, nil, &Config{
pathExclude := "**/*.donot.log"
target, err := NewFileTarget(metrics, logger, client, ps, path, pathExclude, nil, nil, &Config{
// To handle file creation event from channel, set enough long time as sync period
SyncPeriod: 10 * time.Minute,
}, DefaultWatchConig, fakeFileHandler, fakeTargetHandler, "", nil)
Expand All @@ -523,10 +525,18 @@ func TestHandleFileCreationEvent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = os.Create(logFileIgnored)
if err != nil {
t.Fatal(err)
}
fakeFileHandler <- fsnotify.Event{
Name: logFile,
Op: fsnotify.Create,
}
fakeFileHandler <- fsnotify.Event{
Name: logFileIgnored,
Op: fsnotify.Create,
}
requireEventually(t, func() bool {
return len(target.readers) == 1
}, "Expected tails to be 1 at this point in the test...")
Expand Down