问题
I would like to perform an action once all the messages from a pubsub topic have been acknowledged. I tried using Stackdriver monitoring API for the metric "Number of unacknowledged messages broken down by a cloud region" but did not understand the filter for region and why it is required. Where can I see what region my topics use? And for some unknown reason, for some of the topics, the API call fails for no reason at all. What is the best way of knowing if all messages have been acknowledged or not.
回答1:
A Cloud Pub/Sub Topic has no concept of an unacknowledged message. This is purely a property of an individual Subscription, how many messages have not yet been acknowledged.
You can use Stackdriver Alerting to alert you if the unacknowledged message age in any subscription gets too high. Set Resource Type to “Cloud Pub/Sub Subscription” and Metric to “Oldest Unacked Message”, and alert if any time series violates some threshold.
回答2:
This might help if you're looking into a programmatic way to achieve this:
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_name1'][0])
print(result['pubsub_subscription'][project]['subscription_name2'][0])
回答3:
Try using the metric subscription/num_undelivered_messages
instead. Unlike subscription/num_unacked_messages_by_\region
, it is in GA and does not require a region to be specified.
来源:https://stackoverflow.com/questions/53562350/count-number-of-unacknowledged-messages-in-a-pubsub-topic