I'd like to get an alert when a cloud function returns non-OK status codes for more than 90% of executions over 30 minutes.
The series I'm filtering is cloudfunctions.googleapis.com/function/execution_count. Out of all records, I want to count those that satisfy metric.status != 'ok'. If 90% of all records over the last 30min satisfy this condition, I want to raise an alert.
I had a look at Google Cloud's Monitoring Query Language documentation and found this part about ratios: https://cloud.google.com/monitoring/mql/examples#qlx-ratio-ratio
Here's how I wrote my condition:
fetch cloud_function
| metric 'cloudfunctions.googleapis.com/function/execution_count'
| { filter metric.status != 'ok'
; ident }
| group_by [resource.function_name]
| ratio
| window 30m
| condition ratio >= 0.9 '1'
It seems to work, and the graphs indicate correct values. However, it seems that as soon as a function returns an error code, the alert is raised even if there are other successful executions along compensating for the problem.
For example, here's the graph of the metrics at the time an alert was raised, which I got in Monitoring > Alerting > (click on the last alert in the events section)

The function reported in the alert is processPurchase
Is there something I'm missing that could cause the problem? Looking at the graphs, it seems the alert should not trigger. Yet, I have events and notifications about the alert. It resolves a few minutes later.

