feat(inputs.file): Add tag with absolute path of file (#15330)

This commit is contained in:
Joshua Powers 2024-05-10 10:25:51 -06:00 committed by GitHub
parent f3357f369f
commit e389b7b9c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 14 deletions

View File

@ -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

View File

@ -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
@ -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)
}
}

View File

@ -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())
@ -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"]))
}
}

View File

@ -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 = ""