Google PubSub - 计算主题中的消息

Gil*_*rim 17 google-cloud-platform google-cloud-pubsub

我查看了Google PubSub的文档,并尝试查看Google Cloud Monitoring,但找不到任何方法来确定我的主题中的队列大小.

由于我计划使用PubSub进行分析,因此监控队列数非常重要,因此我可以扩大/减少用户数.

我错过了什么?

Kam*_*osn 20

您要查看的指标是"未送达的邮件".您应该能够在"发布/订阅"资源类型下设置在Google云监控中监控此指标的警报或图表.订阅者尚未确认的消息数(即队列大小)是每订阅度量标准,而不是每主题度量标准.有关度量的信息,请参阅pubsub.googleapis.com/subscription/num_undelivered_messagesGCP指标清单(以及其他所有可用的发布/订阅指标).

  • 我试图直接查询API并得到NotFound错误.https://pubsub.googleapis.com/v1/<name_of_subscription>/num_undelivered_messages这是一个网址.我错过了什么? (4认同)

Ste*_*eve 11

如果您正在寻找一种编程方式来实现这一点,这可能会有所帮助:

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])
Run Code Online (Sandbox Code Playgroud)


Mik*_* S. 8

您的问题的答案是"否",PubSub没有显示这些计数的功能.你必须这样做是通过使用Stackdriver的日志事件监控(我花了一些时间来找到它).

对此的口语答案是逐步完成以下操作:

  1. 从GCloud管理控制台导航到: Monitoring

从gcloud管理控制台导航

  1. 这将打开一个带有单独Stackdriver控制台的新窗口
  2. 在Stackdriver中导航:Dashboards>Create Dashboard

在stackdriver中创建新的仪表板

  1. 单击Add Chart仪表板屏幕右上角的按钮

在此输入图像描述

  1. 在输入框中,键入num_undelivered_messages然后SAVE

自动建议添加图表的指标


nor*_*ree 7

根据@steeve 的回答更新版本。(无pandas依赖性)

请注意,您必须指定end_time而不是使用默认值utcnow()

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

project = 'my-project'
sub_name = 'my-sub'
client = monitoring_v3.MetricServiceClient()
result = query.Query(
  client,
  project,
  'pubsub.googleapis.com/subscription/num_undelivered_messages',
  end_time=datetime.datetime.now(),
  minutes=1,
  ).select_resources(subscription_id=sub_name)

for content in result:
  print(content.points[0].value.int64_value)
Run Code Online (Sandbox Code Playgroud)

  • 很好的工作,做得很好。我没有时间审查我提出的解决方案。您的工作将使社区受益 (2认同)