Skip to content

Commit 78d67ba

Browse files
authored
Add configurable option for the 'path' tag override in the Tail plugin. (#9069)
* Add configurable option for the 'path' tag override in the Tail plugin. * get test cases to pass * update default config * convert to configurable string field
1 parent 071fef7 commit 78d67ba

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

plugins/inputs/tail/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ The plugin expects messages in one of the
6464
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
6565
data_format = "influx"
6666

67+
## 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.
68+
# path_tag = "path"
69+
6770
## multiline parser/codec
6871
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
6972
#[inputs.tail.multiline]

plugins/inputs/tail/tail.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Tail struct {
4040
WatchMethod string `toml:"watch_method"`
4141
MaxUndeliveredLines int `toml:"max_undelivered_lines"`
4242
CharacterEncoding string `toml:"character_encoding"`
43+
PathTag string `toml:"path_tag"`
4344

4445
Log telegraf.Logger `toml:"-"`
4546
tailers map[string]*tail.Tail
@@ -70,6 +71,7 @@ func NewTail() *Tail {
7071
FromBeginning: false,
7172
MaxUndeliveredLines: 1000,
7273
offsets: offsetsCopy,
74+
PathTag: "path",
7375
}
7476
}
7577

@@ -115,6 +117,9 @@ const sampleConfig = `
115117
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
116118
data_format = "influx"
117119
120+
## 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.
121+
# path_tag = "path"
122+
118123
## multiline parser/codec
119124
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
120125
#[inputs.tail.multiline]
@@ -380,8 +385,10 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
380385
}
381386
firstLine = false
382387

383-
for _, metric := range metrics {
384-
metric.AddTag("path", tailer.Filename)
388+
if t.PathTag != "" {
389+
for _, metric := range metrics {
390+
metric.AddTag(t.PathTag, tailer.Filename)
391+
}
385392
}
386393

387394
// try writing out metric first without blocking

plugins/inputs/tail/tail_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func NewTestTail() *Tail {
4444
MaxUndeliveredLines: 1000,
4545
offsets: offsetsCopy,
4646
WatchMethod: watchMethod,
47+
PathTag: "path",
4748
}
4849
}
4950

@@ -357,6 +358,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
357358
plugin.Log = testutil.Logger{}
358359
plugin.FromBeginning = true
359360
plugin.Files = []string{tmpfile.Name()}
361+
plugin.PathTag = "customPathTagMyFile"
360362
plugin.SetParserFunc(func() (parsers.Parser, error) {
361363
return json.New(
362364
&json.Config{
@@ -379,15 +381,15 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
379381
expected := []telegraf.Metric{
380382
testutil.MustMetric("cpu",
381383
map[string]string{
382-
"path": tmpfile.Name(),
384+
"customPathTagMyFile": tmpfile.Name(),
383385
},
384386
map[string]interface{}{
385387
"time_idle": 42.0,
386388
},
387389
time.Unix(0, 0)),
388390
testutil.MustMetric("cpu",
389391
map[string]string{
390-
"path": tmpfile.Name(),
392+
"customPathTagMyFile": tmpfile.Name(),
391393
},
392394
map[string]interface{}{
393395
"time_idle": 42.0,

0 commit comments

Comments
 (0)