hikhvar/mqtt2prometheus

How to import an array

Rocco83 opened this issue · 2 comments

I am a newbie of both prometheus & mqtt & mqtt2prometheus.

Anyhow, i'm having trouble parsing the following json:

whatever/boiler {"temperature":23.34,"temperature_unit":"°C","pressure":0,"pressure_unit":"bar","uptime":5116907,"mqttRawData":["00000011","11001011","01011101","11100000"]}

I've added the following section to mqtt2prometheus:

  -
    # The name of the metric in prometheus
    prom_name: whatever_boiler_rawdata
    # The name of the metric in a MQTT JSON message
    mqtt_name: mqttRawData
    # The prometheus help text for this metric
    help: rawdata arduino boiler
    # 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: arduino_boiler

Logs say:

2022-02-01T03:18:32+01:00        error        cmd/mqtt2prometheus.go:116        Error while processing message        {"error": "could not store metrics '{\"temperature\":23.24,\"temperature_unit\":\"°C\",\"pressure\":-0.01,\"pressure_unit\":\"bar\",\"uptime\":5229573,\"mqttRawData\":[\"00000011\",\"11001001\",\"01011101\",\"11000000\"]}' on topic whatever/boiler: failed to parse valid metric value: got data with unexpectd type: []interface {} ('[00000011 11001001 01011101 11000000]')"}

So is possible to import the 4 raw data into prometheus as array index?

Thanks,

The prometheus data model doesn't have an array type. Hence I don't see any way to expose those arrays as prometheus metrics. You could define a metric for every element in the array, adressing individual values of the array with a https://github.com/thedevsaddam/gojsonq path.

Like:

  - prom_name: whatever_boiler_rawdata_0
    mqtt_name: mqttRawData.[0]
    help: rawdata arduino boiler first element from array
    type: gauge
    const_labels:
      sensor_type: arduino_boiler
  - prom_name: whatever_boiler_rawdata_1
    mqtt_name: mqttRawData.[1]
    help: rawdata arduino boiler second element from array
    type: gauge
    const_labels:
      sensor_type: arduino_boiler
      ...
      

However, for that approach you need to know the number of elements in mqttRawData in advance.

This twitter thread is a very good introduction into the prometheus data model: https://twitter.com/iximiuz/status/1482363582100684801

In general this pages has very good learning material regarding prometheus: https://iximiuz.com/en/series/learning-prometheus-and-promql/

I will close the issue, since I think I answered the questions. Please don't hesitate to reopen the issue if you think there are open questions left or my proposed solution doesn't work.