Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a nameable file tag to file input plugin #6650

Merged
merged 2 commits into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = "filename"
```
9 changes: 9 additions & 0 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 @@ -12,6 +13,7 @@ import (

type File struct {
Files []string `toml:"files"`
File string `toml:"file_tag"`
nwneisen marked this conversation as resolved.
Show resolved Hide resolved
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 = "filename"
nwneisen marked this conversation as resolved.
Show resolved Hide resolved
`

// 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.File != "" {
m.AddTag(f.File, 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")},
File: "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.File, key)
assert.Equal(t, filepath.Base(r.Files[0]), value)
}
}
}

func TestJSONParserCompile(t *testing.T) {
var acc testutil.Accumulator
wd, _ := os.Getwd()
Expand Down