Skip to content

Commit

Permalink
[receiver/hostmetrics] Make HOST_PROC_MOUNTINFO part of envMap (#35504)
Browse files Browse the repository at this point in the history
**Description:** 
Make HOST_PROC_MOUNTINFO part of envMap instead of environment variable
  • Loading branch information
atoulme authored Oct 10, 2024
1 parent 9dbe5e0 commit f4a4206
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
27 changes: 27 additions & 0 deletions .chloggen/use_HOST_PROC_MOUNTINFO.yaml
Original file line number Diff line number Diff line change
@@ -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: []
13 changes: 7 additions & 6 deletions receiver/hostmetricsreceiver/hostmetrics_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions receiver/hostmetricsreceiver/hostmetrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -111,7 +110,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))
Expand Down Expand Up @@ -178,9 +177,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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -387,9 +388,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 != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f4a4206

Please sign in to comment.