从 Python 访问 Google PubSub 中的 subscription/num_oustanding_messages 指标

Accessing the subscription/num_oustanding_messages metric in Google PubSub from Python

是否可以从 google-cloud-python 访问 https://cloud.google.com/monitoring/api/metrics#gcp-pubsub 列出的 subscription/num_outstanding_messages 指标?我使用类似的代码成功访问了 num_undelivered_messages 指标,但迭代以下查询(成功)的结果总是产生一个空列表。

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import monitoring
>>> client = monitoring.Client()
>>> q = client.query(
...     "pubsub.googleapis.com/subscription/num_undelivered_messages",
...     minutes=90
... )
>>> len(list(q)) > 0
True
>>> q = client.query(
...     "pubsub.googleapis.com/subscription/num_outstanding_messages",
...     minutes=90
... )
>>> len(list(q)) > 0
False

相关google-cloud-python库版本:

~:pip freeze | grep google
gapic-google-logging-v2==0.10.1
gapic-google-pubsub-v1==0.10.1
google-cloud==0.21.0
google-cloud-bigquery==0.21.0
google-cloud-bigtable==0.21.0
google-cloud-core==0.21.0
google-cloud-datastore==0.21.0
google-cloud-dns==0.21.0
google-cloud-error-reporting==0.21.0
google-cloud-happybase==0.20.0
google-cloud-language==0.21.0
google-cloud-logging==0.21.0
google-cloud-monitoring==0.21.0
google-cloud-pubsub==0.21.0
google-cloud-resource-manager==0.21.0
google-cloud-runtimeconfig==0.21.0
google-cloud-storage==0.21.0
google-cloud-translate==0.21.0
google-cloud-vision==0.21.0
google-gax==0.14.1
googleapis-common-protos==1.5.0
grpc-google-iam-v1==0.10.1
grpc-google-logging-v2==0.10.1
grpc-google-pubsub-v1==0.10.1

num_outstanding_messages表示发送给订阅者但尚未确认的消息数,仅适用于推送订阅者。请注意,尽管 API 没有明确区分这种区别,但在 Stackdriver, the metric shows up as "push outstanding messages." The Subscriber Guide 中讨论了推送订阅者和拉取订阅者之间的区别。