mirror of
https://github.com/hikhvar/mqtt2prometheus.git
synced 2026-08-23 21:16:15 +00:00
scale as unsigned float; tests for values from string or boolean
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user