Skip to content

Commit 281da17

Browse files
authored
[receiver/filestats] fix file.path value (#31753)
**Description:** Fix absolute path of file path **Link to tracking Issue:** Fixes #31738
1 parent 9e68729 commit 281da17

File tree

4 files changed

+50
-67
lines changed

4 files changed

+50
-67
lines changed

.chloggen/filestats_filepath.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: filestatsreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix file.path to return the proper absolute path of the file
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31738]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/filestatsreceiver/integration_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
package filestatsreceiver
77

88
import (
9+
"os"
910
"path/filepath"
11+
"strings"
1012
"testing"
1113
"time"
1214

15+
"github.com/stretchr/testify/require"
1316
"go.opentelemetry.io/collector/component"
1417

1518
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
@@ -18,17 +21,24 @@ import (
1821

1922
func Test_Integration(t *testing.T) {
2023
expectedFile := filepath.Join("testdata", "integration", "expected.yaml")
21-
24+
tempDir := t.TempDir()
25+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, "foo.txt"), []byte("foo"), 0600))
26+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, "bar.txt"), []byte("bar"), 0600))
27+
require.NoError(t, os.WriteFile(filepath.Join(tempDir, "foobar.txt"), []byte("foobar"), 0600))
2228
scraperinttest.NewIntegrationTest(
2329
NewFactory(),
2430
scraperinttest.WithCustomConfig(
2531
func(_ *testing.T, cfg component.Config, _ *scraperinttest.ContainerInfo) {
2632
rCfg := cfg.(*Config)
2733
rCfg.CollectionInterval = time.Second
28-
rCfg.Include = "filestats_*"
34+
rCfg.Include = filepath.Join(tempDir, "foo*")
35+
rCfg.ResourceAttributes.FilePath.Enabled = true
2936
}),
3037
scraperinttest.WithExpectedFile(expectedFile),
3138
scraperinttest.WithCompareOptions(
39+
pmetrictest.ChangeResourceAttributeValue("file.path", func(s string) string {
40+
return strings.TrimPrefix(s, tempDir)
41+
}),
3242
pmetrictest.IgnoreResourceMetricsOrder(),
3343
pmetrictest.IgnoreMetricValues(),
3444
pmetrictest.IgnoreMetricDataPointsOrder(),

receiver/filestatsreceiver/scraper.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package filestatsreceiver // import "github.com/open-telemetry/opentelemetry-col
66
import (
77
"context"
88
"os"
9-
"path/filepath"
109
"time"
1110

1211
"github.com/bmatcuk/doublestar/v4"
@@ -42,18 +41,13 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {
4241
scrapeErrors = append(scrapeErrors, err)
4342
continue
4443
}
45-
path, err := filepath.Abs(fileinfo.Name())
46-
if err != nil {
47-
scrapeErrors = append(scrapeErrors, err)
48-
continue
49-
}
5044
s.mb.RecordFileSizeDataPoint(now, fileinfo.Size())
5145
s.mb.RecordFileMtimeDataPoint(now, fileinfo.ModTime().Unix())
5246
collectStats(now, fileinfo, s.mb, s.logger)
5347

5448
rb := s.mb.NewResourceBuilder()
5549
rb.SetFileName(fileinfo.Name())
56-
rb.SetFilePath(path)
50+
rb.SetFilePath(match)
5751
s.mb.EmitForResource(metadata.WithResource(rb.Emit()))
5852
}
5953

receiver/filestatsreceiver/testdata/integration/expected.yaml

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,25 @@ resourceMetrics:
33
attributes:
44
- key: file.name
55
value:
6-
stringValue: filestats_darwin.go
7-
scopeMetrics:
8-
- metrics:
9-
- description: Elapsed time since the last modification of the file or folder, in seconds since Epoch.
10-
name: file.mtime
11-
sum:
12-
aggregationTemporality: 2
13-
dataPoints:
14-
- asInt: "1703319421"
15-
startTimeUnixNano: "1000000"
16-
timeUnixNano: "2000000"
17-
unit: s
18-
- description: The size of the file or folder, in bytes.
19-
gauge:
20-
dataPoints:
21-
- asInt: "882"
22-
startTimeUnixNano: "1000000"
23-
timeUnixNano: "2000000"
24-
name: file.size
25-
unit: b
26-
scope:
27-
name: otelcol/filestatsreceiver
28-
version: latest
29-
- resource:
30-
attributes:
31-
- key: file.name
6+
stringValue: foo.txt
7+
- key: file.path
328
value:
33-
stringValue: filestats_linux.go
9+
stringValue: /foo.txt
3410
scopeMetrics:
3511
- metrics:
3612
- description: Elapsed time since the last modification of the file or folder, in seconds since Epoch.
3713
name: file.mtime
3814
sum:
3915
aggregationTemporality: 2
4016
dataPoints:
41-
- asInt: "1703319421"
17+
- asInt: "1710390874"
4218
startTimeUnixNano: "1000000"
4319
timeUnixNano: "2000000"
4420
unit: s
4521
- description: The size of the file or folder, in bytes.
4622
gauge:
4723
dataPoints:
48-
- asInt: "845"
24+
- asInt: "3"
4925
startTimeUnixNano: "1000000"
5026
timeUnixNano: "2000000"
5127
name: file.size
@@ -57,49 +33,25 @@ resourceMetrics:
5733
attributes:
5834
- key: file.name
5935
value:
60-
stringValue: filestats_other.go
61-
scopeMetrics:
62-
- metrics:
63-
- description: Elapsed time since the last modification of the file or folder, in seconds since Epoch.
64-
name: file.mtime
65-
sum:
66-
aggregationTemporality: 2
67-
dataPoints:
68-
- asInt: "1703319421"
69-
startTimeUnixNano: "1000000"
70-
timeUnixNano: "2000000"
71-
unit: s
72-
- description: The size of the file or folder, in bytes.
73-
gauge:
74-
dataPoints:
75-
- asInt: "776"
76-
startTimeUnixNano: "1000000"
77-
timeUnixNano: "2000000"
78-
name: file.size
79-
unit: b
80-
scope:
81-
name: otelcol/filestatsreceiver
82-
version: latest
83-
- resource:
84-
attributes:
85-
- key: file.name
36+
stringValue: foobar.txt
37+
- key: file.path
8638
value:
87-
stringValue: filestats_windows.go
39+
stringValue: /foobar.txt
8840
scopeMetrics:
8941
- metrics:
9042
- description: Elapsed time since the last modification of the file or folder, in seconds since Epoch.
9143
name: file.mtime
9244
sum:
9345
aggregationTemporality: 2
9446
dataPoints:
95-
- asInt: "1703319421"
47+
- asInt: "1710390874"
9648
startTimeUnixNano: "1000000"
9749
timeUnixNano: "2000000"
9850
unit: s
9951
- description: The size of the file or folder, in bytes.
10052
gauge:
10153
dataPoints:
102-
- asInt: "901"
54+
- asInt: "6"
10355
startTimeUnixNano: "1000000"
10456
timeUnixNano: "2000000"
10557
name: file.size

0 commit comments

Comments
 (0)