diff --git a/plugins/inputs/tail/README.md b/plugins/inputs/tail/README.md index 5664f8704eec3..abdf0878aff56 100644 --- a/plugins/inputs/tail/README.md +++ b/plugins/inputs/tail/README.md @@ -64,6 +64,9 @@ The plugin expects messages in one of the ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md data_format = "influx" + ## Set the tag that will contain the path of the tailed file. If you don't want this tag, set it to an empty string. + # path_tag = "path" + ## multiline parser/codec ## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html #[inputs.tail.multiline] diff --git a/plugins/inputs/tail/tail.go b/plugins/inputs/tail/tail.go index c7b16eb7a4631..84a91635540bf 100644 --- a/plugins/inputs/tail/tail.go +++ b/plugins/inputs/tail/tail.go @@ -40,6 +40,7 @@ type Tail struct { WatchMethod string `toml:"watch_method"` MaxUndeliveredLines int `toml:"max_undelivered_lines"` CharacterEncoding string `toml:"character_encoding"` + PathTag string `toml:"path_tag"` Log telegraf.Logger `toml:"-"` tailers map[string]*tail.Tail @@ -70,6 +71,7 @@ func NewTail() *Tail { FromBeginning: false, MaxUndeliveredLines: 1000, offsets: offsetsCopy, + PathTag: "path", } } @@ -115,6 +117,9 @@ const sampleConfig = ` ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md data_format = "influx" + ## Set the tag that will contain the path of the tailed file. If you don't want this tag, set it to an empty string. + # path_tag = "path" + ## multiline parser/codec ## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html #[inputs.tail.multiline] @@ -380,8 +385,10 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) { } firstLine = false - for _, metric := range metrics { - metric.AddTag("path", tailer.Filename) + if t.PathTag != "" { + for _, metric := range metrics { + metric.AddTag(t.PathTag, tailer.Filename) + } } // try writing out metric first without blocking diff --git a/plugins/inputs/tail/tail_test.go b/plugins/inputs/tail/tail_test.go index f9acdbcdba6d4..99090f70d67a8 100644 --- a/plugins/inputs/tail/tail_test.go +++ b/plugins/inputs/tail/tail_test.go @@ -44,6 +44,7 @@ func NewTestTail() *Tail { MaxUndeliveredLines: 1000, offsets: offsetsCopy, WatchMethod: watchMethod, + PathTag: "path", } } @@ -357,6 +358,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) { plugin.Log = testutil.Logger{} plugin.FromBeginning = true plugin.Files = []string{tmpfile.Name()} + plugin.PathTag = "customPathTagMyFile" plugin.SetParserFunc(func() (parsers.Parser, error) { return json.New( &json.Config{ @@ -379,7 +381,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) { expected := []telegraf.Metric{ testutil.MustMetric("cpu", map[string]string{ - "path": tmpfile.Name(), + "customPathTagMyFile": tmpfile.Name(), }, map[string]interface{}{ "time_idle": 42.0, @@ -387,7 +389,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) { time.Unix(0, 0)), testutil.MustMetric("cpu", map[string]string{ - "path": tmpfile.Name(), + "customPathTagMyFile": tmpfile.Name(), }, map[string]interface{}{ "time_idle": 42.0,