# Tutorial: Low Usage Alerting On Slack for Google Cloud Platform (GCP)

DevFeed: [Tutorial: Low Usage Alerting On Slack for Google Cloud Platform (GCP)](<https://devfeed.tech/articles/tutorial-low-usage-alerting-on-slack-for-google-cloud-platform-gcp-23882.md>)

Original publisher: [Read original article](<https://engineering.premise.com/tutorial-low-usage-alerting-on-slack-for-google-cloud-platform-gcp-cc68ac8ca4d?source=rss----c5fada0a103d---4>)

Author: Mauricio Martinez

Published: 2023-04-24T15:57:09Z

Content type: tutorial

Language: en

Sources: [Engineering at Premise - Medium](<https://devfeed.tech/sources/engineering-at-premise-medium.md>)

Topics: [Google Cloud Platform (GCP)](<https://devfeed.tech/topics/google-cloud.md>), [Cloud Run](<https://devfeed.tech/topics/cloud-run.md>), [Python](<https://devfeed.tech/topics/python.md>), [Monitoring](<https://devfeed.tech/topics/monitoring.md>), [Slack](<https://devfeed.tech/topics/slack.md>)

Tags: [cloud-computing](<https://devfeed.tech/tags/cloud-computing.md>), [cloud-run](<https://devfeed.tech/tags/cloud-run.md>), [cloudrun](<https://devfeed.tech/tags/cloudrun.md>), [cost-savings](<https://devfeed.tech/tags/cost-savings.md>), [deploy](<https://devfeed.tech/tags/deploy.md>), [gcp](<https://devfeed.tech/tags/gcp.md>), [github](<https://devfeed.tech/tags/github.md>), [google-cloud](<https://devfeed.tech/tags/google-cloud.md>), [metrics](<https://devfeed.tech/tags/metrics.md>), [monitoring](<https://devfeed.tech/tags/monitoring.md>), [python](<https://devfeed.tech/tags/python.md>), [slack](<https://devfeed.tech/tags/slack.md>)

## AI overview

This tutorial explains how to deploy a Python-based Cloud Function that monitors weekly CPU and memory usage for Google Cloud services, including Cloud Run, and sends Slack notifications to service owners when a service may be safely downscaled to reduce costs. It covers creating MQL queries, retrieving monitoring metrics, checking minimum thresholds, and deploying the scheduled function.

## Source excerpt

Google Cloud Platform (GCP) is a powerful cloud computing platform that allows businesses to run their applications and workloads with ease. However, as the number of services and applications increases, it becomes challenging to keep track of the usage of each service and ensure they are cost optimized. To address this issue, we can deploy a Python-based Cloud Function that will monitor the low usage of GCP services and notify service owners weekly via Slack that their service can be safely downscaled to minimize costs. This tutorial will walk you through setting up this entire flow in a few quick and easy steps, while allowing you to easily customize the service thresholds, Slack message ui, and the cron schedule. The code for this tutorial can be found on our gcp-tutorials GitHub repository. ArchitectureGet CPU/Memory Usage Metrics We can use the Metrics Explorer UI to build a MQL query to retrieve the desired data from the monitoring metrics API. In this tutorial we will monitor Cloud Run services, but you can monitor other resources by using a different MQL query. In our example we selected the CPU and Memory Cloud Run metrics: We group by service name and location and select the alignment for the 99th percentage to get the max usage over the 1 week duration. Then click on CODE EDITOR to generate a sample MQL query: fetch cloud_run_revision | metric 'run.googleapis.com/container/cpu/utilizations' | group_by 1w, [value_utilizations_percentile: percentile(value.utilizations, 99)] | every 1w | group_by [resource.service_name, resource.location], [value_utilizations_percentile_max: max(value_utilizations_percentile)]fetch cloud_run_revision | metric 'run.googleapis.com/container/memory/utilizations' | group_by 1w, [value_utilizations_percentile: percentile(value.utilizations, 99)] | every 1w | group_by [resource.service_name, resource.location], [value_utilizations_percentile_max: max(value_utilizations_percentile)]Code Once we have our MQL queries we will use the