Skip to content

Commit

Permalink
properly strip path.rootfs from mountpoint labels
Browse files Browse the repository at this point in the history
Change-type: patch
Connects-to: prometheus#1418
Signed-off-by: dt-rush <[email protected]>
  • Loading branch information
dt-rush committed Jul 18, 2019
1 parent 0de95ef commit 471b2da
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [ENHANCEMENT]
* [BUGFIX] Renamed label `state` to `name` on `node_systemd_service_restart_total`. #1393
* [BUGFIX] Fix netdev nil reference on Darwin #1414
* [BUGFIX] Strip path.rootfs from mountpoint labels #1421

## 0.18.1 / 2019-06-04

Expand Down
2 changes: 1 addition & 1 deletion collector/filesystem_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
stats = append(stats, filesystemStats{
labels: filesystemLabels{
device: device,
mountPoint: mountpoint,
mountPoint: rootfsStripPrefix(mountpoint),
fsType: fstype,
},
size: float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize),
Expand Down
2 changes: 1 addition & 1 deletion collector/filesystem_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
stats = append(stats, filesystemStats{
labels: filesystemLabels{
device: device,
mountPoint: mountpoint,
mountPoint: rootfsStripPrefix(mountpoint),
fsType: fstype,
},
size: float64(fs.Blocks) * float64(fs.Bsize),
Expand Down
2 changes: 1 addition & 1 deletion collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {

filesystems = append(filesystems, filesystemLabels{
device: parts[0],
mountPoint: parts[1],
mountPoint: rootfsStripPrefix(parts[1]),
fsType: parts[2],
options: parts[3],
})
Expand Down
27 changes: 27 additions & 0 deletions collector/filesystem_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,30 @@ func TestMountsFallback(t *testing.T) {
}
}
}

func TestPathRootfs(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil {
t.Fatal(err)
}

expected := map[string]string{
// should modify these mountpoints (removes /host, see fixture proc file)
"/media/volume1": "",
"/media/volume2": "",
// should not modify these mountpoints
"/dev/shm": "",
"/run/lock": "",
"/sys/fs/cgroup": "",
}

filesystems, err := mountPointDetails()
if err != nil {
t.Log(err)
}

for _, fs := range filesystems {
if _, ok := expected[fs.mountPoint]; !ok {
t.Errorf("Got unexpected %s", fs.mountPoint)
}
}
}
5 changes: 5 additions & 0 deletions collector/fixtures_bindmount/proc/mounts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0
/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
8 changes: 8 additions & 0 deletions collector/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package collector

import (
"path/filepath"
"strings"

"github.com/prometheus/procfs"
kingpin "gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -38,3 +39,10 @@ func sysFilePath(name string) string {
func rootfsFilePath(name string) string {
return filepath.Join(*rootfsPath, name)
}

func rootfsStripPrefix(path string) string {
if *rootfsPath == "/" {
return path
}
return strings.TrimPrefix(path, *rootfsPath)
}

0 comments on commit 471b2da

Please sign in to comment.