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:
parent
071fef78ef
commit
78d67ba87b
|
|
@ -64,6 +64,9 @@ The plugin expects messages in one of the
|
||||||
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
||||||
data_format = "influx"
|
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
|
## multiline parser/codec
|
||||||
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
|
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
|
||||||
#[inputs.tail.multiline]
|
#[inputs.tail.multiline]
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ type Tail struct {
|
||||||
WatchMethod string `toml:"watch_method"`
|
WatchMethod string `toml:"watch_method"`
|
||||||
MaxUndeliveredLines int `toml:"max_undelivered_lines"`
|
MaxUndeliveredLines int `toml:"max_undelivered_lines"`
|
||||||
CharacterEncoding string `toml:"character_encoding"`
|
CharacterEncoding string `toml:"character_encoding"`
|
||||||
|
PathTag string `toml:"path_tag"`
|
||||||
|
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
tailers map[string]*tail.Tail
|
tailers map[string]*tail.Tail
|
||||||
|
|
@ -70,6 +71,7 @@ func NewTail() *Tail {
|
||||||
FromBeginning: false,
|
FromBeginning: false,
|
||||||
MaxUndeliveredLines: 1000,
|
MaxUndeliveredLines: 1000,
|
||||||
offsets: offsetsCopy,
|
offsets: offsetsCopy,
|
||||||
|
PathTag: "path",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,6 +117,9 @@ const sampleConfig = `
|
||||||
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
||||||
data_format = "influx"
|
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
|
## multiline parser/codec
|
||||||
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
|
## https://www.elastic.co/guide/en/logstash/2.4/plugins-filters-multiline.html
|
||||||
#[inputs.tail.multiline]
|
#[inputs.tail.multiline]
|
||||||
|
|
@ -380,8 +385,10 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
|
||||||
}
|
}
|
||||||
firstLine = false
|
firstLine = false
|
||||||
|
|
||||||
|
if t.PathTag != "" {
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
metric.AddTag("path", tailer.Filename)
|
metric.AddTag(t.PathTag, tailer.Filename)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try writing out metric first without blocking
|
// try writing out metric first without blocking
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ func NewTestTail() *Tail {
|
||||||
MaxUndeliveredLines: 1000,
|
MaxUndeliveredLines: 1000,
|
||||||
offsets: offsetsCopy,
|
offsets: offsetsCopy,
|
||||||
WatchMethod: watchMethod,
|
WatchMethod: watchMethod,
|
||||||
|
PathTag: "path",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -357,6 +358,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
|
||||||
plugin.Log = testutil.Logger{}
|
plugin.Log = testutil.Logger{}
|
||||||
plugin.FromBeginning = true
|
plugin.FromBeginning = true
|
||||||
plugin.Files = []string{tmpfile.Name()}
|
plugin.Files = []string{tmpfile.Name()}
|
||||||
|
plugin.PathTag = "customPathTagMyFile"
|
||||||
plugin.SetParserFunc(func() (parsers.Parser, error) {
|
plugin.SetParserFunc(func() (parsers.Parser, error) {
|
||||||
return json.New(
|
return json.New(
|
||||||
&json.Config{
|
&json.Config{
|
||||||
|
|
@ -379,7 +381,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
|
||||||
expected := []telegraf.Metric{
|
expected := []telegraf.Metric{
|
||||||
testutil.MustMetric("cpu",
|
testutil.MustMetric("cpu",
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"path": tmpfile.Name(),
|
"customPathTagMyFile": tmpfile.Name(),
|
||||||
},
|
},
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"time_idle": 42.0,
|
"time_idle": 42.0,
|
||||||
|
|
@ -387,7 +389,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
|
||||||
time.Unix(0, 0)),
|
time.Unix(0, 0)),
|
||||||
testutil.MustMetric("cpu",
|
testutil.MustMetric("cpu",
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"path": tmpfile.Name(),
|
"customPathTagMyFile": tmpfile.Name(),
|
||||||
},
|
},
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"time_idle": 42.0,
|
"time_idle": 42.0,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue