diff --git a/pkg/promtail/positions/positions.go b/pkg/promtail/positions/positions.go index c2dedf13100bc2a6f1c6e516b7dfb46b2098ada3..cddd031cc99753628f5ba59f40daef8527da8eff 100644 --- a/pkg/promtail/positions/positions.go +++ b/pkg/promtail/positions/positions.go @@ -107,6 +107,7 @@ func (p *Positions) run() { return case <-ticker.C: p.save() + p.cleanup() } } } @@ -124,6 +125,21 @@ func (p *Positions) save() { } } +func (p *Positions) cleanup() { + for k := range p.positions { + if _, err := os.Stat(k); err == nil { + // File still exists. + } else if os.IsNotExist(err) { + // File no longer exists. + p.Remove(k) + } else { + // Can't determine if file exists or not, some other error. + level.Warn(p.logger).Log("msg", "could not determine if log file "+ + "still exists while cleaning positions file", "error", err) + } + } +} + func readPositionsFile(filename string) (map[string]int64, error) { buf, err := ioutil.ReadFile(filepath.Clean(filename)) if err != nil {