1

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.

Maxim
  • 4,075
  • 1
  • 14
  • 23
rish0097
  • 1,024
  • 2
  • 18
  • 39

4 Answers4

3

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.

Daniel Collins
  • 752
  • 3
  • 14
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])
Steeve
  • 385
  • 2
  • 13
1

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.

Maxim
  • 4,075
  • 1
  • 14
  • 23
  • Okay but I have multiple subscriptions within that topic...that would mean I would have to query every subscription. Is it possible to query all subscriptions in the same request? – rish0097 Dec 04 '18 at 11:56
1

Improving Steeve's answer, i found out that you need to specify the end_time to get the most current data. In my case data was only returned for 4 hours ago. when i added end_time=datetime.now() it returned the most current data.

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

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

print(result['pubsub_subscription'][project]['subscription_name1'][0])
print(result['pubsub_subscription'][project]['subscription_name2'][0])