Skip to content

Commit

Permalink
Use float64 instead of uint64 for all Prom metrics
Browse files Browse the repository at this point in the history
For any possible metric value, always assume the value could require being parsed as a floating point value. This has the positive side-effect of eliminating some conversions to float64 across the codebase.

Fixes issue #111.

Signed-Off-By: Joe Handzik <joseph.t.handzik@hpe.com>
  • Loading branch information
Joe Handzik committed Jul 25, 2017
1 parent 3589413 commit 9828402
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions sources/proc_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
numRegexPattern = regexp.MustCompile(`[0-9]*\.[0-9]+|[0-9]+`)
)

type prometheusType func([]string, []string, string, string, uint64) prometheus.Metric
type prometheusType func([]string, []string, string, string, float64) prometheus.Metric

type lustreProcMetric struct {
filename string
Expand All @@ -42,7 +42,7 @@ type lustreProcMetric struct {
type lustreStatsMetric struct {
title string
help string
value uint64
value float64
extraLabel string
extraLabelValue string
}
Expand Down
46 changes: 23 additions & 23 deletions sources/procfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ func (s *lustreProcfsSource) Update(ch chan<- prometheus.Metric) (err error) {
metricType = single
switch metric.filename {
case "health_check":
err = s.parseTextFile(metric.source, "health_check", path, directoryDepth, metric.helpText, metric.promName, func(nodeType string, nodeName string, name string, helpText string, value uint64) {
err = s.parseTextFile(metric.source, "health_check", path, directoryDepth, metric.helpText, metric.promName, func(nodeType string, nodeName string, name string, helpText string, value float64) {
ch <- metric.metricFunc([]string{nodeType}, []string{nodeName}, name, helpText, value)
})
if err != nil {
return err
}
case "brw_stats", "rpc_stats":
err = s.parseBRWStats(metric.source, "stats", path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, brwOperation string, brwSize string, nodeName string, name string, helpText string, value uint64, extraLabel string, extraLabelValue string) {
err = s.parseBRWStats(metric.source, "stats", path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, brwOperation string, brwSize string, nodeName string, name string, helpText string, value float64, extraLabel string, extraLabelValue string) {
if extraLabelValue == "" {
ch <- metric.metricFunc([]string{nodeType, "operation", "size"}, []string{nodeName, brwOperation, brwSize}, name, helpText, value)
} else {
Expand All @@ -381,7 +381,7 @@ func (s *lustreProcfsSource) Update(ch chan<- prometheus.Metric) (err error) {
return err
}
case "job_stats":
err = s.parseJobStats(metric.source, "job_stats", path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, jobid string, nodeName string, name string, helpText string, value uint64, extraLabel string, extraLabelValue string) {
err = s.parseJobStats(metric.source, "job_stats", path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, jobid string, nodeName string, name string, helpText string, value float64, extraLabel string, extraLabelValue string) {
if extraLabelValue == "" {
ch <- metric.metricFunc([]string{nodeType, "jobid"}, []string{nodeName, jobid}, name, helpText, value)
} else {
Expand All @@ -399,7 +399,7 @@ func (s *lustreProcfsSource) Update(ch chan<- prometheus.Metric) (err error) {
} else if metric.filename == encryptPagePools {
metricType = encryptPagePools
}
err = s.parseFile(metric.source, metricType, path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, nodeName string, name string, helpText string, value uint64, extraLabel string, extraLabelValue string) {
err = s.parseFile(metric.source, metricType, path, directoryDepth, metric.helpText, metric.promName, metric.hasMultipleVals, func(nodeType string, nodeName string, name string, helpText string, value float64, extraLabel string, extraLabelValue string) {
if extraLabelValue == "" {
ch <- metric.metricFunc([]string{nodeType}, []string{nodeName}, name, helpText, value)
} else {
Expand Down Expand Up @@ -447,7 +447,7 @@ func getStatsOperationMetrics(statsFile string, promName string, helpText string
continue
}
bytesSplit := r.Split(opStat, -1)
result, err := strconv.ParseUint(bytesSplit[operation.index], 10, 64)
result, err := strconv.ParseFloat(bytesSplit[operation.index], 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -502,7 +502,7 @@ func getStatsIOMetrics(statsFile string, promName string, helpText string) (metr
return nil, err
}
bytesSplit := r.Split(bytesString, -1)
result, err := strconv.ParseUint(bytesSplit[bytesMap[helpText].index], 10, 64)
result, err := strconv.ParseFloat(bytesSplit[bytesMap[helpText].index], 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -593,7 +593,7 @@ func getJobStatsIOMetrics(jobBlock string, jobID string, promName string, helpTe
if len(opNumbers) < 1 {
return nil, nil
}
result, err := strconv.ParseUint(strings.TrimSpace(opNumbers[opMap[helpText].index]), 10, 64)
result, err := strconv.ParseFloat(strings.TrimSpace(opNumbers[opMap[helpText].index]), 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -649,8 +649,8 @@ func getJobStatsOperationMetrics(jobBlock string, jobID string, promName string,
if len(opNumbers) < 1 {
continue
}
var result uint64
result, err = strconv.ParseUint(strings.TrimSpace(opNumbers[operation.index]), 10, 64)
var result float64
result, err = strconv.ParseFloat(strings.TrimSpace(opNumbers[operation.index]), 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -692,7 +692,7 @@ func parseJobStatsText(jobStats string, promName string, helpText string, hasMul
return metricList, nil
}

func (s *lustreProcfsSource) parseJobStats(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, string, uint64, string, string)) (err error) {
func (s *lustreProcfsSource) parseJobStats(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, string, float64, string, string)) (err error) {
_, nodeName, err := parseFileElements(path, directoryDepth)
if err != nil {
return err
Expand All @@ -715,7 +715,7 @@ func (s *lustreProcfsSource) parseJobStats(nodeType string, metricType string, p
return nil
}

func (s *lustreProcfsSource) parseBRWStats(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, string, string, uint64, string, string)) (err error) {
func (s *lustreProcfsSource) parseBRWStats(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, string, string, float64, string, string)) (err error) {
_, nodeName, err := parseFileElements(path, directoryDepth)
if err != nil {
return err
Expand Down Expand Up @@ -748,7 +748,7 @@ func (s *lustreProcfsSource) parseBRWStats(nodeType string, metricType string, p
extraLabelValue = pathElements[len(pathElements)-3]
}
for _, item := range metricList {
value, err := strconv.ParseUint(item.value, 10, 64)
value, err := strconv.ParseFloat(item.value, 64)
if err != nil {
return err
}
Expand All @@ -757,7 +757,7 @@ func (s *lustreProcfsSource) parseBRWStats(nodeType string, metricType string, p
return nil
}

func (s *lustreProcfsSource) parseTextFile(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, handler func(string, string, string, string, uint64)) (err error) {
func (s *lustreProcfsSource) parseTextFile(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, handler func(string, string, string, string, float64)) (err error) {
filename, nodeName, err := parseFileElements(path, directoryDepth)
if err != nil {
return err
Expand All @@ -770,13 +770,13 @@ func (s *lustreProcfsSource) parseTextFile(nodeType string, metricType string, p
switch filename {
case "health_check":
if strings.TrimSpace(fileString) == "healthy" {
value, err := strconv.ParseUint(strings.TrimSpace(healthCheckHealthy), 10, 64)
value, err := strconv.ParseFloat(strings.TrimSpace(healthCheckHealthy), 64)
if err != nil {
return err
}
handler(nodeType, nodeName, promName, helpText, value)
} else {
value, err := strconv.ParseUint(strings.TrimSpace(healthCheckUnhealthy), 10, 64)
value, err := strconv.ParseFloat(strings.TrimSpace(healthCheckUnhealthy), 64)
if err != nil {
return err
}
Expand All @@ -786,7 +786,7 @@ func (s *lustreProcfsSource) parseTextFile(nodeType string, metricType string, p
return nil
}

func (s *lustreProcfsSource) parseFile(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, uint64, string, string)) (err error) {
func (s *lustreProcfsSource) parseFile(nodeType string, metricType string, path string, directoryDepth int, helpText string, promName string, hasMultipleVals bool, handler func(string, string, string, string, float64, string, string)) (err error) {
_, nodeName, err := parseFileElements(path, directoryDepth)
if err != nil {
return err
Expand All @@ -797,7 +797,7 @@ func (s *lustreProcfsSource) parseFile(nodeType string, metricType string, path
if err != nil {
return err
}
convertedValue, err := strconv.ParseUint(strings.TrimSpace(string(value)), 10, 64)
convertedValue, err := strconv.ParseFloat(strings.TrimSpace(string(value)), 64)
if err != nil {
return err
}
Expand All @@ -815,7 +815,7 @@ func (s *lustreProcfsSource) parseFile(nodeType string, metricType string, path
return nil
}

func (s *lustreProcfsSource) counterMetric(labels []string, labelValues []string, name string, helpText string, value uint64) prometheus.Metric {
func (s *lustreProcfsSource) counterMetric(labels []string, labelValues []string, name string, helpText string, value float64) prometheus.Metric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", name),
Expand All @@ -824,12 +824,12 @@ func (s *lustreProcfsSource) counterMetric(labels []string, labelValues []string
nil,
),
prometheus.CounterValue,
float64(value),
value,
labelValues...,
)
}

func (s *lustreProcfsSource) gaugeMetric(labels []string, labelValues []string, name string, helpText string, value uint64) prometheus.Metric {
func (s *lustreProcfsSource) gaugeMetric(labels []string, labelValues []string, name string, helpText string, value float64) prometheus.Metric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", name),
Expand All @@ -838,12 +838,12 @@ func (s *lustreProcfsSource) gaugeMetric(labels []string, labelValues []string,
nil,
),
prometheus.GaugeValue,
float64(value),
value,
labelValues...,
)
}

func (s *lustreProcfsSource) untypedMetric(labels []string, labelValues []string, name string, helpText string, value uint64) prometheus.Metric {
func (s *lustreProcfsSource) untypedMetric(labels []string, labelValues []string, name string, helpText string, value float64) prometheus.Metric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", name),
Expand All @@ -852,7 +852,7 @@ func (s *lustreProcfsSource) untypedMetric(labels []string, labelValues []string
nil,
),
prometheus.UntypedValue,
float64(value),
value,
labelValues...,
)
}
8 changes: 4 additions & 4 deletions sources/procfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestGetJobStats(t *testing.T) {
testJobID := "29"
testPromName := "job_write_bytes_total"
testHelpText := writeTotalHelp
expected := uint64(274726912)
expected := float64(274726912)

metricList, err := getJobStatsIOMetrics(testJobBlock, testJobID, testPromName, testHelpText)
if err != nil {
Expand All @@ -69,7 +69,7 @@ func TestGetJobStats(t *testing.T) {
t.Fatalf("Retrieved an unexpected number of items. Expected: %d, Got: %d", 1, l)
}
if metricList[0].value != expected {
t.Fatalf("Retrieved an unexpected value. Expected: %d, Got: %d", expected, metricList[0].value)
t.Fatalf("Retrieved an unexpected value. Expected: %f, Got: %f", expected, metricList[0].value)
}
if metricList[0].help != writeTotalHelp {
t.Fatal("Retrieved an unexpected help text.")
Expand All @@ -88,8 +88,8 @@ func TestGetJobStats(t *testing.T) {
if l := len(metricList); l != 10 {
t.Fatalf("Retrieved an unexpected number of items. Expected: %d, Got: %d", 1, l)
}
if metricList[9].value != 10 {
t.Fatalf("Retrieved an unexpected value. Expected: %d, Got: %d", 10, metricList[9].value)
if metricList[9].value != float64(10) {
t.Fatalf("Retrieved an unexpected value. Expected: %f, Got: %f", float64(10), metricList[9].value)
}
if metricList[3].help != jobStatsHelp {
t.Fatal("Retrieved an unexpected help text.")
Expand Down
16 changes: 8 additions & 8 deletions sources/procsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (s *lustreProcsysSource) Update(ch chan<- prometheus.Metric) (err error) {
if metric.filename == stats {
metricType = stats
}
err = s.parseFile(metric.source, metricType, path, metric.helpText, metric.promName, func(nodeType string, nodeName string, name string, helpText string, value uint64) {
err = s.parseFile(metric.source, metricType, path, metric.helpText, metric.promName, func(nodeType string, nodeName string, name string, helpText string, value float64) {
ch <- metric.metricFunc([]string{nodeType}, []string{nodeName}, name, helpText, value)
})
if err != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ func parseSysStatsFile(helpText string, promName string, statsFile string) (metr
return metric, nil
}
index := statsMap[helpText]
value, err := strconv.ParseUint(statsResults[index], 10, 64)
value, err := strconv.ParseFloat(statsResults[index], 64)
if err != nil {
return metric, err
}
Expand All @@ -158,7 +158,7 @@ func parseSysStatsFile(helpText string, promName string, statsFile string) (metr
return metric, nil
}

func (s *lustreProcsysSource) parseFile(nodeType string, metricType string, path string, helpText string, promName string, handler func(string, string, string, string, uint64)) (err error) {
func (s *lustreProcsysSource) parseFile(nodeType string, metricType string, path string, helpText string, promName string, handler func(string, string, string, string, float64)) (err error) {
_, nodeName, err := parseFileElements(path, 0)
if err != nil {
return err
Expand All @@ -169,7 +169,7 @@ func (s *lustreProcsysSource) parseFile(nodeType string, metricType string, path
if err != nil {
return err
}
convertedValue, err := strconv.ParseUint(strings.TrimSpace(string(value)), 10, 64)
convertedValue, err := strconv.ParseFloat(strings.TrimSpace(string(value)), 64)
if err != nil {
return err
}
Expand All @@ -189,7 +189,7 @@ func (s *lustreProcsysSource) parseFile(nodeType string, metricType string, path
return nil
}

func (s *lustreProcsysSource) counterMetric(labels []string, labelValues []string, name string, helpText string, value uint64) prometheus.Metric {
func (s *lustreProcsysSource) counterMetric(labels []string, labelValues []string, name string, helpText string, value float64) prometheus.Metric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", name),
Expand All @@ -198,12 +198,12 @@ func (s *lustreProcsysSource) counterMetric(labels []string, labelValues []strin
nil,
),
prometheus.CounterValue,
float64(value),
value,
labelValues...,
)
}

func (s *lustreProcsysSource) gaugeMetric(labels []string, labelValues []string, name string, helpText string, value uint64) prometheus.Metric {
func (s *lustreProcsysSource) gaugeMetric(labels []string, labelValues []string, name string, helpText string, value float64) prometheus.Metric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(Namespace, "", name),
Expand All @@ -212,7 +212,7 @@ func (s *lustreProcsysSource) gaugeMetric(labels []string, labelValues []string,
nil,
),
prometheus.GaugeValue,
float64(value),
value,
labelValues...,
)
}

0 comments on commit 9828402

Please sign in to comment.