From 740352bc00607e3fee4f4d9c0609c0052c9b3be9 Mon Sep 17 00:00:00 2001 From: alessandro negrin Date: Mon, 28 Nov 2022 18:39:39 +0100 Subject: [PATCH 1/4] scaling option for MQTT values --- pkg/config/config.go | 1 + pkg/metrics/parser.go | 5 +++++ pkg/metrics/parser_test.go | 31 +++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 728665c..aea9554 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -135,6 +135,7 @@ type MetricConfig struct { ValueType string `yaml:"type"` ConstantLabels map[string]string `yaml:"const_labels"` StringValueMapping *StringValueMappingConfig `yaml:"string_value_mapping"` + MQTTValueScale uint32 `yaml:"mqtt_value_scale"` } // StringValueMappingConfig defines the mapping from string to float diff --git a/pkg/metrics/parser.go b/pkg/metrics/parser.go index 8a465ce..a152517 100644 --- a/pkg/metrics/parser.go +++ b/pkg/metrics/parser.go @@ -94,6 +94,11 @@ func (p *Parser) parseMetric(metricPath string, deviceID string, value interface } else { return Metric{}, fmt.Errorf("got data with unexpectd type: %T ('%s')", value, value) } + + if cfg.MQTTValueScale != 0 { + metricValue = metricValue / float64(cfg.MQTTValueScale) + } + return Metric{ Description: cfg.PrometheusDescription(), Value: metricValue, diff --git a/pkg/metrics/parser_test.go b/pkg/metrics/parser_test.go index fa82f75..d15bef1 100644 --- a/pkg/metrics/parser_test.go +++ b/pkg/metrics/parser_test.go @@ -1,11 +1,12 @@ package metrics import ( - "github.com/hikhvar/mqtt2prometheus/pkg/config" - "github.com/prometheus/client_golang/prometheus" "reflect" "testing" "time" + + "github.com/hikhvar/mqtt2prometheus/pkg/config" + "github.com/prometheus/client_golang/prometheus" ) func TestParser_parseMetric(t *testing.T) { @@ -94,6 +95,32 @@ func TestParser_parseMetric(t *testing.T) { Topic: "", }, }, + { + name: "scaled float value", + fields: fields{ + map[string][]config.MetricConfig{ + "humidity": []config.MetricConfig{ + { + PrometheusName: "humidity", + ValueType: "gauge", + MQTTValueScale: 100, + }, + }, + }, + }, + args: args{ + metricPath: "humidity", + deviceID: "dht22", + value: 12.6, + }, + want: Metric{ + Description: prometheus.NewDesc("humidity", "", []string{"sensor", "topic"}, nil), + ValueType: prometheus.GaugeValue, + Value: 0.126, + IngestTime: testNow(), + Topic: "", + }, + }, { name: "bool value true", fields: fields{ From a889c7769b6ebf3c8bf23929fe4572e04e75e455 Mon Sep 17 00:00:00 2001 From: alessandro negrin Date: Mon, 28 Nov 2022 18:43:30 +0100 Subject: [PATCH 2/4] updated readme --- Readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Readme.md b/Readme.md index 079fda7..1c88805 100644 --- a/Readme.md +++ b/Readme.md @@ -186,6 +186,8 @@ metrics: - prom_name: humidity # The name of the metric in a MQTT JSON message mqtt_name: humidity + # The scale of the metric in a MQTT JSON message (mqtt_value : scale = prom_value : 1) + mqtt_value_scale: 100 # The prometheus help text for this metric help: DHT22 humidity reading # The prometheus type for this metric. Valid values are: "gauge" and "counter" From d077874127d4979e2292dc43f157ca970ad42247 Mon Sep 17 00:00:00 2001 From: alessandro negrin Date: Tue, 29 Nov 2022 10:33:55 +0100 Subject: [PATCH 3/4] scale as unsigned float; tests for values from string or boolean --- pkg/config/config.go | 2 +- pkg/metrics/parser.go | 2 +- pkg/metrics/parser_test.go | 78 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index aea9554..07a27a2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -135,7 +135,7 @@ type MetricConfig struct { ValueType string `yaml:"type"` ConstantLabels map[string]string `yaml:"const_labels"` StringValueMapping *StringValueMappingConfig `yaml:"string_value_mapping"` - MQTTValueScale uint32 `yaml:"mqtt_value_scale"` + MQTTValueScale float64 `yaml:"mqtt_value_scale"` } // StringValueMappingConfig defines the mapping from string to float diff --git a/pkg/metrics/parser.go b/pkg/metrics/parser.go index a152517..2a59eb0 100644 --- a/pkg/metrics/parser.go +++ b/pkg/metrics/parser.go @@ -96,7 +96,7 @@ func (p *Parser) parseMetric(metricPath string, deviceID string, value interface } if cfg.MQTTValueScale != 0 { - metricValue = metricValue / float64(cfg.MQTTValueScale) + metricValue = metricValue / cfg.MQTTValueScale } return Metric{ diff --git a/pkg/metrics/parser_test.go b/pkg/metrics/parser_test.go index d15bef1..64f5429 100644 --- a/pkg/metrics/parser_test.go +++ b/pkg/metrics/parser_test.go @@ -51,6 +51,32 @@ func TestParser_parseMetric(t *testing.T) { Topic: "", }, }, + { + name: "scaled string value", + fields: fields{ + map[string][]config.MetricConfig{ + "temperature": []config.MetricConfig{ + { + PrometheusName: "temperature", + ValueType: "gauge", + MQTTValueScale: 100, + }, + }, + }, + }, + args: args{ + metricPath: "temperature", + deviceID: "dht22", + value: "12.6", + }, + want: Metric{ + Description: prometheus.NewDesc("temperature", "", []string{"sensor", "topic"}, nil), + ValueType: prometheus.GaugeValue, + Value: 0.126, + IngestTime: testNow(), + Topic: "", + }, + }, { name: "string value failure", fields: fields{ @@ -121,6 +147,32 @@ func TestParser_parseMetric(t *testing.T) { Topic: "", }, }, + { + name: "negative scaled float value", + fields: fields{ + map[string][]config.MetricConfig{ + "humidity": []config.MetricConfig{ + { + PrometheusName: "humidity", + ValueType: "gauge", + MQTTValueScale: -0.5, + }, + }, + }, + }, + args: args{ + metricPath: "humidity", + deviceID: "dht22", + value: 12.6, + }, + want: Metric{ + Description: prometheus.NewDesc("humidity", "", []string{"sensor", "topic"}, nil), + ValueType: prometheus.GaugeValue, + Value: -25.2, + IngestTime: testNow(), + Topic: "", + }, + }, { name: "bool value true", fields: fields{ @@ -146,6 +198,32 @@ func TestParser_parseMetric(t *testing.T) { Topic: "", }, }, + { + name: "scaled bool value", + fields: fields{ + map[string][]config.MetricConfig{ + "enabled": []config.MetricConfig{ + { + PrometheusName: "enabled", + ValueType: "gauge", + MQTTValueScale: 2, + }, + }, + }, + }, + args: args{ + metricPath: "enabled", + deviceID: "dht22", + value: true, + }, + want: Metric{ + Description: prometheus.NewDesc("enabled", "", []string{"sensor", "topic"}, nil), + ValueType: prometheus.GaugeValue, + Value: 0.5, + IngestTime: testNow(), + Topic: "", + }, + }, { name: "bool value false", fields: fields{ From e76641898a9d3a1a99e0ab9d68db9643fcd2212e Mon Sep 17 00:00:00 2001 From: alessandro negrin Date: Tue, 29 Nov 2022 16:18:03 +0100 Subject: [PATCH 4/4] scale as a multiplier --- Readme.md | 2 +- pkg/metrics/parser.go | 2 +- pkg/metrics/parser_test.go | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index 1c88805..259d396 100644 --- a/Readme.md +++ b/Readme.md @@ -186,7 +186,7 @@ metrics: - prom_name: humidity # The name of the metric in a MQTT JSON message mqtt_name: humidity - # The scale of the metric in a MQTT JSON message (mqtt_value : scale = prom_value : 1) + # The scale of the metric in a MQTT JSON message (prom_value = mqtt_value * scale) mqtt_value_scale: 100 # The prometheus help text for this metric help: DHT22 humidity reading diff --git a/pkg/metrics/parser.go b/pkg/metrics/parser.go index 2a59eb0..c08da3b 100644 --- a/pkg/metrics/parser.go +++ b/pkg/metrics/parser.go @@ -96,7 +96,7 @@ func (p *Parser) parseMetric(metricPath string, deviceID string, value interface } if cfg.MQTTValueScale != 0 { - metricValue = metricValue / cfg.MQTTValueScale + metricValue = metricValue * cfg.MQTTValueScale } return Metric{ diff --git a/pkg/metrics/parser_test.go b/pkg/metrics/parser_test.go index 64f5429..8d6a680 100644 --- a/pkg/metrics/parser_test.go +++ b/pkg/metrics/parser_test.go @@ -59,7 +59,7 @@ func TestParser_parseMetric(t *testing.T) { { PrometheusName: "temperature", ValueType: "gauge", - MQTTValueScale: 100, + MQTTValueScale: 0.01, }, }, }, @@ -129,7 +129,7 @@ func TestParser_parseMetric(t *testing.T) { { PrometheusName: "humidity", ValueType: "gauge", - MQTTValueScale: 100, + MQTTValueScale: 0.01, }, }, }, @@ -155,7 +155,7 @@ func TestParser_parseMetric(t *testing.T) { { PrometheusName: "humidity", ValueType: "gauge", - MQTTValueScale: -0.5, + MQTTValueScale: -2, }, }, }, @@ -206,7 +206,7 @@ func TestParser_parseMetric(t *testing.T) { { PrometheusName: "enabled", ValueType: "gauge", - MQTTValueScale: 2, + MQTTValueScale: 0.5, }, }, },