-
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
Trim null characters in Value data format #2049
Conversation
Some producers (such as the paho embedded c mqtt client) add a null character "\x00" to the end of a message. The Value parser would fail on any message from such a producer.
@@ -23,6 +23,7 @@ func (v *ValueParser) Parse(buf []byte) ([]telegraf.Metric, error) { | |||
if v.DataType == "string" { | |||
vStr = strings.TrimSpace(string(buf)) | |||
} else { | |||
buf = bytes.Trim(buf, "\x00") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does bytes.TrimSpace trim null characters? maybe we should just do buf = bytes.TrimSpace(buf)
as the first step in this function, then we can also remove the strings.TrimSpace if the data type is a string as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bytes.TrimSpace does not trim null characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I think maybe we should trim whitespace from none string values as well, In which case we could call bytes.TrimSpace and bytes.Trim(buf, "\x00") first thing in the function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please do that, in that case the first step of the function should be to do buf = bytes.TrimSpace(bytes.Trim(buf, "\x00"))
if v.DataType == "string" { | ||
vStr = strings.TrimSpace(string(buf)) | ||
} else { | ||
vStr := string(buf) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than reassigning the variable, you can just do vStr := string(bytes.TrimSpace(bytes.Trim(buf, "\x00")))
thanks, please update the changelog and I'll merge |
Required for all PRs:
Some producers (such as the paho embedded c mqtt client) add a null
character "\x00" to the end of a message. The Value parser fails on
any message from such a producer.