From 8cd21ce6a1d91e1bd1589ee6fc77ea0427d6cb8b Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Thu, 5 Sep 2024 23:42:13 -0700 Subject: [PATCH] [receiver/hostmetrics] In filesystem scraper, do not prefix partitions when using the environment variable HOST_PROC_MOUNTINFO --- .chloggen/fix_envvar_filesystem.yaml | 27 ++++++++++++ .../filesystemscraper/filesystem_scraper.go | 4 ++ .../filesystem_scraper_test.go | 42 ++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 .chloggen/fix_envvar_filesystem.yaml diff --git a/.chloggen/fix_envvar_filesystem.yaml b/.chloggen/fix_envvar_filesystem.yaml new file mode 100644 index 000000000000..8740fb531498 --- /dev/null +++ b/.chloggen/fix_envvar_filesystem.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: hostmetricsreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: In filesystem scraper, do not prefix partitions when using the environment variable HOST_PROC_MOUNTINFO + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35043] + +# (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/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index 6f834cb72f3a..809d44c8f717 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -6,6 +6,7 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col import ( "context" "fmt" + "os" "path/filepath" "strings" "time" @@ -162,5 +163,8 @@ func (f *fsFilter) includeMountPoint(mountPoint string) bool { // translateMountsRootPath translates a mountpoint from the host perspective to the chrooted perspective. func translateMountpoint(rootPath, mountpoint string) string { + if mountInfo := os.Getenv("HOST_PROC_MOUNTINFO"); mountInfo != "" { + return mountpoint + } return filepath.Join(rootPath, mountpoint) } diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go index bbb5d03b2fa3..726ec916b081 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go @@ -30,6 +30,7 @@ func TestScrape(t *testing.T) { name string config Config rootPath string + osEnv map[string]string bootTimeFunc func(context.Context) (uint64, error) partitionsFunc func(context.Context, bool) ([]disk.PartitionStat, error) usageFunc func(context.Context, string) (*disk.UsageStat, error) @@ -195,6 +196,43 @@ func TestScrape(t *testing.T) { }, }, }, + { + name: "RootPath at /hostfs but HOST_PROC_MOUNTINFO is set", + osEnv: map[string]string{ + "HOST_PROC_MOUNTINFO": "/proc/1/self", + }, + config: Config{ + MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), + }, + rootPath: filepath.Join("/", "hostfs"), + usageFunc: func(_ context.Context, s string) (*disk.UsageStat, error) { + if s != "mount_point_a" { + return nil, errors.New("mountpoint translated according to RootPath") + } + return &disk.UsageStat{ + Fstype: "fs_type_a", + }, nil + }, + partitionsFunc: func(context.Context, bool) ([]disk.PartitionStat, error) { + return []disk.PartitionStat{ + { + Device: "device_a", + Mountpoint: "mount_point_a", + Fstype: "fs_type_a", + }, + }, nil + }, + expectMetrics: true, + expectedDeviceDataPoints: 1, + expectedDeviceAttributes: []map[string]pcommon.Value{ + { + "device": pcommon.NewValueStr("device_a"), + "mountpoint": pcommon.NewValueStr("mount_point_a"), + "type": pcommon.NewValueStr("fs_type_a"), + "mode": pcommon.NewValueStr("unknown"), + }, + }, + }, { name: "Invalid Include Device Filter", config: Config{ @@ -314,7 +352,9 @@ func TestScrape(t *testing.T) { for _, test := range testCases { test := test t.Run(test.name, func(t *testing.T) { - t.Parallel() + for k, v := range test.osEnv { + t.Setenv(k, v) + } test.config.SetRootPath(test.rootPath) scraper, err := newFileSystemScraper(context.Background(), receivertest.NewNopSettings(), &test.config) if test.newErrRegex != "" {