lonkoenig
lonkoenig

Reputation: 42

How to check the quota limit in Google Cloud Vision API?

Is there an API to see how many calls you've made this billing cycle to the Google Cloud Vision API? I would like to add this information to a UI so user's know when they're about to make queries that will be charged.

Upvotes: 1

Views: 1675

Answers (1)

Guillem Xercavins
Guillem Xercavins

Reputation: 7058

The easiest way is to use the API page on the Console. This will show the total count of requests. Take into account that you can edit the URL to match the desired number of days. For example, as today is April 9th and there is no 9 Days button on the UI I edited the URL parameter as &duration=P9D. The result is:

enter image description here

This can also be done programmatically using Stackdriver Monitoring but it's not straightforward. The serviceruntime metrics collect resource usage for Google APIs. If you navigate to the Metrics Explorer in SD Monitoring and start typing serviceruntime in the textbox you then can select the api/request_count metric type:

enter image description here

Then, in the filter option you can select Vision API metrics with service=vision.googleapis.com. Note that you can also filter and aggregate by other parameters such as the request method or response code. This will display the number of requests to Vision API (4 in my case):

enter image description here

However, this does not fit your use case, as this is a DELTA metric and not a CUMULATIVE one (Metric Kinds). You need to add up all requests to obtain the total number. As I'm not so familiar with custom metrics, my idea was to look into the underlying API call and aggregate all the data points in the time series with a bash script.

First, we need the Project ID and Access Token:

TOKEN=$(gcloud auth application-default print-access-token)
PROJECT=$(gcloud config get-value project 2>\dev\null)

And also we'll need to provide the initial and final timestamps in TimeInterval format. We are interested in up-to-date statistics so the end time will be the current one (credit). For the initial time we just need to parse the current year and month as day and time will be known:

NOW=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
AUX=-01T00:00:00Z
START_MONTH=$(echo $NOW | cut -c1-7)$AUX

Take into account that you might need to adjust that for when quotas are actually refilled, though. Then you can proceed to call the SD Monitoring API (modify the filter for different options or APIs other than Vision):

curl "https://monitoring.googleapis.com/v3/projects/$PROJECT/timeSeries?filter=metric.type%3D%22serviceruntime.googleapis.com%2Fapi%2Frequest_count%22%20AND%20resource.labels.service%3D%22vision.googleapis.com%22&interval.endTime=$NOW&interval.startTime=$START_MONTH" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Accept: application/json" \
  --compressed > result.txt

You can change the filter properties for metrics for other services. Saving the result into an intermediate file is not necessary but helps visualize it. The response will contain a timeSeries object with the request count in the value field of each data point:

{
  "timeSeries": [
    {

      ...

      },
      "metricKind": "DELTA",
      "valueType": "INT64",
      "points": [
        {
          "interval": {
            "startTime": "2018-04-08T11:20:48.224Z",
            "endTime": "2018-04-08T11:21:48.224Z"
          },
          "value": {
            "int64Value": "1"
          }
        },
        {
          "interval": {
            "startTime": "2018-04-08T11:18:18.224Z",
            "endTime": "2018-04-08T11:19:18.224Z"
          },
          "value": {
            "int64Value": "2"
          }
        },
        {
          "interval": {
            "startTime": "2018-04-08T11:18:08.224Z",
            "endTime": "2018-04-08T11:19:08.224Z"
          },
          "value": {
            "int64Value": "1"
          }
        }
      ]
    }
  ]
}

Then, you can parse the results to add up those values using jq or, in my case, bash scripting (credit):

cat result.txt | grep int64Value | cut -d '"' -f4 | awk '{s+=$1} END {printf "%.0f\n", s}'

This will output 4, as expected according to the cumulative request count in my case

Upvotes: 5

Related Questions