How to Create Alert in Cloud Monitoring in GCP
To create an alert in
Google Cloud Monitoring, go to the Alerting section in the Cloud Console, click Create Policy, add conditions based on metrics, and set notification channels. This setup lets you get notified when your cloud resources meet specific criteria.Syntax
Creating an alert policy in GCP involves these parts:
- Alert Policy: The main container for your alert rules.
- Conditions: Define when the alert triggers, based on metrics or logs.
- Notification Channels: Where alerts are sent (email, SMS, Slack, etc.).
- Documentation: Optional notes to explain the alert.
bash
gcloud alpha monitoring policies create \ --notification-channels="CHANNEL_ID" \ --condition-display-name="High CPU Usage" \ --condition-filter="metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.type=\"gce_instance\"" \ --condition-aggregations-alignment-period="60s" \ --condition-aggregations-per-series-aligner="ALIGN_MEAN" \ --condition-comparison="COMPARISON_GT" \ --condition-threshold-value=0.8 \ --condition-duration="300s" \ --display-name="CPU Usage Alert"
Example
This example creates an alert policy that triggers when a Compute Engine instance's CPU usage is above 80% for 5 minutes. It sends notifications to a specified email channel.
bash
gcloud alpha monitoring policies create \ --notification-channels="projects/your-project/notificationChannels/1234567890" \ --condition-display-name="High CPU Usage" \ --condition-filter="metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.type=\"gce_instance\"" \ --condition-aggregations-alignment-period="60s" \ --condition-aggregations-per-series-aligner="ALIGN_MEAN" \ --condition-comparison="COMPARISON_GT" \ --condition-threshold-value=0.8 \ --condition-duration="300s" \ --display-name="CPU Usage Alert"
Output
Created alerting policy: projects/your-project/alertPolicies/1234567890
Common Pitfalls
Common mistakes when creating alerts in GCP Monitoring include:
- Using incorrect metric filters that do not match your resource type.
- Not setting a proper duration, causing alerts to trigger too often or never.
- Forgetting to add notification channels, so alerts are created but no one is notified.
- Using the wrong alignment period or aligner, which affects how data points are averaged.
bash
## Wrong filter example (missing resource type): gcloud alpha monitoring policies create \ --notification-channels="CHANNEL_ID" \ --condition-filter="metric.type=\"compute.googleapis.com/instance/cpu/utilization\"" \ --condition-duration="300s" \ --display-name="Bad Alert" ## Correct filter example: gcloud alpha monitoring policies create \ --notification-channels="CHANNEL_ID" \ --condition-filter="metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.type=\"gce_instance\"" \ --condition-duration="300s" \ --display-name="Good Alert"
Quick Reference
- Alert Policy: Group of alert conditions.
- Condition Filter: Metric and resource type to watch.
- Threshold: Value that triggers alert.
- Duration: How long condition must hold.
- Notification Channels: Where alerts are sent.
Key Takeaways
Create alert policies in GCP Monitoring by defining conditions and notification channels.
Use correct metric filters and resource types to ensure alerts trigger properly.
Set a reasonable duration to avoid noisy or missed alerts.
Always add notification channels to receive alert messages.
Test alerts with real metrics to confirm they work as expected.