Skip to content

Commit

Permalink
feat(inputs.file): Add tag with absolute path of file
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed May 9, 2024
1 parent 43c8db9 commit a33f8ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
13 changes: 9 additions & 4 deletions plugins/inputs/file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## 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. Cautious when file name variation is high, this can increase the cardinality
## significantly. Read more about cardinality here:
## Please use caution when using the following options: when file name
## variation is high, this can increase the cardinality significantly. Read
## more about cardinality here:
## https://docs.influxdata.com/influxdb/cloud/reference/glossary/#series-cardinality

## Name of tag to store the name of the file. Disabled if not set.
# file_tag = ""

## Name of tag to store the absolute path and name of the file. Disabled if
## not set.
# file_path_tag = ""
```

## Metrics
Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var sampleConfig string
type File struct {
Files []string `toml:"files"`
FileTag string `toml:"file_tag"`
FilePathTag string `toml:"file_path_tag"`
CharacterEncoding string `toml:"character_encoding"`

parserFunc telegraf.ParserFunc
Expand Down Expand Up @@ -54,6 +55,11 @@ func (f *File) Gather(acc telegraf.Accumulator) error {
if f.FileTag != "" {
m.AddTag(f.FileTag, filepath.Base(k))
}
if f.FilePathTag != "" {
if absPath, err := filepath.Abs(k); err == nil {
m.AddTag(f.FilePathTag, absPath)
}
}
acc.AddMetric(m)
}
}
Expand Down
14 changes: 8 additions & 6 deletions plugins/inputs/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func TestFileTag(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
r := File{
Files: []string{filepath.Join(wd, "dev", "testfiles", "json_a.log")},
FileTag: "filename",
Files: []string{filepath.Join(wd, "dev", "testfiles", "json_a.log")},
FileTag: "filename",
FilePathTag: "filepath",
}
require.NoError(t, r.Init())

Expand All @@ -56,10 +57,11 @@ func TestFileTag(t *testing.T) {
require.NoError(t, r.Gather(&acc))

for _, m := range acc.Metrics {
for key, value := range m.Tags {
require.Equal(t, r.FileTag, key)
require.Equal(t, filepath.Base(r.Files[0]), value)
}
require.Contains(t, m.Tags, "filename")
require.Equal(t, filepath.Base(r.Files[0]), m.Tags["filename"])

require.Contains(t, m.Tags, "filepath")
require.True(t, filepath.IsAbs(m.Tags["filepath"]))
}
}

Expand Down
13 changes: 9 additions & 4 deletions plugins/inputs/file/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
## 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. Cautious when file name variation is high, this can increase the cardinality
## significantly. Read more about cardinality here:
## Please use caution when using the following options: when file name
## variation is high, this can increase the cardinality significantly. Read
## more about cardinality here:
## https://docs.influxdata.com/influxdb/cloud/reference/glossary/#series-cardinality

## Name of tag to store the name of the file. Disabled if not set.
# file_tag = ""

## Name of tag to store the absolute path and name of the file. Disabled if
## not set.
# file_path_tag = ""

0 comments on commit a33f8ee

Please sign in to comment.