From 281da17fc53822d2d68931d28a2173d6963ffae0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 13 Mar 2024 22:39:03 -0700 Subject: [PATCH] [receiver/filestats] fix file.path value (#31753) **Description:** Fix absolute path of file path **Link to tracking Issue:** Fixes #31738 --- .chloggen/filestats_filepath.yaml | 27 ++++++++ .../filestatsreceiver/integration_test.go | 14 +++- receiver/filestatsreceiver/scraper.go | 8 +-- .../testdata/integration/expected.yaml | 68 +++---------------- 4 files changed, 50 insertions(+), 67 deletions(-) create mode 100755 .chloggen/filestats_filepath.yaml diff --git a/.chloggen/filestats_filepath.yaml b/.chloggen/filestats_filepath.yaml new file mode 100755 index 0000000000000..0d9f7cb253091 --- /dev/null +++ b/.chloggen/filestats_filepath.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: filestatsreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix file.path to return the proper absolute path of the file + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31738] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/filestatsreceiver/integration_test.go b/receiver/filestatsreceiver/integration_test.go index 821483afa4b66..a9069dbde5791 100644 --- a/receiver/filestatsreceiver/integration_test.go +++ b/receiver/filestatsreceiver/integration_test.go @@ -6,10 +6,13 @@ package filestatsreceiver import ( + "os" "path/filepath" + "strings" "testing" "time" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest" @@ -18,17 +21,24 @@ import ( func Test_Integration(t *testing.T) { expectedFile := filepath.Join("testdata", "integration", "expected.yaml") - + tempDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("foo"), 0600)) + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "bar.txt"), []byte("bar"), 0600)) + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "foobar.txt"), []byte("foobar"), 0600)) scraperinttest.NewIntegrationTest( NewFactory(), scraperinttest.WithCustomConfig( func(_ *testing.T, cfg component.Config, _ *scraperinttest.ContainerInfo) { rCfg := cfg.(*Config) rCfg.CollectionInterval = time.Second - rCfg.Include = "filestats_*" + rCfg.Include = filepath.Join(tempDir, "foo*") + rCfg.ResourceAttributes.FilePath.Enabled = true }), scraperinttest.WithExpectedFile(expectedFile), scraperinttest.WithCompareOptions( + pmetrictest.ChangeResourceAttributeValue("file.path", func(s string) string { + return strings.TrimPrefix(s, tempDir) + }), pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreMetricValues(), pmetrictest.IgnoreMetricDataPointsOrder(), diff --git a/receiver/filestatsreceiver/scraper.go b/receiver/filestatsreceiver/scraper.go index 5e09b58919478..67807f3f655f4 100644 --- a/receiver/filestatsreceiver/scraper.go +++ b/receiver/filestatsreceiver/scraper.go @@ -6,7 +6,6 @@ package filestatsreceiver // import "github.com/open-telemetry/opentelemetry-col import ( "context" "os" - "path/filepath" "time" "github.com/bmatcuk/doublestar/v4" @@ -42,18 +41,13 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) { scrapeErrors = append(scrapeErrors, err) continue } - path, err := filepath.Abs(fileinfo.Name()) - if err != nil { - scrapeErrors = append(scrapeErrors, err) - continue - } s.mb.RecordFileSizeDataPoint(now, fileinfo.Size()) s.mb.RecordFileMtimeDataPoint(now, fileinfo.ModTime().Unix()) collectStats(now, fileinfo, s.mb, s.logger) rb := s.mb.NewResourceBuilder() rb.SetFileName(fileinfo.Name()) - rb.SetFilePath(path) + rb.SetFilePath(match) s.mb.EmitForResource(metadata.WithResource(rb.Emit())) } diff --git a/receiver/filestatsreceiver/testdata/integration/expected.yaml b/receiver/filestatsreceiver/testdata/integration/expected.yaml index a54b368539a72..5e4e76b92a373 100644 --- a/receiver/filestatsreceiver/testdata/integration/expected.yaml +++ b/receiver/filestatsreceiver/testdata/integration/expected.yaml @@ -3,34 +3,10 @@ resourceMetrics: attributes: - key: file.name value: - stringValue: filestats_darwin.go - scopeMetrics: - - metrics: - - description: Elapsed time since the last modification of the file or folder, in seconds since Epoch. - name: file.mtime - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "1703319421" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - unit: s - - description: The size of the file or folder, in bytes. - gauge: - dataPoints: - - asInt: "882" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - name: file.size - unit: b - scope: - name: otelcol/filestatsreceiver - version: latest - - resource: - attributes: - - key: file.name + stringValue: foo.txt + - key: file.path value: - stringValue: filestats_linux.go + stringValue: /foo.txt scopeMetrics: - metrics: - description: Elapsed time since the last modification of the file or folder, in seconds since Epoch. @@ -38,14 +14,14 @@ resourceMetrics: sum: aggregationTemporality: 2 dataPoints: - - asInt: "1703319421" + - asInt: "1710390874" startTimeUnixNano: "1000000" timeUnixNano: "2000000" unit: s - description: The size of the file or folder, in bytes. gauge: dataPoints: - - asInt: "845" + - asInt: "3" startTimeUnixNano: "1000000" timeUnixNano: "2000000" name: file.size @@ -57,34 +33,10 @@ resourceMetrics: attributes: - key: file.name value: - stringValue: filestats_other.go - scopeMetrics: - - metrics: - - description: Elapsed time since the last modification of the file or folder, in seconds since Epoch. - name: file.mtime - sum: - aggregationTemporality: 2 - dataPoints: - - asInt: "1703319421" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - unit: s - - description: The size of the file or folder, in bytes. - gauge: - dataPoints: - - asInt: "776" - startTimeUnixNano: "1000000" - timeUnixNano: "2000000" - name: file.size - unit: b - scope: - name: otelcol/filestatsreceiver - version: latest - - resource: - attributes: - - key: file.name + stringValue: foobar.txt + - key: file.path value: - stringValue: filestats_windows.go + stringValue: /foobar.txt scopeMetrics: - metrics: - description: Elapsed time since the last modification of the file or folder, in seconds since Epoch. @@ -92,14 +44,14 @@ resourceMetrics: sum: aggregationTemporality: 2 dataPoints: - - asInt: "1703319421" + - asInt: "1710390874" startTimeUnixNano: "1000000" timeUnixNano: "2000000" unit: s - description: The size of the file or folder, in bytes. gauge: dataPoints: - - asInt: "901" + - asInt: "6" startTimeUnixNano: "1000000" timeUnixNano: "2000000" name: file.size