Merge pull request #125 from debugloop/master

fix #117 by distinquishing prometheus value types
This commit is contained in:
Steve Brunton 2022-02-21 13:03:02 -05:00 committed by GitHub
commit 5d7c795597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -71,16 +71,23 @@ func (c *interfaceCollector) collectMetricForProperty(property string, re *proto
desc := c.descriptions[property]
if value := re.Map[property]; value != "" {
var (
v float64
err error
v float64
vtype prometheus.ValueType
err error
)
vtype = prometheus.CounterValue
switch property {
case "running":
vtype = prometheus.GaugeValue
if value == "true" {
v = 1
} else {
v = 0
}
case "actual-mtu":
vtype = prometheus.GaugeValue
fallthrough
default:
v, err = strconv.ParseFloat(value, 64)
if err != nil {
@ -94,7 +101,8 @@ func (c *interfaceCollector) collectMetricForProperty(property string, re *proto
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"])
}
}

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) {
var v float64
var vtype prometheus.ValueType
var err error
// const boardname = "BOARD"
// const version = "3.33.3"
@ -90,11 +91,13 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
if property == "uptime" {
v, err = parseUptime(re.Map[property])
vtype = prometheus.CounterValue
} else {
if re.Map[property] == "" {
return
}
v, err = strconv.ParseFloat(re.Map[property], 64)
vtype = prometheus.GaugeValue
}
if err != nil {
@ -108,7 +111,7 @@ func (c *resourceCollector) collectMetricForProperty(property string, re *proto.
}
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) {