Skip to content

Commit 070e15f

Browse files
authored
Merge pull request #106 from anegrin/float-value-scale
Scaling option for values
2 parents 380d12f + e766418 commit 070e15f

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ metrics:
186186
- prom_name: humidity
187187
# The name of the metric in a MQTT JSON message
188188
mqtt_name: humidity
189+
# The scale of the metric in a MQTT JSON message (prom_value = mqtt_value * scale)
190+
mqtt_value_scale: 100
189191
# The prometheus help text for this metric
190192
help: DHT22 humidity reading
191193
# The prometheus type for this metric. Valid values are: "gauge" and "counter"

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ type MetricConfig struct {
135135
ValueType string `yaml:"type"`
136136
ConstantLabels map[string]string `yaml:"const_labels"`
137137
StringValueMapping *StringValueMappingConfig `yaml:"string_value_mapping"`
138+
MQTTValueScale float64 `yaml:"mqtt_value_scale"`
138139
}
139140

140141
// StringValueMappingConfig defines the mapping from string to float

pkg/metrics/parser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func (p *Parser) parseMetric(metricPath string, deviceID string, value interface
9494
} else {
9595
return Metric{}, fmt.Errorf("got data with unexpectd type: %T ('%s')", value, value)
9696
}
97+
98+
if cfg.MQTTValueScale != 0 {
99+
metricValue = metricValue * cfg.MQTTValueScale
100+
}
101+
97102
return Metric{
98103
Description: cfg.PrometheusDescription(),
99104
Value: metricValue,

pkg/metrics/parser_test.go

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package metrics
22

33
import (
4-
"github.com/hikhvar/mqtt2prometheus/pkg/config"
5-
"github.com/prometheus/client_golang/prometheus"
64
"reflect"
75
"testing"
86
"time"
7+
8+
"github.com/hikhvar/mqtt2prometheus/pkg/config"
9+
"github.com/prometheus/client_golang/prometheus"
910
)
1011

1112
func TestParser_parseMetric(t *testing.T) {
@@ -50,6 +51,32 @@ func TestParser_parseMetric(t *testing.T) {
5051
Topic: "",
5152
},
5253
},
54+
{
55+
name: "scaled string value",
56+
fields: fields{
57+
map[string][]config.MetricConfig{
58+
"temperature": []config.MetricConfig{
59+
{
60+
PrometheusName: "temperature",
61+
ValueType: "gauge",
62+
MQTTValueScale: 0.01,
63+
},
64+
},
65+
},
66+
},
67+
args: args{
68+
metricPath: "temperature",
69+
deviceID: "dht22",
70+
value: "12.6",
71+
},
72+
want: Metric{
73+
Description: prometheus.NewDesc("temperature", "", []string{"sensor", "topic"}, nil),
74+
ValueType: prometheus.GaugeValue,
75+
Value: 0.126,
76+
IngestTime: testNow(),
77+
Topic: "",
78+
},
79+
},
5380
{
5481
name: "string value failure",
5582
fields: fields{
@@ -94,6 +121,58 @@ func TestParser_parseMetric(t *testing.T) {
94121
Topic: "",
95122
},
96123
},
124+
{
125+
name: "scaled float value",
126+
fields: fields{
127+
map[string][]config.MetricConfig{
128+
"humidity": []config.MetricConfig{
129+
{
130+
PrometheusName: "humidity",
131+
ValueType: "gauge",
132+
MQTTValueScale: 0.01,
133+
},
134+
},
135+
},
136+
},
137+
args: args{
138+
metricPath: "humidity",
139+
deviceID: "dht22",
140+
value: 12.6,
141+
},
142+
want: Metric{
143+
Description: prometheus.NewDesc("humidity", "", []string{"sensor", "topic"}, nil),
144+
ValueType: prometheus.GaugeValue,
145+
Value: 0.126,
146+
IngestTime: testNow(),
147+
Topic: "",
148+
},
149+
},
150+
{
151+
name: "negative scaled float value",
152+
fields: fields{
153+
map[string][]config.MetricConfig{
154+
"humidity": []config.MetricConfig{
155+
{
156+
PrometheusName: "humidity",
157+
ValueType: "gauge",
158+
MQTTValueScale: -2,
159+
},
160+
},
161+
},
162+
},
163+
args: args{
164+
metricPath: "humidity",
165+
deviceID: "dht22",
166+
value: 12.6,
167+
},
168+
want: Metric{
169+
Description: prometheus.NewDesc("humidity", "", []string{"sensor", "topic"}, nil),
170+
ValueType: prometheus.GaugeValue,
171+
Value: -25.2,
172+
IngestTime: testNow(),
173+
Topic: "",
174+
},
175+
},
97176
{
98177
name: "bool value true",
99178
fields: fields{
@@ -119,6 +198,32 @@ func TestParser_parseMetric(t *testing.T) {
119198
Topic: "",
120199
},
121200
},
201+
{
202+
name: "scaled bool value",
203+
fields: fields{
204+
map[string][]config.MetricConfig{
205+
"enabled": []config.MetricConfig{
206+
{
207+
PrometheusName: "enabled",
208+
ValueType: "gauge",
209+
MQTTValueScale: 0.5,
210+
},
211+
},
212+
},
213+
},
214+
args: args{
215+
metricPath: "enabled",
216+
deviceID: "dht22",
217+
value: true,
218+
},
219+
want: Metric{
220+
Description: prometheus.NewDesc("enabled", "", []string{"sensor", "topic"}, nil),
221+
ValueType: prometheus.GaugeValue,
222+
Value: 0.5,
223+
IngestTime: testNow(),
224+
Topic: "",
225+
},
226+
},
122227
{
123228
name: "bool value false",
124229
fields: fields{

0 commit comments

Comments
 (0)