Skip to content

Commit

Permalink
Do not mark the position if the file is removed, doing so only result…
Browse files Browse the repository at this point in the history
…s in an error because you can't get the file size of a removed file. (grafana#2658)

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
slim-bean authored and cyriltovena committed Oct 21, 2020
1 parent 0ac1ce4 commit fb789b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/promtail/targets/file/filetarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (t *FileTarget) run() {
defer func() {
helpers.LogError("closing watcher", t.watcher.Close)
for _, v := range t.tails {
v.stop()
v.stop(false)
}
level.Debug(t.logger).Log("msg", "watcher closed, tailer stopped, positions saved")
close(t.done)
Expand Down Expand Up @@ -313,7 +313,7 @@ func (t *FileTarget) startTailing(ps []string) {
func (t *FileTarget) stopTailingAndRemovePosition(ps []string) {
for _, p := range ps {
if tailer, ok := t.tails[p]; ok {
tailer.stop()
tailer.stop(true)
t.positions.Remove(tailer.path)
delete(t.tails, p)
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/promtail/targets/file/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ func (t *tailer) markPositionAndSize() error {
return nil
}

func (t *tailer) stop() {
func (t *tailer) stop(removed bool) {
// Save the current position before shutting down tailer
err := t.markPositionAndSize()
if err != nil {
level.Error(t.logger).Log("msg", "error marking file position when stopping tailer", "path", t.path, "error", err)
if !removed {
err := t.markPositionAndSize()
if err != nil {
level.Error(t.logger).Log("msg", "error marking file position when stopping tailer", "path", t.path, "error", err)
}
}
close(t.quit)
<-t.done
Expand Down

0 comments on commit fb789b8

Please sign in to comment.