Skip to content

Commit

Permalink
Add configurable option for the 'path' tag override in the Tail plugi…
Browse files Browse the repository at this point in the history
…n. (influxdata#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
  • Loading branch information
ivorybilled authored Mar 31, 2021
1 parent 071fef7 commit 78d67ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions plugins/inputs/tail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 9 additions & 2 deletions plugins/inputs/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,6 +71,7 @@ func NewTail() *Tail {
FromBeginning: false,
MaxUndeliveredLines: 1000,
offsets: offsetsCopy,
PathTag: "path",
}
}

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions plugins/inputs/tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewTestTail() *Tail {
MaxUndeliveredLines: 1000,
offsets: offsetsCopy,
WatchMethod: watchMethod,
PathTag: "path",
}
}

Expand Down Expand Up @@ -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{
Expand All @@ -379,15 +381,15 @@ 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,
},
time.Unix(0, 0)),
testutil.MustMetric("cpu",
map[string]string{
"path": tmpfile.Name(),
"customPathTagMyFile": tmpfile.Name(),
},
map[string]interface{}{
"time_idle": 42.0,
Expand Down

0 comments on commit 78d67ba

Please sign in to comment.