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
This commit is contained in:
David Bennett 2021-03-31 12:06:13 -04:00 committed by GitHub
parent 071fef78ef
commit 78d67ba87b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -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]

View File

@ -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

View File

@ -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,