Skip to content

Commit

Permalink
[receiver/hostmetrics] respect env var use in filesystem scraper (#35045
Browse files Browse the repository at this point in the history
)

**Description:** 
In filesystem scraper, do not prefix partitions when using the
environment variable HOST_PROC_MOUNTINFO

**Link to tracking Issue:**
Fixes #35043
  • Loading branch information
atoulme authored Sep 10, 2024
1 parent 0678231 commit 44bc868
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/fix_envvar_filesystem.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: 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: []
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package filesystemscraper // import "github.com/open-telemetry/opentelemetry-col
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 44bc868

Please sign in to comment.