Add a nameable file tag to file input plugin (#6650)

This commit is contained in:
Nick Neisen 2019-11-13 14:00:41 -07:00 committed by Daniel Nelson
parent 4f4063ba01
commit 20bb673a0e
3 changed files with 44 additions and 2 deletions

View File

@ -7,6 +7,7 @@ Files will always be read in their entirety, if you wish to tail/follow a file
use the [tail input plugin](/plugins/inputs/tail) instead. use the [tail input plugin](/plugins/inputs/tail) instead.
### Configuration: ### Configuration:
```toml ```toml
[[inputs.file]] [[inputs.file]]
## Files to parse each interval. ## Files to parse each interval.
@ -22,4 +23,8 @@ use the [tail input plugin](/plugins/inputs/tail) instead.
## more about them here: ## more about them here:
## 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"
## Name a tag containing the name of the file the data was parsed from. Leave empty
## to disable.
# file_tag = ""
``` ```

View File

@ -3,6 +3,7 @@ package file
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path/filepath"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/internal/globpath"
@ -11,8 +12,9 @@ import (
) )
type File struct { type File struct {
Files []string `toml:"files"` Files []string `toml:"files"`
parser parsers.Parser FileTag string `toml:"file_tag"`
parser parsers.Parser
filenames []string filenames []string
} }
@ -31,6 +33,10 @@ const sampleConfig = `
## more about them here: ## more about them here:
## 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"
## Name a tag containing the name of the file the data was parsed from. Leave empty
## to disable.
# file_tag = ""
` `
// SampleConfig returns the default configuration of the Input // SampleConfig returns the default configuration of the Input
@ -54,6 +60,9 @@ func (f *File) Gather(acc telegraf.Accumulator) error {
} }
for _, m := range metrics { for _, m := range metrics {
if f.FileTag != "" {
m.AddTag(f.FileTag, filepath.Base(k))
}
acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time()) acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
} }
} }

View File

@ -21,6 +21,34 @@ func TestRefreshFilePaths(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, 2, len(r.filenames)) assert.Equal(t, 2, len(r.filenames))
} }
func TestFileTag(t *testing.T) {
acc := testutil.Accumulator{}
wd, err := os.Getwd()
require.NoError(t, err)
r := File{
Files: []string{filepath.Join(wd, "dev/testfiles/json_a.log")},
FileTag: "filename",
}
parserConfig := parsers.Config{
DataFormat: "json",
}
nParser, err := parsers.NewParser(&parserConfig)
assert.NoError(t, err)
r.parser = nParser
err = r.Gather(&acc)
require.NoError(t, err)
for _, m := range acc.Metrics {
for key, value := range m.Tags {
assert.Equal(t, r.FileTag, key)
assert.Equal(t, filepath.Base(r.Files[0]), value)
}
}
}
func TestJSONParserCompile(t *testing.T) { func TestJSONParserCompile(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
wd, _ := os.Getwd() wd, _ := os.Getwd()