Search code examples
google-cloud-platformgoogle-cloud-metrics

Google Cloud Metrics - get pub/sub metric filtered by subscription_id


Using GCP and having following data in Metrics explorer:

enter image description here

I need using Java client to get a current number of undelivered_messages in the specific subscription_id.

Found following example: https://cloud.google.com/monitoring/custom-metrics/reading-metrics

ListTimeSeriesRequest.Builder requestBuilder =
          ListTimeSeriesRequest.newBuilder()
              .setName(name.toString())
              .setFilter(
                  "metric.type=\"pubsub.googleapis.com/subscription/num_undelivered_messages\" AND metric.label.subscription_id = \"myexporter\"")
              .setInterval(interval)
              .setView(ListTimeSeriesRequest.TimeSeriesView.HEADERS);

Which brings error:

Caused by: io.grpc.StatusRuntimeException: NOT_FOUND: The metric referenced by the provided filter is unknown. Check the metric name and labels.

But it's not clear:

  • how to filter data by subscription_id
  • how to get not TimeSeries data, but current value

Solution

    1. For your first question replace metric.label.subscription_id by resource.label.\"subscription_id\" in the setFilter

    2. For your second question, you can play with the documentation example here : make sure to have a time interval of at least 60s (61 seconds worked for me). Any way get the last value sorted by end time