Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaling option for values #106

Merged
merged 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 float64 `yaml:"mqtt_value_scale"`
}

// StringValueMappingConfig defines the mapping from string to float
Expand Down
5 changes: 5 additions & 0 deletions pkg/metrics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 / cfg.MQTTValueScale
anegrin marked this conversation as resolved.
Show resolved Hide resolved
}

return Metric{
Description: cfg.PrometheusDescription(),
Value: metricValue,
Expand Down
109 changes: 107 additions & 2 deletions pkg/metrics/parser_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -50,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{
Expand Down Expand Up @@ -94,6 +121,58 @@ func TestParser_parseMetric(t *testing.T) {
Topic: "",
},
},
{
name: "scaled float value",
hikhvar marked this conversation as resolved.
Show resolved Hide resolved
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: "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{
Expand All @@ -119,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{
Expand Down