Skip to content

Commit

Permalink
Add a nameable file tag to file input plugin (#6650)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwneisen authored and danielnelson committed Nov 13, 2019
1 parent 4f4063b commit 20bb673
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/file/README.md
Expand Up @@ -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.

### Configuration:

```toml
[[inputs.file]]
## Files to parse each interval.
Expand All @@ -22,4 +23,8 @@ use the [tail input plugin](/plugins/inputs/tail) instead.
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "influx"

## Name a tag containing the name of the file the data was parsed from. Leave empty
## to disable.
# file_tag = ""
```
13 changes: 11 additions & 2 deletions plugins/inputs/file/file.go
Expand Up @@ -3,6 +3,7 @@ package file
import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/globpath"
Expand All @@ -11,8 +12,9 @@ import (
)

type File struct {
Files []string `toml:"files"`
parser parsers.Parser
Files []string `toml:"files"`
FileTag string `toml:"file_tag"`
parser parsers.Parser

filenames []string
}
Expand All @@ -31,6 +33,10 @@ const sampleConfig = `
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
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
Expand All @@ -54,6 +60,9 @@ func (f *File) Gather(acc telegraf.Accumulator) error {
}

for _, m := range metrics {
if f.FileTag != "" {
m.AddTag(f.FileTag, filepath.Base(k))
}
acc.AddFields(m.Name(), m.Fields(), m.Tags(), m.Time())
}
}
Expand Down
28 changes: 28 additions & 0 deletions plugins/inputs/file/file_test.go
Expand Up @@ -21,6 +21,34 @@ func TestRefreshFilePaths(t *testing.T) {
require.NoError(t, err)
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) {
var acc testutil.Accumulator
wd, _ := os.Getwd()
Expand Down

0 comments on commit 20bb673

Please sign in to comment.