Skip to content

Commit c98bbe7

Browse files
dt-rushoblitorum
authored andcommitted
properly strip path.rootfs from mountpoint labels (prometheus#1421)
Change-type: patch Connects-to: prometheus#1418 Signed-off-by: dt-rush <nickp@balena.io>
1 parent 77fb075 commit c98bbe7

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [ENHANCEMENT] Include additional XFS runtime statistics. #1423
1818
* [BUGFIX] Renamed label `state` to `name` on `node_systemd_service_restart_total`. #1393
1919
* [BUGFIX] Fix netdev nil reference on Darwin #1414
20+
* [BUGFIX] Strip path.rootfs from mountpoint labels #1421
2021

2122
## 0.18.1 / 2019-06-04
2223

collector/filesystem_bsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
6969
stats = append(stats, filesystemStats{
7070
labels: filesystemLabels{
7171
device: device,
72-
mountPoint: mountpoint,
72+
mountPoint: rootfsStripPrefix(mountpoint),
7373
fsType: fstype,
7474
},
7575
size: float64(mnt[i].f_blocks) * float64(mnt[i].f_bsize),

collector/filesystem_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
7373
stats = append(stats, filesystemStats{
7474
labels: filesystemLabels{
7575
device: device,
76-
mountPoint: mountpoint,
76+
mountPoint: rootfsStripPrefix(mountpoint),
7777
fsType: fstype,
7878
},
7979
size: float64(fs.Blocks) * float64(fs.Bsize),

collector/filesystem_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
165165

166166
filesystems = append(filesystems, filesystemLabels{
167167
device: parts[0],
168-
mountPoint: parts[1],
168+
mountPoint: rootfsStripPrefix(parts[1]),
169169
fsType: parts[2],
170170
options: parts[3],
171171
})

collector/filesystem_linux_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,30 @@ func TestMountsFallback(t *testing.T) {
112112
}
113113
}
114114
}
115+
116+
func TestPathRootfs(t *testing.T) {
117+
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil {
118+
t.Fatal(err)
119+
}
120+
121+
expected := map[string]string{
122+
// should modify these mountpoints (removes /host, see fixture proc file)
123+
"/media/volume1": "",
124+
"/media/volume2": "",
125+
// should not modify these mountpoints
126+
"/dev/shm": "",
127+
"/run/lock": "",
128+
"/sys/fs/cgroup": "",
129+
}
130+
131+
filesystems, err := mountPointDetails()
132+
if err != nil {
133+
t.Log(err)
134+
}
135+
136+
for _, fs := range filesystems {
137+
if _, ok := expected[fs.mountPoint]; !ok {
138+
t.Errorf("Got unexpected %s", fs.mountPoint)
139+
}
140+
}
141+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0
2+
/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0
3+
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
4+
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
5+
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0

collector/paths.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package collector
1515

1616
import (
1717
"path/filepath"
18+
"strings"
1819

1920
"github.com/prometheus/procfs"
2021
kingpin "gopkg.in/alecthomas/kingpin.v2"
@@ -38,3 +39,10 @@ func sysFilePath(name string) string {
3839
func rootfsFilePath(name string) string {
3940
return filepath.Join(*rootfsPath, name)
4041
}
42+
43+
func rootfsStripPrefix(path string) string {
44+
if *rootfsPath == "/" {
45+
return path
46+
}
47+
return strings.TrimPrefix(path, *rootfsPath)
48+
}

0 commit comments

Comments
 (0)