I have a setup where I am publishing messages to Google Cloud PubSub service.
I wish to get the size of each individual message that I am publishing to PubSub. So for this, I identified the following approaches (Note: I am using the Python clients for publishing and subscribing, following a line-by-line implementation as presented in their documentation):
- View the message count from the Google Cloud Console using the 'Monitoring' feature
- Create a pull subscription client and view the size using
message.sizein the callback function for the messages that are being pulled from the requested topic. - Estimate the size of the messages before publishing by converting them to JSON as per the PubSub message schema and using
sys.getsizeof()
For a sample message like as follows which I published using a Python publisher client:
{
"data": 'Test_message',
"attributes": {
'dummyField1': 'dummyFieldValue1',
'dummyField2': 'dummyFieldValue2'
}
}
, I get the size as 101 as the message.size output from the following callback function in the subcription client:
def callback(message):
print(f"Received {message.data}.")
if message.attributes:
print("Attributes:")
for key in message.attributes:
value = message.attributes.get(key)
print(f"{key}: {value}")
print(message.size)
message.ack()
Whereas the size displayed on Cloud Console Monitoring is something around 79 B.

So these are my questions:
- Why are the sizes different for the same message?
- Is the output of
message.sizein bytes? - How do I view the size of a message before publishing using the python client?
- How do I view the size of a single message on the Cloud Console, rather than a aggregated measure of size during a given timeframe which I could find in the Monitoring section?
Return the size of the underlying message, in bytes. Regarding your question about the value of message_sizes this metric means theDistribution of publish message sizes (in bytes). It is Sampled every 60 seconds. After sampling, data is not visible for up to 240 seconds, link. Could you tell me the reason you want to check the message size before publishinhg? - Alexandre Moraesmessage.sizeand 'message_sizes` (as mentioned above) satisfy your needs? - Alexandre Moraes