I have an mqtt telegraf input source containing a json string. The “message” contains two metrics separated by a comma as follows:
{"rssi":-62,"snr":9.25,"pferror":1400,"packetSize":7,"message":"104,105"}
I would like to split the “message” data into two metrics called “spd” and “dir” to be outputted to InfluxDB. I am aware of the processors.split function in Telegraf however do not have the experience to implement it.
Any help would be greatly appreciated.
Anaisdg
September 11, 2023, 4:33pm
2
Hello @simonsmart9 ,
Yes absolutely.
Split as far as I understand I think you might have to use the starlark or execd processor plugin for that type of splitting.
Here’s a similar example.
https://community.influxdata.com/t/how-to-split-measurement-fields-into-separate-measurements/27550/2\
Many thanks @Anaisdg
Starlark did the trick. For anyone who is trying something similar, herewith the code.
[[inputs.mqtt_consumer]]
servers = ["tcp://localhost:1883"]
topics = ["higham/#"]
connection_timeout = "30s"
username = "mymqttusername"
password = "mymqttpassword"
data_format = "json_v2"
[[inputs.mqtt_consumer.json_v2]]
measurement_name = "higham_anemometer"
[[inputs.mqtt_consumer.json_v2.field]]
path = "message"
rename = "higham_message"
type = "string"
[[processors.starlark]]
namepass = ['higham_anemometer']
source = '''
def apply(metric):
higham_data = metric.fields['higham_message']
metric.fields['higham_spd'] = higham_data.split(",")[0]
metric.fields['higham_dir'] = higham_data.split(",")[1]
return metric
'''
srebhan
September 12, 2023, 9:37am
4
@simonsmart9 just as a heads-up, since v1.28.0 (released yesterday) we now do have a split processor which is exactly targetting your use-case to split a metric into multiple ones…