feat(inputs.file): Add tag with absolute path of file (#15330)
This commit is contained in:
parent
f3357f369f
commit
e389b7b9c4
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"]))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 = ""
|
||||
|
|
|
|||
Loading…
Reference in New Issue