fix #117 by distinquishing prometheus value types

This commit is contained in:
Daniel Nägele 2021-10-25 14:10:09 +02:00
parent 4bfa7adfef
commit e74de4edf1
2 changed files with 15 additions and 4 deletions

View File

@ -72,15 +72,22 @@ func (c *interfaceCollector) collectMetricForProperty(property string, re *proto
if value := re.Map[property]; value != "" { if value := re.Map[property]; value != "" {
var ( var (
v float64 v float64
vtype prometheus.ValueType
err error err error
) )
vtype = prometheus.CounterValue
switch property { switch property {
case "running": case "running":
vtype = prometheus.GaugeValue
if value == "true" { if value == "true" {
v = 1 v = 1
} else { } else {
v = 0 v = 0
} }
case "actual-mtu":
vtype = prometheus.GaugeValue
fallthrough
default: default:
v, err = strconv.ParseFloat(value, 64) v, err = strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
@ -94,7 +101,8 @@ func (c *interfaceCollector) collectMetricForProperty(property string, re *proto
return return
} }
} }
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, ctx.ch <- prometheus.MustNewConstMetric(desc, vtype, v, ctx.device.Name, ctx.device.Address,
re.Map["name"], re.Map["type"], re.Map["disabled"], re.Map["comment"], re.Map["running"], re.Map["slave"]) re.Map["name"], re.Map["type"], re.Map["disabled"], re.Map["comment"], re.Map["running"], re.Map["slave"])
} }
} }

View File

@ -81,6 +81,7 @@ func (c *resourceCollector) collectForStat(re *proto.Sentence, ctx *collectorCon
func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) { func (c *resourceCollector) collectMetricForProperty(property string, re *proto.Sentence, ctx *collectorContext) {
var v float64 var v float64
var vtype prometheus.ValueType
var err error var err error
// const boardname = "BOARD" // const boardname = "BOARD"
// const version = "3.33.3" // const version = "3.33.3"
@ -90,11 +91,13 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
if property == "uptime" { if property == "uptime" {
v, err = parseUptime(re.Map[property]) v, err = parseUptime(re.Map[property])
vtype = prometheus.CounterValue
} else { } else {
if re.Map[property] == "" { if re.Map[property] == "" {
return return
} }
v, err = strconv.ParseFloat(re.Map[property], 64) v, err = strconv.ParseFloat(re.Map[property], 64)
vtype = prometheus.GaugeValue
} }
if err != nil { if err != nil {
@ -108,7 +111,7 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
} }
desc := c.descriptions[property] desc := c.descriptions[property]
ctx.ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, ctx.device.Name, ctx.device.Address, boardname, version) ctx.ch <- prometheus.MustNewConstMetric(desc, vtype, v, ctx.device.Name, ctx.device.Address, boardname, version)
} }
func parseUptime(uptime string) (float64, error) { func parseUptime(uptime string) (float64, error) {