Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package collector

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand All @@ -43,6 +42,9 @@ var mountTimeout = kingpin.Flag("collector.filesystem.mount-timeout",
var statWorkerCount = kingpin.Flag("collector.filesystem.stat-workers",
"how many stat calls to process simultaneously").
Hidden().Default("4").Int()
var procMountsPath = kingpin.Flag("collector.filesystem.proc-mounts-path",
"Override absolute path to mounts file. If set, disables the default /proc/1/mounts fallback logic.").
Default("").String()
var stuckMounts = make(map[string]struct{})
var stuckMountsMtx = &sync.Mutex{}

Expand Down Expand Up @@ -178,8 +180,13 @@ func stuckMountWatcher(mountPoint string, success chan struct{}, logger log.Logg
}

func mountPointDetails(logger log.Logger) ([]filesystemLabels, error) {
file, err := os.Open(procFilePath("1/mounts"))
if errors.Is(err, os.ErrNotExist) {
path := procFilePath("1/mounts")
if *procMountsPath != "" {
path = *procMountsPath
}
Comment on lines +183 to +186
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path := procFilePath("1/mounts")
if *procMountsPath != "" {
path = *procMountsPath
}
path := *procMountsPath
if path == "" {
path = procFilePath("1/mounts")
}
  1. Isn't it better to prepare a list of paths to try to open like:
    paths := []string{'/proc/1/mounts', '/proc/mounts'}
    if *procMountsPath != "" {
        slices.Insert(paths, 0, *procMountsPath)
    }
    <iterate over paths and try to os.Open()>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 1: will consider, neet to test
For 2: that was my initial reaction, but it wasn't the intention of the request, the path, according to @nmarukovich, has to be defined by the user.


file, err := os.Open(path)
if err != nil && *procMountsPath == "" {
// Fallback to `/proc/mounts` if `/proc/1/mounts` is missing due hidepid.
level.Debug(logger).Log("msg", "Reading root mounts failed, falling back to system mounts", "err", err)
file, err = os.Open(procFilePath("mounts"))
Expand Down
40 changes: 40 additions & 0 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,43 @@ func TestPathRootfs(t *testing.T) {
}
}
}

func TestProcMountsPathOverride(t *testing.T) {
// Save and restore flag values so changes don't leak into other tests.
priorProcMountsPath := *procMountsPath
priorProcPath := *procPath
t.Cleanup(func() {
_, _ = kingpin.CommandLine.Parse([]string{
"--path.procfs", priorProcPath,
"--collector.filesystem.proc-mounts-path", priorProcMountsPath,
})
})

// --path.procfs points to fixtures with 25+ mount points,
// but --collector.filesystem.proc-mounts-path overrides to a file with only 1 mount.
_, err := kingpin.CommandLine.Parse([]string{
"--path.procfs", "./fixtures/proc",
"--collector.filesystem.proc-mounts-path", "./fixtures_hidepid/proc/mounts",
})
if err != nil {
t.Fatal(err)
}

expected := map[string]string{
"/": "",
}

filesystems, err := mountPointDetails(log.NewNopLogger())
if err != nil {
t.Fatal(err)
}

if len(filesystems) != len(expected) {
t.Errorf("expected %d mounts, got %d", len(expected), len(filesystems))
}
for _, fs := range filesystems {
if _, ok := expected[fs.mountPoint]; !ok {
t.Errorf("Got unexpected %s", fs.mountPoint)
}
}
}
Loading