Skip to content
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

feat(inputs.mqtt_consumer): Add incoming mqtt message size calculation #11426

Merged
merged 29 commits into from
Aug 22, 2022

Conversation

mrro253
Copy link
Contributor

@mrro253 mrro253 commented Jun 30, 2022

Required for all PRs:

resolves #11334

Gets the size of each component of the incoming mqtt message as described here then adds it to the selfstat object so that it can be accessed by the internal plugin. Also gets the amount of messages received.

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Jun 30, 2022
Fetch upstream changes to resolve conflicts in order to open a pull request into my forked repo and test my plugin.
@Hipska Hipska added area/mqtt plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Jul 4, 2022
@Hipska Hipska changed the title feat: Add incoming mqtt message size calculation feat(inputs/mqtt_consumer): Add incoming mqtt message size calculation Jul 4, 2022
@mrro253
Copy link
Contributor Author

mrro253 commented Jul 4, 2022

todo: can't get the mqtt_consumer.go file to recognize the new collect_bytes_received field from the configuration file in order to allow the data collection to be optional.

Copy link
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, you are going the good way!

plugins/inputs/couchbase/README.md Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/README.md Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
@Hipska Hipska marked this pull request as draft July 5, 2022 09:07
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
Copy link
Contributor

@Hipska Hipska left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Maybe also mention this in the README the way GitHub plugin did. (In Metrics and in Example Output section)

@mrro253 mrro253 marked this pull request as ready for review July 8, 2022 12:57
@mrro253 mrro253 marked this pull request as draft July 11, 2022 08:06
@mrro253
Copy link
Contributor Author

mrro253 commented Jul 11, 2022

Update: upon testing the plugin, I've found that it's calculating every single message as only 61 bytes (found this by dividing the size by the number of messages at various points in time, and would always get 61). I'm thinking that it has to do with the way I add the message size, perhaps it's calculating the size of the object type rather than the object itself? Any suggestions?

plugins/inputs/mqtt_consumer/README.md Outdated Show resolved Hide resolved
@Hipska
Copy link
Contributor

Hipska commented Aug 1, 2022

Are you still working on it? Or is this ready to be reviewed? (Asking since it is still marked as draft)

@mrro253 mrro253 marked this pull request as ready for review August 2, 2022 19:41
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice feature @mrro253! I do have only two small comments...

plugins/inputs/mqtt_consumer/README.md Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
@srebhan srebhan self-assigned this Aug 3, 2022
plugins/inputs/internal/README.md Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/README.md Outdated Show resolved Hide resolved
plugins/inputs/mqtt_consumer/README.md Outdated Show resolved Hide resolved
@Hipska Hipska added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Aug 5, 2022
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the contribution @mrro253! Just have one "out-of-interest" comment regading the remaining-length.

plugins/inputs/mqtt_consumer/mqtt_consumer.go Outdated Show resolved Hide resolved
Co-authored-by: Sven Rebhan <36194019+srebhan@users.noreply.github.com>
@srebhan
Copy link
Member

srebhan commented Aug 5, 2022

!retry-failed

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks @mrro253!

@srebhan
Copy link
Member

srebhan commented Aug 5, 2022

@sspaink another one not running CircleCI... :-(

@sspaink
Copy link
Contributor

sspaink commented Aug 8, 2022

@mrro253 To get the tests to pass you will need to run make fmt to resolve the formatting issue, thanks!

@sspaink sspaink changed the title feat(inputs/mqtt_consumer): Add incoming mqtt message size calculation feat(inputs.mqtt_consumer): Add incoming mqtt message size calculation Aug 8, 2022
@srebhan srebhan requested a review from sspaink August 9, 2022 12:16
Comment on lines 251 to 274
topicSize := len(msg.Topic()) + 2
switch int(msg.Qos()) {
case 1:
qOsSize = 2
qosFlagsSize = 4
case 2:
qOsSize = 2
qosFlagsSize = 12
default:
qOsSize = 0
}
payloadSize := len(msg.Payload())
remainingContent := topicSize + qOsSize + payloadSize
if remainingContent < 1<<7 {
remainingLength = 1
} else if remainingContent < 1<<14 {
remainingLength = 2
} else if remainingContent < 1<<21 {
remainingLength = 3
} else {
remainingLength = 4
}
publishMessageSize := remainingLength + remainingContent + 1
byteCount := publishMessageSize + qosFlagsSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes. They will be useful!

Should this only be reporting the payload size instead of the whole message size? The message has been parsed by now.

The rest of the code here seems to be making an educated guess on what the actual message size was at a lower level. I'm concerned that this isn't correct and there's no way to easily check it. I also wonder if the mqtt module will change and make this inaccurate in the future.

Is there a way to get the message size directly from eclipse/paho.mqtt.golang? If not, could we look into adding it upstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the message has already been parsed, what I did to get the total incoming message size was work backwards from the given topic, payload, and flags. I don't want just the payload size, I want to get the total incoming packet size in order to know how much data usage a client is using. I was recommended to go to the eclipse/paho.mqtt.golang community to see if there was a way to get the message size directly from there, and the technique I implemented here was based off of their suggestion here. I suppose it could be possible to add this metric directly to eclipse/paho.mqtt.golang but what would be the advantages of doing it there rather than here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A message in that thread illustrates my concern:

... and if it's MQTT v5 then you need to add some more on to account for properties, but you won't absolutely get all the information that you need to correctly calculate the size in a user library."

The calculations here may be accurate for your use case, but evidently they're not accurate for all use cases (at least not v5) and it may not be possible to know when they're accurate.

I do think the payload size and number of messages are great internal metrics because they're directly measured, not derived.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. So would you recommend I change it to just collect the payload size and number of messages metrics rather than trying to get the total size of the message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything preventing this from being merged if we remove the code to calculate the total size. Maybe we should go ahead with that, then if/when we find a way to directly measure the total size we can add it in another PR.

@telegraf-tiger
Copy link
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_amd64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

🥳 This pull request decreases the Telegraf binary size by -1.86 % for linux amd64 (new size: 147.6 MB, nightly size 150.4 MB)

📦 Click here to get additional PR build artifacts

Artifact URLs

DEB RPM TAR GZ ZIP
amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip
arm64.deb armel.rpm darwin_arm64.tar.gz windows_i386.zip
armel.deb armv6hl.rpm freebsd_amd64.tar.gz
armhf.deb i386.rpm freebsd_armv7.tar.gz
i386.deb ppc64le.rpm freebsd_i386.tar.gz
mips.deb riscv64.rpm linux_amd64.tar.gz
mipsel.deb s390x.rpm linux_arm64.tar.gz
ppc64el.deb x86_64.rpm linux_armel.tar.gz
riscv64.deb linux_armhf.tar.gz
s390x.deb linux_i386.tar.gz
linux_mips.tar.gz
linux_mipsel.tar.gz
linux_ppc64le.tar.gz
linux_riscv64.tar.gz
linux_s390x.tar.gz
static_linux_amd64.tar.gz

@sspaink sspaink merged commit 1dc617e into influxdata:master Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/mqtt feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get incoming message size from MQTT
5 participants