From e389b7b9c49ae7d53f44864655848eb225dd7aa9 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Fri, 10 May 2024 10:25:51 -0600 Subject: [PATCH] feat(inputs.file): Add tag with absolute path of file (#15330) --- plugins/inputs/file/README.md | 13 +++++++++---- plugins/inputs/file/file.go | 6 ++++++ plugins/inputs/file/file_test.go | 14 ++++++++------ plugins/inputs/file/sample.conf | 13 +++++++++---- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/plugins/inputs/file/README.md b/plugins/inputs/file/README.md index f732125aefefe..8870c69d3a21e 100644 --- a/plugins/inputs/file/README.md +++ b/plugins/inputs/file/README.md @@ -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 diff --git a/plugins/inputs/file/file.go b/plugins/inputs/file/file.go index 84d989fb0bd36..a0b13c472e85e 100644 --- a/plugins/inputs/file/file.go +++ b/plugins/inputs/file/file.go @@ -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) } } diff --git a/plugins/inputs/file/file_test.go b/plugins/inputs/file/file_test.go index cd539bd5a5fe0..91e7fc8a8a134 100644 --- a/plugins/inputs/file/file_test.go +++ b/plugins/inputs/file/file_test.go @@ -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"])) } } diff --git a/plugins/inputs/file/sample.conf b/plugins/inputs/file/sample.conf index 706d1bfff6481..9f8788d4b6388 100644 --- a/plugins/inputs/file/sample.conf +++ b/plugins/inputs/file/sample.conf @@ -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 = ""