获取 Google Cloud PubSub 中单条消息的大小
Get the size of a single message in Google Cloud PubSub
我有一个将消息发布到 Google Cloud PubSub 服务的设置。
我希望获得发布到 PubSub 的每条消息的大小。因此,为此,我确定了以下方法(注意:我使用 Python 客户端进行发布和订阅,按照其文档中介绍的逐行实现):
- 使用“Monitoring”功能从 Google 云控制台查看消息计数
- 创建一个 pull subscription client 并在回调函数中使用
message.size
查看从所请求主题中提取的消息的大小。
- 估计 publishing by converting them to JSON as per the PubSub message schema 之前和使用
sys.getsizeof()
的邮件大小
对于我使用 Python 发布者客户端发布的示例消息,如下所示:
{
"data": 'Test_message',
"attributes": {
'dummyField1': 'dummyFieldValue1',
'dummyField2': 'dummyFieldValue2'
}
}
,我从订阅客户端中的以下回调函数得到的大小为 101 作为 message.size
输出:
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()
而云控制台监控上显示的大小约为 79 B。
所以这些是我的问题:
- 为什么同一条消息的大小不同?
message.size
的输出是字节数吗?
- 如何在使用 python 客户端发布之前查看消息的大小?
- 如何在 Cloud Console 上查看单个消息的大小,而不是在给定时间范围内的聚合大小度量(我可以在“监控”部分中找到)?
为了进一步为社区做贡献,我总结了我们的讨论作为答案。
- 关于
message.size
,它是订阅者客户端消息中的一个属性。另外,根据documentation,其定义为:
Returns the size of the underlying message, in bytes
因此在发布之前您将无法使用它。
- 相反,
message_size
是 Google Cloud Metrics 中的一个指标,由 Cloud Monitoring 使用,here。
最后,讨论的最后一个主题是您的目标是监控您的配额支出,以便您可以继续使用免费套餐。因此,最好的选择是使用 Cloud Monitoring 并根据 pubsub.googleapis.com/topic/byte_cost
等指标设置警报。这里有一些链接,您可以在其中找到更多相关信息:Quota utilisation, Alert event based, Alert Policies.
关于您关于发布前查看消息大小的第三个问题,计费消息大小是消息数据、属性(键加值)、20 字节的时间戳和一些字节的总和 message_id
。请参阅 Cloud Pub/Sub Pricing guide. Note that the minimum of 1000 bytes is billable regardless of message size so if your messages may be smaller than 1000 bytes it’s important to have good batch settings. The message_id
is assigned server-side and is not guaranteed to be a certain size but it is returned by the publish call as a future so you can see examples. This should allow you to get a pretty accurate estimate of message cost within the publisher client. Note that you can also use the monitoring client library 从 Python 客户端中读取 Cloud Monitoring 指标。
关于你的第四个问题,没有办法从分布指标中提取单个数据点(除非你在查询的时间段内只发布了一条消息,在这种情况下,平均值会告诉你那条消息的大小消息)。
我有一个将消息发布到 Google Cloud PubSub 服务的设置。
我希望获得发布到 PubSub 的每条消息的大小。因此,为此,我确定了以下方法(注意:我使用 Python 客户端进行发布和订阅,按照其文档中介绍的逐行实现):
- 使用“Monitoring”功能从 Google 云控制台查看消息计数
- 创建一个 pull subscription client 并在回调函数中使用
message.size
查看从所请求主题中提取的消息的大小。 - 估计 publishing by converting them to JSON as per the PubSub message schema 之前和使用
sys.getsizeof()
的邮件大小
对于我使用 Python 发布者客户端发布的示例消息,如下所示:
{
"data": 'Test_message',
"attributes": {
'dummyField1': 'dummyFieldValue1',
'dummyField2': 'dummyFieldValue2'
}
}
,我从订阅客户端中的以下回调函数得到的大小为 101 作为 message.size
输出:
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()
而云控制台监控上显示的大小约为 79 B。
所以这些是我的问题:
- 为什么同一条消息的大小不同?
message.size
的输出是字节数吗?- 如何在使用 python 客户端发布之前查看消息的大小?
- 如何在 Cloud Console 上查看单个消息的大小,而不是在给定时间范围内的聚合大小度量(我可以在“监控”部分中找到)?
为了进一步为社区做贡献,我总结了我们的讨论作为答案。
- 关于
message.size
,它是订阅者客户端消息中的一个属性。另外,根据documentation,其定义为:
Returns the size of the underlying message, in bytes
因此在发布之前您将无法使用它。
- 相反,
message_size
是 Google Cloud Metrics 中的一个指标,由 Cloud Monitoring 使用,here。
最后,讨论的最后一个主题是您的目标是监控您的配额支出,以便您可以继续使用免费套餐。因此,最好的选择是使用 Cloud Monitoring 并根据 pubsub.googleapis.com/topic/byte_cost
等指标设置警报。这里有一些链接,您可以在其中找到更多相关信息:Quota utilisation, Alert event based, Alert Policies.
关于您关于发布前查看消息大小的第三个问题,计费消息大小是消息数据、属性(键加值)、20 字节的时间戳和一些字节的总和 message_id
。请参阅 Cloud Pub/Sub Pricing guide. Note that the minimum of 1000 bytes is billable regardless of message size so if your messages may be smaller than 1000 bytes it’s important to have good batch settings. The message_id
is assigned server-side and is not guaranteed to be a certain size but it is returned by the publish call as a future so you can see examples. This should allow you to get a pretty accurate estimate of message cost within the publisher client. Note that you can also use the monitoring client library 从 Python 客户端中读取 Cloud Monitoring 指标。
关于你的第四个问题,没有办法从分布指标中提取单个数据点(除非你在查询的时间段内只发布了一条消息,在这种情况下,平均值会告诉你那条消息的大小消息)。