如何使用 python 客户端库获取 Pubsub 中存在的未送达消息数(指标 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 客户端库 [=18] =].

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])