How to create GCP Monitoring Alert
Table of Contents
- Introduction
- Requirements
- Use correct GCP Project
- Create alerting policy to Pub/Sub topic
- Create alerting policy to failed scheduled queries
- Conclusion
Introduction
This blog shows how to create monitoring alert with GCP and provide few examples.
Requirements
- Has GCP account
- Already download and setup gcloud
Use correct GCP Project
run gcloud config list
command to check current account or project is the one to run gcp project or not.
gcloud config list
In case if gcp project is not the one, run the gcloud init
and follow the steps to set to the correct gcp project.
gcloud init
Monitoring 1 - Create alerting policy to Pub/Sub topic
The task here is, by some reason we want to know whenever Pub/Sub got published, we want to receive an email as well.
We'll create GCP monitoring policy to specific Pub/Sub Topic by using PowerShell command.
The parameter $topic
and $project
can be replace with your own project settings, the idea here is trying to monitor the specific Pub/Sub topic if any of the topic had published then the --if=>0
kick in and will send notification.
$filter = 'resource.type = \"pubsub_topic\" AND (resource.labels.topic_id = \"{topic}\" AND resource.labels.project_id = \"{project}\") AND metric.type = \"pubsub.googleapis.com/topic/send_request_count\"';
$aggregation = '{"alignmentPeriod": "300s","perSeriesAligner": "ALIGN_MEAN"}';
$filter = $filter -replace "{topic}", $topic
$filter = $filter -replace "{project}", $project
$conditionName = "Condition - " + $topic;
$displayName = "Display Name - " + $topic;
$document = "Failed!" + $topic;
$channel = 123456; // replace with your notification channel id.
gcloud alpha monitoring policies create --if='> 0' --condition-filter=$filter --aggregation=$aggregation --duration='0s' --condition-display-name=$conditionName --notification-channels=$channel --combiner='OR' --enabled --trigger-count=1 --display-name=$displayName --documentation=$document
Monitoring 2 - Craete monitoring policy when BQ Shedule Queries failed to run
Here, we want to notify when schedule queries failed. First is the filter, here we use the metric to watch the scheduled query and watch if the state is FAILED.
$filter = 'resource.type = \"bigquery_dts_config\" AND metric.type = \"bigquerydatatransfer.googleapis.com/transfer_config/completed_runs\" AND metric.labels.completion_state = \"FAILED\"';
$aggregation = '{"alignmentPeriod": "300s","perSeriesAligner": "ALIGN_MEAN"}';
$conditionName = "Condition Name";
$displayName = "Display Name";
$document = "Failed!";
$channel = 123456; // replace with your notification channel id.
gcloud alpha monitoring policies create --if='> 0' --condition-filter=$filter --aggregation=$aggregation --duration='0s' --condition-display-name=$conditionName --notification-channels=$channel --combiner='OR' --enabled --trigger-count=1 --display-name=$displayName --documentation=$document