How to create GCP Monitoring Alert

2022/03/183 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Requirements
  3. Use Correct GCP Project
  4. Create Alerting Policy for Pub/Sub Topic
  5. Create Alerting Policy for Failed Scheduled Queries
  6. Conclusion

Introduction

This blog shows how to create monitoring alerts with GCP and provides a few examples.

Requirements

  • Have a GCP account
  • Already downloaded and set up gcloud

Use Correct GCP Project

Run the gcloud config list command to check whether the current account or project is the correct one for your GCP project.

gcloud  config list

In case the GCP project is not the correct one, run gcloud init and follow the steps to set the correct GCP project.

gcloud init

Create Alerting Policy for Pub/Sub Topic

The task here is: for some reason we want to know whenever a Pub/Sub topic gets published, and we want to receive an email as well.

We'll create a GCP monitoring policy for a specific Pub/Sub topic by using a PowerShell command. The parameters $topic and $project can be replaced with your own project settings. The idea here is to monitor a specific Pub/Sub topic, and if any message has been published, then the --if=>0 condition kicks in and will send a 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

Create Alerting Policy for Failed Scheduled Queries

Here, we want to send a notification when scheduled queries fail. First is the filter — here we use the metric to watch the scheduled query and check 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

Conclusion

GCP Monitoring alerts are a powerful way to stay informed about your cloud infrastructure. By creating alerting policies for Pub/Sub topics and BigQuery scheduled queries, you can quickly detect and respond to issues in your GCP environment.