繁体   English   中英

如何使用 python 客户端库获取 Pubsub 中存在的未传递消息(metric api)的数量?

[英]How can I get the number of undelivered messages (metric api) present in Pubsub using python client library?

我使用 Pubsub 作为排队机制工具,并想知道驻留在 Pubsub 主题内的消息计数。 出于同样的目的,我决定使用 Google API 指标pubsub.googleapis.com/subscription/num_undelivered_messages但我无法弄清楚如何使用 python 客户端库 monitoring_v3 来实现这一点。

from google.cloud import monitoring_v3
import time,os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/key.json"

client = monitoring_v3.MetricServiceClient()
project = 'project_name'
project_name = client.project_path(project)
metric_type = "pubsub.googleapis.com/subscription/num_undelivered_messages"

您能否指导我如何进一步进行并查询名为 num_undelivered_messages 的这个 google api 指标?

这对我有用,但我不确定这是否是创作者的意图。

from google.cloud import monitoring_v3
from google.cloud.monitoring_v3 import query

project = "..."
client = monitoring_v3.MetricServiceClient()
result = query.Query(
    client,
    project,
    'pubsub.googleapis.com/subscription/num_undelivered_messages',
    minutes=1).as_dataframe()

对于特定订阅,您可能需要以这种方式运行代码:

from google.cloud import monitoring_v3
from google.cloud.monitoring_v3 import query

project = "my-project"
client = monitoring_v3.MetricServiceClient()
result = query.Query(client,project,'pubsub.googleapis.com/subscription/num_undelivered_messages', minutes=60).as_dataframe()

print(result['pubsub_subscription'][project]['subscription_name'][0])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM