Skip to content

Commit

Permalink
Use float64 in the ZFS collector
Browse files Browse the repository at this point in the history
ZFS metrics can also be unsigned 64-bit integers that won't fit in
int64 and causes the whole collector to fail.
  • Loading branch information
fpletz committed Oct 25, 2017
1 parent 715ebd1 commit db2edaa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions collector/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s zfsSysctl) metricName() string {
return strings.Replace(parts[len(parts)-1], "-", "_", -1)
}

func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value int64) prometheus.Metric {
func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value float64) prometheus.Metric {
metricName := sysctl.metricName()

return prometheus.MustNewConstMetric(
Expand All @@ -86,11 +86,11 @@ func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, val
nil,
),
prometheus.UntypedValue,
float64(value),
value,
)
}

func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value int64) prometheus.Metric {
func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value float64) prometheus.Metric {
metricName := sysctl.metricName()

return prometheus.MustNewConstMetric(
Expand All @@ -101,7 +101,7 @@ func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value
nil,
),
prometheus.UntypedValue,
float64(value),
value,
poolName,
)
}
12 changes: 6 additions & 6 deletions collector/zfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *zfsCollector) updateZfsStats(subsystem string, ch chan<- prometheus.Met
}
defer file.Close()

return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v int64) {
return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v float64) {
ch <- c.constSysctlMetric(subsystem, s, v)
})
}
Expand All @@ -64,7 +64,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
return errZFSNotAvailable
}

err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int64) {
err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v float64) {
ch <- c.constPoolMetric(poolName, s, v)
})
file.Close()
Expand All @@ -76,7 +76,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
return nil
}

func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, int64)) error {
func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, float64)) error {
scanner := bufio.NewScanner(reader)

parseLine := false
Expand All @@ -95,7 +95,7 @@ func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler

key := fmt.Sprintf("kstat.zfs.misc.%s.%s", fmtExt, parts[0])

value, err := strconv.ParseInt(parts[2], 10, 64)
value, err := strconv.ParseFloat(parts[2], 64)
if err != nil {
return fmt.Errorf("could not parse expected integer value for %q", key)
}
Expand All @@ -109,7 +109,7 @@ func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler
return scanner.Err()
}

func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, handler func(string, zfsSysctl, int64)) error {
func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, handler func(string, zfsSysctl, float64)) error {
scanner := bufio.NewScanner(reader)

parseLine := false
Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, h
for i, field := range fields {
key := fmt.Sprintf("kstat.zfs.misc.%s.%s", zpoolFile, field)

value, err := strconv.ParseInt(line[i], 10, 64)
value, err := strconv.ParseFloat(line[i], 64)
if err != nil {
return fmt.Errorf("could not parse expected integer value for %q: %v", key, err)
}
Expand Down
32 changes: 16 additions & 16 deletions collector/zfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func TestArcstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.arcstats.hits") {
return
}

handlerCalled = true

if v != int64(8772612) {
if v != float64(8772612) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -68,15 +68,15 @@ func TestZfetchstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.zfetchstats.hits") {
return
}

handlerCalled = true

if v != int64(7067992) {
if v != float64(7067992) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -104,15 +104,15 @@ func TestZilParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.zil.zil_commit_count") {
return
}

handlerCalled = true

if v != int64(10) {
if v != float64(10) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -140,15 +140,15 @@ func TestVdevCacheStatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.vdev_cache_stats.delegations") {
return
}

handlerCalled = true

if v != int64(40) {
if v != float64(40) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -176,15 +176,15 @@ func TestXuioStatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.xuio_stats.onloan_read_buf") {
return
}

handlerCalled = true

if v != int64(32) {
if v != float64(32) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -212,15 +212,15 @@ func TestFmParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.fm.erpt-dropped") {
return
}

handlerCalled = true

if v != int64(18) {
if v != float64(18) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -248,15 +248,15 @@ func TestDmuTxParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v int64) {
err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v float64) {

if s != zfsSysctl("kstat.zfs.misc.dmu_tx.dmu_tx_assigned") {
return
}

handlerCalled = true

if v != int64(3532844) {
if v != float64(3532844) {
t.Fatalf("Incorrect value parsed from procfs data")
}

Expand Down Expand Up @@ -289,14 +289,14 @@ func TestZpoolParsing(t *testing.T) {
t.Fatal(err)
}

err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int64) {
err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v float64) {
if s != zfsSysctl("kstat.zfs.misc.io.nread") {
return
}

handlerCalled = true

if v != int64(1884160) && v != int64(2826240) {
if v != float64(1884160) && v != float64(2826240) {
t.Fatalf("Incorrect value parsed from procfs data %v", v)
}

Expand Down

0 comments on commit db2edaa

Please sign in to comment.