Skip to content

Commit

Permalink
Breaking: Don't append + automatically to the topic
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Petrausch committed Jul 18, 2020
1 parent 29eb925 commit f13c36a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {

for {
err = mqttclient.Subscribe(mqttClientOptions, mqttclient.SubscribeOptions{
Topic: cfg.MQTT.TopicPath + "/+",
Topic: cfg.MQTT.TopicPath,
QoS: cfg.MQTT.QoS,
OnMessageReceived: ingest.SetupSubscriptionHandler(errorChan),
})
Expand Down
2 changes: 1 addition & 1 deletion config.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mqtt:
# Optional: Username and Password for authenticating with the MQTT Server
# user: bob
# password: happylittleclouds
# The Topic path to subscripe to. Actually this will become `$topic_path/+`
# The Topic path to subscripe to.
topic_path: v1/devices/me
# Optional: Regular expression to extract the device ID from the topic path. The default regular expression, assumes
# that the last "element" of the topic_path is the device id.
Expand Down
90 changes: 90 additions & 0 deletions examples/gosund_sp111.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Settings for the MQTT Client. Currently only these three are supported
mqtt:
# The MQTT broker to connect to
server: tcp://192.168.1.11:1883
# Optional: Username and Password for authenticating with the MQTT Server
# user: bob
# password: happylittleclouds
# The Topic path to subscripe to. Actually this will become `$topic_path/+`
topic_path: tele/+/SENSOR
# Optional: Regular expression to extract the device ID from the topic path. The default regular expression, assumes
# that the last "element" of the topic_path is the device id.
# The regular expression must contain a named capture group with the name deviceid
# For example the expression for tasamota based sensors is "tele/(?P<deviceid>.*)/.*"
device_id_regex: "tele/(?P<deviceid>.*)/SENSOR"
# The MQTT QoS level
qos: 0
cache:
# Timeout. Each received metric will be presented for this time if no update is send via MQTT.
# Set the timeout to -1 to disable the deletion of metrics from the cache. The exporter presents the ingest timestamp
# to prometheus.
timeout: 24h
# This is a list of valid metrics. Only metrics listed here will be exported
metrics:
# The name of the metric in prometheus
- prom_name: consumed_energy_total_kilowatthours
mqtt_name: "ENERGY.Total"
help: "total measured kilowatthours since flash"
type: counter
- prom_name: voltage_volt
mqtt_name: "ENERGY.Voltage"
help: "Currently measured voltage"
type: gauge
- prom_name: current_ampere
mqtt_name: "ENERGY.Current"
help: "Currently measured current"
type: gauge
- prom_name: temperature
# The name of the metric in a MQTT JSON message
mqtt_name: temperature
# The prometheus help text for this metric
help: DHT22 temperature reading
# The prometheus type for this metric. Valid values are: "gauge" and "counter"
type: gauge
# A map of string to string for constant labels. This labels will be attached to every prometheus metric
const_labels:
sensor_type: dht22
# The name of the metric in prometheus
- prom_name: humidity
# The name of the metric in a MQTT JSON message
mqtt_name: humidity
# The prometheus help text for this metric
help: DHT22 humidity reading
# The prometheus type for this metric. Valid values are: "gauge" and "counter"
type: gauge
# A map of string to string for constant labels. This labels will be attached to every prometheus metric
const_labels:
sensor_type: dht22
# The name of the metric in prometheus
- prom_name: heat_index
# The name of the metric in a MQTT JSON message
mqtt_name: heat_index
# The prometheus help text for this metric
help: DHT22 heatIndex calculation
# The prometheus type for this metric. Valid values are: "gauge" and "counter"
type: gauge
# A map of string to string for constant labels. This labels will be attached to every prometheus metric
const_labels:
sensor_type: dht22
# The name of the metric in prometheus
- prom_name: state
# The name of the metric in a MQTT JSON message
mqtt_name: state
# Regular expression to only match sensors with the given name pattern
sensor_name_filter: "^.*-light$"
# The prometheus help text for this metric
help: Light state
# The prometheus type for this metric. Valid values are: "gauge" and "counter"
type: gauge
# A map of string to string for constant labels. This labels will be attached to every prometheus metric
const_labels:
sensor_type: ikea
# When specified, enables mapping between string values to metric values.
string_value_mapping:
# A map of string to metric value.
map:
off: 0
low: 0
# Metric value to use if a match cannot be found in the map above.
# If not specified, parsing error will occur.
error_value: 1
5 changes: 3 additions & 2 deletions pkg/metrics/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func (i *Ingest) store(deviceID string, payload []byte) error {
for path := range i.metricConfigs {
rawValue := parsed.Find(path)
parsed.Reset()
fmt.Printf("query path: %q data: %v\n", path, rawValue)

if rawValue == nil {
continue
}
m, err := i.parseMetric(path, deviceID, rawValue)
if err != nil {
return fmt.Errorf("failed to parse valid metric value: %w", err)
Expand Down

0 comments on commit f13c36a

Please sign in to comment.