Promtail: Fix file descriptor leak (#8987)

This reduces the amount of open file descriptors per followed journal
file from 3 to 2. It also releases file descriptors for removed files
(journal vacuuming).

The `defaultJournalEntryFunc` is only called once upon startup. It
creates a `Journal` instance to only read a single entry to determine
the age of the entry referenced in the positions file. Right after, this
instance is not required anymore.

The actually used `Journal` instance used to follow the journal is
created inside `journalTargetWithReader`.
pull/8984/head^2
René Scheibe 3 years ago committed by GitHub
parent f785faa820
commit 954df433e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 18
      clients/pkg/promtail/targets/journal/journaltarget.go

@ -77,6 +77,7 @@
* [8988](https://github.com/grafana/loki/pull/8988) **darxriggs**: Promtail: Prevent logging errors on normal shutdown.
* [9155](https://github.com/grafana/loki/pull/9155) **farodin91**: Promtail: Break on iterate journal failure.
* [8987](https://github.com/grafana/loki/pull/8987) **darxriggs**: Promtail: Fix file descriptor leak.
#### LogCLI

@ -56,21 +56,25 @@ var defaultJournalReaderFunc = func(c sdjournal.JournalReaderConfig) (journalRea
return sdjournal.NewJournalReader(c)
}
var defaultJournalEntryFunc = func(c sdjournal.JournalReaderConfig, cursor string) (*sdjournal.JournalEntry, error) {
var (
journal *sdjournal.Journal
err error
)
var defaultJournalEntryFunc = func(c sdjournal.JournalReaderConfig, cursor string) (entry *sdjournal.JournalEntry, err error) {
var journal *sdjournal.Journal
if c.Path != "" {
journal, err = sdjournal.NewJournalFromDir(c.Path)
} else {
journal, err = sdjournal.NewJournal()
}
if err != nil {
return nil, err
} else if err := journal.SeekCursor(cursor); err != nil {
}
defer func() {
if errClose := journal.Close(); err == nil {
err = errClose
}
}()
err = journal.SeekCursor(cursor)
if err != nil {
return nil, err
}

Loading…
Cancel
Save