Search code examples
google-cloud-platformyoutube-apiquota

Get actual Quota Usage of Google Service in Google Cloud API (YouTube Data API)


I was wondering if there is a way to fetch the actual quota usage of a Google Cloud Service via the Google API like the information provided in the Cloud Console.

Google Cloud Console dashboard showing quota usages for the YouTube Data API service

As far as I've researched there are multiple APIs regarding this topic:

Service Usage API

This API only lists services and allows enabling/disabling services.

Cloud Quotas API

These endpoints only allow viewing (and modifying) the Quota limits. Going by the screenshot above, values 50k, 1.8m, 180k.

Cloud Metrics API

Would be the closest candidate but haven't figured out how to configure the permissions yet (Permission monitoring.monitoredResourceDescriptors.list denied) and it looks like those endpoints would only deliver data from their infrastructure products like Compute, Storage, Data Analytics, etc.

Did anyone manage to get actual usage metrics / remaining quota counts for a Google Cloud service - here especially for the YouTube Data API? Unfortunately Google also doesn't send any Rate-Limit headers with their API responses.

Thanks!!


Solution

  • I just passed through all you described above, and finally succeed:

    1. You need to add the Cloud Monitoring API to your app to get access to the monitoring.googleapis.com domain (listed as Stackdriver Monitoring API in the API list).
    2. You need to enable the billing on your app https://console.developers.google.com/billing/enable by entering a credit card as the Cloud Monitoring API is an Entreprise product and is not free anymore after a certain amount of requests (> 1Million request so you will be probably fine).
    3. You need then with your app to query https://monitoring.googleapis.com/v3/projects/{YourProjectId}/timeSeries route using an interval.startTime and a interval.endTime interval parameter (I'm personally using start and end of the day based on my current GMT+ diff as the quota is reset every day). {YourProjectId} is displayed when you go in Google Cloud Console and select your project in the drop box top left. The most difficult part of all of that is the last parameter named filter that took me a big while to figure out: 'metric.type="serviceruntime.googleapis.com/quota/rate/net_usage" AND resource.labels.service="youtube.googleapis.com"',
    4. The response of this query will then contains all points you can see on your dashboard "quota used graph", you just need to sum their value up to get the current YoutubeApi quota.

    Google console + App console comparaison

    Note: Keep in mind that the value you get from their API is the one at the moment you make the query, if you have parallels calls, you will probably get a wrong value. I'm personally using this call just to help sync the quota reset on the beginning of the day.

    Sources:

    1. https://cloud.google.com/monitoring/api/v3/filters#metric-descriptor-filter
    2. https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list