Parse mqtt topics to values depending on value data type
Hi, with ref to https://github.com/influxdata/telegraf/issues/15949 I'm in a simlar situation. The proposed solution (using a starlark script) looks like a quite heavy solution for a presumably frequent challenge: to parse and store mqtt key value pairs, with value being int/float/string/... depending on the mqtt topic. Due to my limited starlark capabilities I'm stuck.
I know about the auto_float feature and it seems to be the origin of the issues I have with one of my influxDB v1.8.3 measurements.
> show field keys from e3dc
name: e3dc
fieldKey fieldType
-------- ---------
value float
value string
Digging into the documentation, influxDB seems to set the data type of fieldKey per "shard", and it looks like completely random what data type would arrive 1st in a new shard, as such defining value's data type for this shard. InfluxDB itself reports both data types, correctly, but denies to accept write attempts to the other data type afterwards, until a new shard is allocated by the DB engine itself.
Here is how the relevant section of my telegraf.conf looks like
[[inputs.mqtt_consumer]]
alias = "telegraf1_mqtt_value_1"
client_id = "telegraf1_mqtt_value_1"
servers = ["tcp://127.0.0.1:1883"]
username = "uuuu"
password = "pppp"
data_format = "value"
data_type = "auto_float"
topics = [
"e3dc/#",
]
[[inputs.mqtt_consumer.topic_parsing]]
topic = "+/#"
measurement = "measurement/_"
Any advise, pieces of code or toml, or else would be appreciated. Thanks.
Well Telegraf does not have a type definition for the topics so it has to guess what to do. How do you know which datatype you want for a topic?
Basically there are the following ways you can tackle this:
- Define one
mqtt_consumerper data-type and then list the topics with that datatype, i.e. having multiple input plugins. The downside is that you do have multiple client-connections to the broker and you have to specify each topic individually. - You do have information about the topic-datatype relation so you can store the values as
stringin themqtt_consumerand then enforce the correct type via the converter processor or starlark processor. - You make sure the sending side enforces types e.g. by always sending non-numerical characters with a string etc.
There is no magic in Telegraf unfortunately so the information has to be put into the system...
“Any sufficiently advanced technology is indistinguishable from magic.” Clarke's (most famous) law
You do have information about the topic-datatype relation so you can store the values as string in the mqtt_consumer and then enforce the correct type via the converter processor or starlark processor.
The issue is only with simple mqtt payload (data_format = "value"). The payload itself can be either a number - int or float - or a string (one may imagine a lot of other fancy data type like time_t or else, but this simple categorization should cover >99% of the use cases) in combination with influxDB v1.8.3's limited capabilities to deal with a field key having different data types, combined with the strange auto-typing per shard. And with Grafana's evenly limited capability to draw charts from string values - they expect a number to be retrieved and do not convert.
The obvious solution that would come to my mind: put a suffix to the field key if the desired conversion to i.e. float (triggered by auto-float) would fail because the mqttt payload does not contain number-convertible chars. That would generate key-value pairs like:
- mqtt-topic - value (if the payload is directly convertible)
- mqtt-topic - value_s (in all other cases) avoiding to puzzle influxDB.
If that is already achievable via the converter processor (trying to convert like auto-float and modifying the value field name, if that would fail), I'd be more than interessted in learning about the how-to.
I hate it to leave open ends. So, for those you may stumble across a similar (and imho quite common) issue, this is the solution I applied. However, I think this functionality should be standard when inputting and parsing simple mqtt messages.
#####################
# For all simple (topic, value) pairs from mqtt
# check if type of value == string: replace value with value_s
#
# This prevents to have value::field with different types in influx, causing undesired effects when a new shard is generated
#
# https://github.com/influxdata/telegraf/blob/master/plugins/processors/starlark/testdata/rename.star
# https://github.com/google/starlark-go/blob/d1966c6b9fcd/doc/spec.md
#####################
[[processors.starlark]]
# Processor will be called for
order = 3
namepass = ['openDTU', 'P1P2', 'e3dc', 'knx', 'mtr', 'transform']
source = '''
# loads log.debug(), log.info(), log.warn(), log.error()
# https://github.com/influxdata/telegraf/blob/master/plugins/processors/starlark/testdata/logging.star
load("logging.star", "log")
renames = {
'value': 'value_s',
}
def apply( metric ):
for k,v in metric.fields.items():
# log.debug("DEBUG-fields: "+str(k)+" "+str(v))
if k in renames:
if type(v) == 'string':
metric.fields[renames[k]] = v
metric.fields.pop(k)
return metric
'''