Skip to content

Instantly share code, notes, and snippets.

@sha1n
Last active January 26, 2023 09:56
Show Gist options
  • Save sha1n/ca82ed3ae024b6e8dce78eb155bdbb0b to your computer and use it in GitHub Desktop.
Save sha1n/ca82ed3ae024b6e8dce78eb155bdbb0b to your computer and use it in GitHub Desktop.
Google Custom Search API Quota Alert MQL
fetch consumer_quota
# Only fetch the quota usage metric
| metric 'serviceruntime.googleapis.com/quota/rate/net_usage'
# Only count custom search API requests
| filter
(metric.method == 'google.customsearch.v1.CustomSearchService.List'
&& metric.quota_metric == 'customsearch.googleapis.com/requests')
# Map 'yyyymmdd' formatted date field in PST timezone to column 'Date'
| map
add[
Date:
end().timestamp_to_string("%Y%m%d", "America/Los_Angeles")
.string_to_int64()]
# Group by Date, sum with sliding 24h sliding window
| group_by [Date], sliding(24h), [s: sum(val(0))]
| every 1m
# Calculate percent value based on 10K quota
| value (val() / 10000)*100 '%'
# Return only the last Date
| top 1, Date
| condition val() >= 80 '%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment