From f9018830c311da71475079b636ad38c6dfd8481f Mon Sep 17 00:00:00 2001
From: Anthony Woods <awoods@grafana.com>
Date: Thu, 10 Jan 2019 03:28:25 +0800
Subject: [PATCH] handle file renames

When a file is renamed a "rename" and a "create" event are sent.
We need to treat the rename as a remove.  If the new filename matches
the path glob, then we will start tailing it when the "create" event
is reveived.
---
 pkg/promtail/target.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/pkg/promtail/target.go b/pkg/promtail/target.go
index d271328a..4330b0f6 100644
--- a/pkg/promtail/target.go
+++ b/pkg/promtail/target.go
@@ -157,6 +157,14 @@ func (t *Target) run() {
 					tailer.cleanup()
 					delete(t.tails, event.Name)
 				}
+			case fsnotify.Rename:
+				// Rename is only issued on the original file path; the new name receives a Create event
+				tailer, ok := t.tails[event.Name]
+				if ok {
+					helpers.LogError("stopping tailer", tailer.stop)
+					tailer.cleanup()
+					delete(t.tails, event.Name)
+				}
 
 			default:
 				level.Debug(t.logger).Log("msg", "got unknown event", "event", event)
-- 
GitLab