Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/filestats] fix file.path value #31753

Merged
merged 1 commit into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions .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: []
14 changes: 12 additions & 2 deletions receiver/filestatsreceiver/integration_test.go
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down
8 changes: 1 addition & 7 deletions receiver/filestatsreceiver/scraper.go
Expand Up @@ -6,7 +6,6 @@ package filestatsreceiver // import "github.com/open-telemetry/opentelemetry-col
import (
"context"
"os"
"path/filepath"
"time"

"github.com/bmatcuk/doublestar/v4"
Expand Down Expand Up @@ -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()))
}

Expand Down
68 changes: 10 additions & 58 deletions receiver/filestatsreceiver/testdata/integration/expected.yaml
Expand Up @@ -3,49 +3,25 @@ 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.
name: file.mtime
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
Expand All @@ -57,49 +33,25 @@ 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.
name: file.mtime
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
Expand Down