-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MQTT input + JSON data format - trailing NULL char in payload causes parse error #7536
Comments
I think, that I found, what could be the reason of JSON parser behave as above. I've compared part of source code in parsers/value/parser.go:
At line 20, there is function to trim Nul ('\x00') character - everything works fine for that data format. On the other hand - in case of json/parser.go this part of function obviously looks different:
In this case there is no part where nulls are trimmed, probably this one would fix problem:
|
Adding a null byte to the end of the JSON document seems like an error, the document is no longer valid JSON, null bytes are not allowed whitespace: https://tools.ietf.org/html/rfc7159#section-2 I'm not familiar with MQTT Producer, and a search brings up a few links, can you add a link to the product you are using? |
I'm using industrial MQTT producer - OI Gateway brought by Wonderware (AVEVA now) software. Below you'll find some video with explanation, how it works: |
Thanks for the link, can you report the issue to them? I don't really want to workaround other vendors bugs in Telegraf, since it makes things more complex on our end. |
I fully understand that making workarounds could lead someday to problems, but I belive that in this case it's not only a workaround, but more like - unification. I was curious why "value" data format has those trimming at the end and i found pull request from 2016 associated with exact the same bug as we have here - it was request #2049
So it's not even an issue with json data format, but any other, where MQTT payload need to be parsed. And partially I confirmed this: I've tried to send data via influx line protocol, but also got an error:
As we see above - again at the end of payload message nul character appears. As well, as in case of plain value data format, but value data format was fixed long time ago :) In my opinion it's worth to consider adding null character trimming not only in json parser, but also in rest of parsers (where issue like this one could happen), except value, where it's implemented already. |
That's an interesting issue, I learned something new about Telegraf today. I tested the master branch of the https://github.com/eclipse/paho.mqtt.embedded-c, and I do not have this issue with the trailing nul. I was also unable to find any issues reported where it may have been fixed. There are a few things accepting the PR would be an issue for. We want to be able to support non-text based formats in the parsers where we cannot safely remove trailing nul characters. We also would like to be able to add support for compression to inputs. Given that this would also need added for all parsers, I'm inclined to not add a workaround for these bad payloads. I can give you a workaround though. In the |
Thank you for explanation @danielnelson, I appreciate it. Fun fact - using string parser as first parser (in mqtt_consumer) removes null char by default, and THEN json parser has no problem with converting data to json format... For everyone who would face this problem in the future, my config file looks like this at the moment:
|
As of Jan 9th 2023, this error is still out there. I've implemented the workaround. I agree with @danielnelson this is hard to pin down the source of the bad data (ie. is this a bad-sensor), as there is no context around the parsing error. Good context would be the topic that has the bogus data. What I would like to see fixed is that the entire plugin chokes-up - it should fail on the bad value, then recover gracefully. Shall I put that into a feature request? |
Hi everyone,
I’m working on some project, where I need to collect data to influxdb using mqtt input plugin in telegraf. But I’m facing strange behaviour of mqtt input. I’m sending data on specific topic using JSON string (i.e. “{value”:15}", but MQTT producer, which I’m using adds, null (’\x00’) char at the end of message. It looks like below in preview using MQTTBox client:
First message was sent from MQTT producer and it has lenght of 41 bytes, while second I sent from regular MQTT client - MQTTBox - length of the second message is 40 bytes - null character at the end is missing. First message causes error in telegraf, while second is correctly wrote to database:
My current telegraf config file looks like this (I have to use multiple MQTT inputs to multiple Influx databases, but this not a problem):
Do you have any idea, how to configure telegraf to (maybe) ignore those null characters, or cut this one byte using some processor plugin? I read some about proccessors.string.trim_right plugin, but as I tested - it only works, when input data are correct, so It won't be helpful in this case.
It’s also important that, when I’m using “value” data_format of mqtt input, instead of “json” - telegraf works fine with this mqtt producer, even at the end of simple value also null character appears.
So maybe plugin (or even parser) should ignore those characters in payload (i.e. whitespaces, nulls) by default? Anyways - I have to use json format to pass multiple parameters to influx at once.
I will appreciate any help
Have a good day!
The text was updated successfully, but these errors were encountered: