Setting Up Anomaly Detection with Dynamic Thresholds in GCP Cloud Monitoring

Setting Up Anomaly Detection with Dynamic Thresholds in GCP Cloud Monitoring

Setting Up Anomaly Detection with Dynamic Thresholds in GCP Cloud Monitoring

Setting Up Anomaly Detection with Dynamic Thresholds in GCP Cloud Monitoring

Setting Up Anomaly Detection with Dynamic Thresholds in GCP Cloud Monitoring

Blog Summary

Most production workloads do not run at a steady load. Traffic rises during business hours, drops at night, and changes again during batch jobs, releases or seasonal peaks. A fixed rule such as “alert when CPU is above 80%” does not account for any of this.

Blog Details

Why Static Alerts Are No Longer Enough 

Most production workloads do not run at a steady load. Traffic rises during business hours, drops at night, and changes again during batch jobs, releases or seasonal peaks. A fixed rule such as “alert when CPU is above 80%” does not account for any of this. 


In our managed services work, we usually see two problems with static thresholds: 

  • Too many alerts on normal behaviour: A VM that reaches 85% CPU every evening during peak hours raises the same alert every day, and the operations team soon learns to ignore it. 

  • Missed alerts on real issues: A VM that normally runs at 10–15% CPU and suddenly sits at 50% is clearly behaving abnormally, yet a static 80% rule will never flag it. 

Dynamic thresholds solve both problems. Instead of comparing a metric against a fixed number, we compare it against how that resource normally behaves. 


What is Anomaly Detection Using Dynamic Thresholds & How It Works in GCP 

What it is: 

A dynamic threshold compares a metric’s recent value with its own history, not with a fixed number. For example, rather than “alert if CPU is above 80%”, the rule becomes “alert if CPU over the last 5 minutes is more than twice its recent average”. 


How it works in Google Cloud Monitoring: 

Cloud Monitoring supports this through PromQL-based alerting policies. PromQL conditions can reference any Cloud Monitoring metric, including Compute Engine system metrics, and support complex conditions such as ratios and dynamic thresholds. Other alerting policy types can only query 25 hours of data, but PromQL alerting policies can be configured to query up to 2 years of metric data. This long-lookback capability is currently in Preview [1][5]. 


The logic works in four stages: 

  1. Baseline: A longer time window (for example, the last hour or the last week) is averaged to show what “normal” looks like for that resource. 

  2. Current value: A short recent window (for example, the last 5 minutes) is averaged in the same way. 

  3. Comparison: The PromQL expression compares the two values using a multiplier, such as 2× the baseline. 

  4. Alert: When the comparison is true for the configured retest window, Cloud Monitoring creates an alert and sends notifications to the attached channels [2][3][4]. 


What Google Cloud Provides and What We Built 

Cloud Monitoring provides the PromQL alerting engine, the long-lookback query support, notification channels and automatic incident handling [1][3]. The dynamic threshold itself is not a ready-made setting. It is a PromQL expression that we wrote for this demo, which compares the 5-minute average CPU with the 1-hour average and fires when the current value is more than twice the baseline: 


avg_over_time(compute_googleapis_com:instance_cpu_utilization[5m]) 


> 2 * avg_over_time(compute_googleapis_com:instance_cpu_utilization[1h]) 


The same approach can be extended with other PromQL functions, such as standard deviation bands or week-over-week comparisons, but those are author-designed expressions and were not used in this demo. 


Benefits of Using Dynamic Thresholds 

  • Fewer false alarms: Regular daily or weekly peaks become part of the baseline, so they no longer trigger alerts. 

  • Earlier detection of unusual behaviour: Abnormal changes are caught even when the value stays well below a traditional static limit. 

  • One rule for many workloads: You set a multiplier instead of a separate threshold for every VM, so policies do not need retuning as workloads grow. 

  • Better context for responders: Notes added in the policy’s Documentation section appear in the alert email, so the on-call engineer has triage steps straight away [2]. 


Note: A new resource has no history yet, so its baseline is unreliable at first. Let the metric build some history before relying on the dynamic alert, or apply dynamic thresholds to aggregated metrics instead of individual new workloads. 

 
Real time Console steps 

All screenshots in this section were captured by the author in Quadra’s internal Google Cloud lab project, set up for this demonstration. Google Cloud Console screens are shown for instructional purposes only. 

 
1. Provision the Test Infrastructure 

 

2.Explore Metrics Baseline 

 

3.Construct the PromQL Dynamic Expression 
 


 

 
 
4.Configure Alerting & Notification Policy 




5.Simulate an Anomaly & Test Incident Response 
 

 
 
6.Verify and resolve the alert 

 

 
 
7.Mail Notification for the Alert 


 
8.Stop the CPU load 

 

 
9.Metric recovery and the incident automatically resolves 


Note: PromQL-based alerts do not use the standard auto-close duration. They close automatically after the condition has been false for the larger of 270 seconds or twice the evaluation interval [1]. 


References 


[1] PromQL-based alerting policies – Cloud Monitoring documentation: https://docs.cloud.google.com/monitoring/promql/promql-in-alerting 

[2] Create PromQL-based alerting policies (Console): https://docs.cloud.google.com/monitoring/promql/create-promql-alerts-console

[3] Alerting overview – Cloud Monitoring documentation: https://docs.cloud.google.com/monitoring/alerts

[4] Create and manage notification channels: https://docs.cloud.google.com/monitoring/support/notification-options

[5] Google Cloud Blog – Anomaly detection using dynamic thresholds and two-year-long alerts in Cloud Monitoring: https://cloud.google.com/blog/products/management-tools/cloud-monitoring-adds-long-lookback-alert-policies-for-promql/

Why Static Alerts Are No Longer Enough 

Most production workloads do not run at a steady load. Traffic rises during business hours, drops at night, and changes again during batch jobs, releases or seasonal peaks. A fixed rule such as “alert when CPU is above 80%” does not account for any of this. 


In our managed services work, we usually see two problems with static thresholds: 

  • Too many alerts on normal behaviour: A VM that reaches 85% CPU every evening during peak hours raises the same alert every day, and the operations team soon learns to ignore it. 

  • Missed alerts on real issues: A VM that normally runs at 10–15% CPU and suddenly sits at 50% is clearly behaving abnormally, yet a static 80% rule will never flag it. 

Dynamic thresholds solve both problems. Instead of comparing a metric against a fixed number, we compare it against how that resource normally behaves. 


What is Anomaly Detection Using Dynamic Thresholds & How It Works in GCP 

What it is: 

A dynamic threshold compares a metric’s recent value with its own history, not with a fixed number. For example, rather than “alert if CPU is above 80%”, the rule becomes “alert if CPU over the last 5 minutes is more than twice its recent average”. 


How it works in Google Cloud Monitoring: 

Cloud Monitoring supports this through PromQL-based alerting policies. PromQL conditions can reference any Cloud Monitoring metric, including Compute Engine system metrics, and support complex conditions such as ratios and dynamic thresholds. Other alerting policy types can only query 25 hours of data, but PromQL alerting policies can be configured to query up to 2 years of metric data. This long-lookback capability is currently in Preview [1][5]. 


The logic works in four stages: 

  1. Baseline: A longer time window (for example, the last hour or the last week) is averaged to show what “normal” looks like for that resource. 

  2. Current value: A short recent window (for example, the last 5 minutes) is averaged in the same way. 

  3. Comparison: The PromQL expression compares the two values using a multiplier, such as 2× the baseline. 

  4. Alert: When the comparison is true for the configured retest window, Cloud Monitoring creates an alert and sends notifications to the attached channels [2][3][4]. 


What Google Cloud Provides and What We Built 

Cloud Monitoring provides the PromQL alerting engine, the long-lookback query support, notification channels and automatic incident handling [1][3]. The dynamic threshold itself is not a ready-made setting. It is a PromQL expression that we wrote for this demo, which compares the 5-minute average CPU with the 1-hour average and fires when the current value is more than twice the baseline: 


avg_over_time(compute_googleapis_com:instance_cpu_utilization[5m]) 


> 2 * avg_over_time(compute_googleapis_com:instance_cpu_utilization[1h]) 


The same approach can be extended with other PromQL functions, such as standard deviation bands or week-over-week comparisons, but those are author-designed expressions and were not used in this demo. 


Benefits of Using Dynamic Thresholds 

  • Fewer false alarms: Regular daily or weekly peaks become part of the baseline, so they no longer trigger alerts. 

  • Earlier detection of unusual behaviour: Abnormal changes are caught even when the value stays well below a traditional static limit. 

  • One rule for many workloads: You set a multiplier instead of a separate threshold for every VM, so policies do not need retuning as workloads grow. 

  • Better context for responders: Notes added in the policy’s Documentation section appear in the alert email, so the on-call engineer has triage steps straight away [2]. 


Note: A new resource has no history yet, so its baseline is unreliable at first. Let the metric build some history before relying on the dynamic alert, or apply dynamic thresholds to aggregated metrics instead of individual new workloads. 

 
Real time Console steps 

All screenshots in this section were captured by the author in Quadra’s internal Google Cloud lab project, set up for this demonstration. Google Cloud Console screens are shown for instructional purposes only. 

 
1. Provision the Test Infrastructure 

 

2.Explore Metrics Baseline 

 

3.Construct the PromQL Dynamic Expression 
 


 

 
 
4.Configure Alerting & Notification Policy 




5.Simulate an Anomaly & Test Incident Response 
 

 
 
6.Verify and resolve the alert 

 

 
 
7.Mail Notification for the Alert 


 
8.Stop the CPU load 

 

 
9.Metric recovery and the incident automatically resolves 


Note: PromQL-based alerts do not use the standard auto-close duration. They close automatically after the condition has been false for the larger of 270 seconds or twice the evaluation interval [1]. 


References 


[1] PromQL-based alerting policies – Cloud Monitoring documentation: https://docs.cloud.google.com/monitoring/promql/promql-in-alerting 

[2] Create PromQL-based alerting policies (Console): https://docs.cloud.google.com/monitoring/promql/create-promql-alerts-console

[3] Alerting overview – Cloud Monitoring documentation: https://docs.cloud.google.com/monitoring/alerts

[4] Create and manage notification channels: https://docs.cloud.google.com/monitoring/support/notification-options

[5] Google Cloud Blog – Anomaly detection using dynamic thresholds and two-year-long alerts in Cloud Monitoring: https://cloud.google.com/blog/products/management-tools/cloud-monitoring-adds-long-lookback-alert-policies-for-promql/

We are the strategic technology partner for the world's leading businesses, architecting the intelligent, secure, and resilient systems that transform ambition into lasting advantage.

ISO 9001

ISO 27001

ISO/IEC 42001

Get the Blueprint, Delivered

Subscribe to receive our latest insights on technology strategy and leadership.

Quadrasystems.net © All rights reserved

We are the strategic technology partner for the world's leading businesses, architecting the intelligent, secure, and resilient systems that transform ambition into lasting advantage.

ISO 9001

ISO 27001

ISO/IEC 42001

Get the Blueprint, Delivered

Subscribe to receive our latest insights on technology strategy and leadership.

Quadrasystems.net © All rights reserved

We are the strategic technology partner for the world's leading businesses, architecting the intelligent, secure, and resilient systems that transform ambition into lasting advantage.

ISO 9001

ISO 27001

ISO/IEC 42001

Get the Blueprint, Delivered

Subscribe to receive our latest insights on technology strategy and leadership.

Quadrasystems.net © All rights reserved