【发布时间】:2022-01-04 11:05:31
【问题描述】:
在使用 GCP Pub/Sub 时,我需要密切关注我的主题并检索未传递消息的数量。它与 Google Query Monitoring 的这个 sn-p 配合得很好:Link。
但我需要按属性对消息进行分组。每条消息都有一个带有参数的正文,例如:{'target':'A'},我真的需要这样的东西:
| msg.target | undelivered messages |
|---|---|
| A | 34 |
| B | 42 |
| C | 42 |
如果不使用消息,我无法成功访问它。
这是我的第一次尝试:
import json
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
project_id = "xxxx"
subscription_id = "xxxx"
subscription_path = subscriber.subscription_path(project_id, subscription_id)
response = subscriber.pull(
request={"subscription": subscription_path,"max_messages":9999}
)
ack_ids=[r.ack_id for r in response.received_messages]
subscriber.modify_ack_deadline(
request={
"subscription": subscription_path,
"ack_ids": ack_ids,
"ack_deadline_seconds": 0, ## The message will be immediatly 'pullable' again ?
}
)
messages = [ json.loads(r.message.data.decode()) for r in response.received_messages ]
for m in messages :
## Parse all messages to get my needed counts
但它运行得不是很好。我每次都会收到随机数量的消息,因此无法确定我在看什么。
所以我在这里进行实验。
我看到了 3 种方式:
- 也许可以直接从 Google Query Monitoring 访问消息正文属性?
- 也许我使用/解析/释放所有消息的方法没有正确写入,这就是它无法正常工作的原因?
- 也许我都错了,创建多个主题而不是在消息正文中保留属性会更有效,或者还有另一种方法可以“标记”消息以在监控中对它们进行分组?
你知道怎么做吗? 非常感谢您的帮助!
【问题讨论】:
标签: python google-cloud-platform google-cloud-pubsub google-cloud-monitoring