Skip to content

Commit 38b1383

Browse files
committed
[receiver/hostmetrics] Make HOST_PROC_MOUNTINFO part of the configuration instead of environment variable
1 parent 679374a commit 38b1383

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: hostmetricsreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Use HOST_PROC_MOUNTINFO as part of configuration instead of environment variable
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: [35504]
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/hostmetricsreceiver/hostmetrics_linux.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import (
1414
)
1515

1616
var gopsutilEnvVars = map[common.EnvKeyType]string{
17-
common.HostProcEnvKey: "/proc",
18-
common.HostSysEnvKey: "/sys",
19-
common.HostEtcEnvKey: "/etc",
20-
common.HostVarEnvKey: "/var",
21-
common.HostRunEnvKey: "/run",
22-
common.HostDevEnvKey: "/dev",
17+
common.HostProcEnvKey: "/proc",
18+
common.HostSysEnvKey: "/sys",
19+
common.HostEtcEnvKey: "/etc",
20+
common.HostVarEnvKey: "/var",
21+
common.HostRunEnvKey: "/run",
22+
common.HostDevEnvKey: "/dev",
23+
common.HostProcMountinfo: "",
2324
}
2425

2526
// This exists to validate that different instances of the hostmetricsreceiver do not

receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col
66
import (
77
"context"
88
"fmt"
9-
"os"
109
"path/filepath"
1110
"strings"
1211
"time"
@@ -95,7 +94,7 @@ func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
9594
if !s.fsFilter.includePartition(partition) {
9695
continue
9796
}
98-
translatedMountpoint := translateMountpoint(s.config.RootPath, partition.Mountpoint)
97+
translatedMountpoint := translateMountpoint(ctx, s.config.RootPath, partition.Mountpoint)
9998
usage, usageErr := s.usage(ctx, translatedMountpoint)
10099
if usageErr != nil {
101100
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 {
162161
}
163162

164163
// translateMountsRootPath translates a mountpoint from the host perspective to the chrooted perspective.
165-
func translateMountpoint(rootPath, mountpoint string) string {
166-
if mountInfo := os.Getenv("HOST_PROC_MOUNTINFO"); mountInfo != "" {
167-
return mountpoint
164+
func translateMountpoint(ctx context.Context, rootPath string, mountpoint string) string {
165+
if env, ok := ctx.Value(common.EnvKey).(common.EnvMap); ok {
166+
mountInfo := env[common.EnvKeyType("HOST_PROC_MOUNTINFO")]
167+
if mountInfo != "" {
168+
return mountpoint
169+
}
168170
}
169171
return filepath.Join(rootPath, mountpoint)
170172
}

receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime"
1212
"testing"
1313

14+
"github.com/shirou/gopsutil/v4/common"
1415
"github.com/shirou/gopsutil/v4/disk"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
@@ -30,7 +31,7 @@ func TestScrape(t *testing.T) {
3031
name string
3132
config Config
3233
rootPath string
33-
osEnv map[string]string
34+
osEnv map[common.EnvKeyType]string
3435
bootTimeFunc func(context.Context) (uint64, error)
3536
partitionsFunc func(context.Context, bool) ([]disk.PartitionStat, error)
3637
usageFunc func(context.Context, string) (*disk.UsageStat, error)
@@ -198,8 +199,8 @@ func TestScrape(t *testing.T) {
198199
},
199200
{
200201
name: "RootPath at /hostfs but HOST_PROC_MOUNTINFO is set",
201-
osEnv: map[string]string{
202-
"HOST_PROC_MOUNTINFO": "/proc/1/self",
202+
osEnv: map[common.EnvKeyType]string{
203+
common.HostProcMountinfo: "/proc/1/self",
203204
},
204205
config: Config{
205206
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
@@ -352,9 +353,11 @@ func TestScrape(t *testing.T) {
352353
for _, test := range testCases {
353354
test := test
354355
t.Run(test.name, func(t *testing.T) {
356+
envMap := common.EnvMap{}
355357
for k, v := range test.osEnv {
356-
t.Setenv(k, v)
358+
envMap[k] = v
357359
}
360+
test.config.EnvMap = envMap
358361
test.config.SetRootPath(test.rootPath)
359362
scraper, err := newFileSystemScraper(context.Background(), receivertest.NewNopSettings(), &test.config)
360363
if test.newErrRegex != "" {

receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/shirou/gopsutil/v4/common"
1415
"github.com/shirou/gopsutil/v4/cpu"
1516
"github.com/shirou/gopsutil/v4/process"
1617
"github.com/stretchr/testify/assert"
@@ -88,7 +89,17 @@ func TestScrape(t *testing.T) {
8889
if test.mutateMetricsConfig != nil {
8990
test.mutateMetricsConfig(t, &metricsBuilderConfig.Metrics)
9091
}
91-
scraper, err := newProcessScraper(receivertest.NewNopSettings(), &Config{MetricsBuilderConfig: metricsBuilderConfig})
92+
cfg := &Config{MetricsBuilderConfig: metricsBuilderConfig}
93+
cfg.EnvMap = common.EnvMap{
94+
common.HostProcEnvKey: "/proc",
95+
common.HostSysEnvKey: "/sys",
96+
common.HostEtcEnvKey: "/etc",
97+
common.HostVarEnvKey: "/var",
98+
common.HostRunEnvKey: "/run",
99+
common.HostDevEnvKey: "/dev",
100+
common.HostProcMountinfo: "",
101+
}
102+
scraper, err := newProcessScraper(receivertest.NewNopSettings(), cfg)
92103
if test.mutateScraper != nil {
93104
test.mutateScraper(scraper)
94105
}

0 commit comments

Comments
 (0)