diff --git a/.chloggen/use_HOST_PROC_MOUNTINFO.yaml b/.chloggen/use_HOST_PROC_MOUNTINFO.yaml new file mode 100644 index 000000000000..06bd4b785e42 --- /dev/null +++ b/.chloggen/use_HOST_PROC_MOUNTINFO.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: enhancement + +# 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: Use HOST_PROC_MOUNTINFO as part of configuration instead of environment variable + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35504] + +# (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/hostmetrics_linux.go b/receiver/hostmetricsreceiver/hostmetrics_linux.go index 43b83fcea692..132eb3978d08 100644 --- a/receiver/hostmetricsreceiver/hostmetrics_linux.go +++ b/receiver/hostmetricsreceiver/hostmetrics_linux.go @@ -14,12 +14,13 @@ import ( ) var gopsutilEnvVars = map[common.EnvKeyType]string{ - common.HostProcEnvKey: "/proc", - common.HostSysEnvKey: "/sys", - common.HostEtcEnvKey: "/etc", - common.HostVarEnvKey: "/var", - common.HostRunEnvKey: "/run", - common.HostDevEnvKey: "/dev", + common.HostProcEnvKey: "/proc", + common.HostSysEnvKey: "/sys", + common.HostEtcEnvKey: "/etc", + common.HostVarEnvKey: "/var", + common.HostRunEnvKey: "/run", + common.HostDevEnvKey: "/dev", + common.HostProcMountinfo: "", } // This exists to validate that different instances of the hostmetricsreceiver do not diff --git a/receiver/hostmetricsreceiver/hostmetrics_linux_test.go b/receiver/hostmetricsreceiver/hostmetrics_linux_test.go index 3876c4444cda..d6c3cd7f1ad3 100644 --- a/receiver/hostmetricsreceiver/hostmetrics_linux_test.go +++ b/receiver/hostmetricsreceiver/hostmetrics_linux_test.go @@ -50,11 +50,12 @@ func TestLoadConfigRootPath(t *testing.T) { cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig() cpuScraperCfg.SetRootPath("testdata") cpuScraperCfg.SetEnvMap(common.EnvMap{ - common.HostDevEnvKey: "testdata/dev", - common.HostEtcEnvKey: "testdata/etc", - common.HostRunEnvKey: "testdata/run", - common.HostSysEnvKey: "testdata/sys", - common.HostVarEnvKey: "testdata/var", + common.HostDevEnvKey: "testdata/dev", + common.HostEtcEnvKey: "testdata/etc", + common.HostProcMountinfo: "testdata", + common.HostRunEnvKey: "testdata/run", + common.HostSysEnvKey: "testdata/sys", + common.HostVarEnvKey: "testdata/var", }) expectedConfig.Scrapers = map[string]internal.Config{cpuscraper.TypeStr: cpuScraperCfg} diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index 809d44c8f717..25dedf0e48eb 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -6,7 +6,6 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col import ( "context" "fmt" - "os" "path/filepath" "strings" "time" @@ -95,7 +94,7 @@ func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) { if !s.fsFilter.includePartition(partition) { continue } - translatedMountpoint := translateMountpoint(s.config.RootPath, partition.Mountpoint) + translatedMountpoint := translateMountpoint(ctx, s.config.RootPath, partition.Mountpoint) usage, usageErr := s.usage(ctx, translatedMountpoint) if usageErr != nil { errors.AddPartial(0, fmt.Errorf("failed to read usage at %s: %w", translatedMountpoint, usageErr)) @@ -162,9 +161,12 @@ 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 +func translateMountpoint(ctx context.Context, rootPath string, mountpoint string) string { + if env, ok := ctx.Value(common.EnvKey).(common.EnvMap); ok { + mountInfo := env[common.EnvKeyType("HOST_PROC_MOUNTINFO")] + if 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 d438357c58e0..946c3e10c9ca 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go @@ -11,6 +11,7 @@ import ( "runtime" "testing" + "github.com/shirou/gopsutil/v4/common" "github.com/shirou/gopsutil/v4/disk" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -30,7 +31,7 @@ func TestScrape(t *testing.T) { name string config Config rootPath string - osEnv map[string]string + osEnv map[common.EnvKeyType]string bootTimeFunc func(context.Context) (uint64, error) partitionsFunc func(context.Context, bool) ([]disk.PartitionStat, error) usageFunc func(context.Context, string) (*disk.UsageStat, error) @@ -198,8 +199,8 @@ 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", + osEnv: map[common.EnvKeyType]string{ + common.HostProcMountinfo: "/proc/1/self", }, config: Config{ MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), @@ -352,9 +353,11 @@ func TestScrape(t *testing.T) { for _, test := range testCases { test := test t.Run(test.name, func(t *testing.T) { + envMap := common.EnvMap{} for k, v := range test.osEnv { - t.Setenv(k, v) + envMap[k] = v } + test.config.EnvMap = envMap test.config.SetRootPath(test.rootPath) scraper, err := newFileSystemScraper(context.Background(), receivertest.NewNopSettings(), &test.config) if test.newErrRegex != "" { diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go index 4d983b8a664e..c937b58558d7 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/shirou/gopsutil/v4/common" "github.com/shirou/gopsutil/v4/cpu" "github.com/shirou/gopsutil/v4/process" "github.com/stretchr/testify/assert" @@ -88,7 +89,17 @@ func TestScrape(t *testing.T) { if test.mutateMetricsConfig != nil { test.mutateMetricsConfig(t, &metricsBuilderConfig.Metrics) } - scraper, err := newProcessScraper(receivertest.NewNopSettings(), &Config{MetricsBuilderConfig: metricsBuilderConfig}) + cfg := &Config{MetricsBuilderConfig: metricsBuilderConfig} + cfg.EnvMap = common.EnvMap{ + common.HostProcEnvKey: "/proc", + common.HostSysEnvKey: "/sys", + common.HostEtcEnvKey: "/etc", + common.HostVarEnvKey: "/var", + common.HostRunEnvKey: "/run", + common.HostDevEnvKey: "/dev", + common.HostProcMountinfo: "", + } + scraper, err := newProcessScraper(receivertest.NewNopSettings(), cfg) if test.mutateScraper != nil { test.mutateScraper(scraper) }