MQTT consumer json_query not tagging with external property



I am receiving json formatted data with mqtt. The problem is that I can not set the json_name_key and the tag_keys with external properties which are not inside the json_query subset.

For example:

“data”: [{“temp”:50, “pressure”:10, “time”:"--------"},
{“temp”:40, “pressure”:20, “time”:"--------"},
{“temp”:30, “pressure”:10, “time”:"--------"}]

If I set json_query= “data”, I can not use for tag “tag1” and “tag2” and for measurement name “name”. The desired output is:

sensor,tag1=tag1,tag2=tag2 temp=50 pressure=10
sensor,tag1=tag1,tag2=tag2 temp=40 pressure=20
sensor,tag1=tag1,tag2=tag2 temp=30 pressure=10

How can I change my telegraf.conf file to make it happen ?



Sounds like the same requirement as in Json metric not captured.

1 Like

I have solved the issue. It is not the cleanest way, but you can create your own plugin. I have made slight changes to the mqtt_consumer plugin source code to add the external fields into each element of the array before passing it to the json parser. Finally I add these fields as tags in the telegraf.conf tag_keys array.

To create your own plugin you can follow this guide:

To sum up, the steps are:

  • Download the telegraf source code
  • In the telegraf/inputs folder, duplicate the mqtt_consumer and change the name.
  • Make the necessary modifications in the go file
  • Add the plugin in this file imports: plugins/inputs/all/all.go
  • Just go back in the telegraf folder and do make
  • Once it is compiled, you have a new telegraf executable, you can launch it with ./telegraf --config <you-own-custom-telegraf.conf>