From e74de4edf13e86ddc4fa6452e545f059cf529207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20N=C3=A4gele?= Date: Mon, 25 Oct 2021 14:10:09 +0200 Subject: [PATCH] fix #117 by distinquishing prometheus value types --- collector/interface_collector.go | 14 +++++++++++--- collector/resource_collector.go | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/collector/interface_collector.go b/collector/interface_collector.go index dfcbc1b..1a9b4ca 100644 --- a/collector/interface_collector.go +++ b/collector/interface_collector.go @@ -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"]) + } } diff --git a/collector/resource_collector.go b/collector/resource_collector.go index 2dfbc3d..7d63693 100644 --- a/collector/resource_collector.go +++ b/collector/resource_collector.go @@ -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) {