# 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](<https://devfeed.tech/sources/engineering-at-premise-medium.md>)

Topics: [Google Cloud Platform (GCP)](<https://devfeed.tech/topics/google-cloud.md>), [cost-optimization](<https://devfeed.tech/topics/cost-optimization.md>), [Cloud Monitoring](<https://devfeed.tech/topics/cloud-monitoring.md>), [Cloud Functions](<https://devfeed.tech/topics/cloud-functions.md>), [Slack](<https://devfeed.tech/topics/slack.md>), [function as a service](<https://devfeed.tech/topics/function-as-a-service.md>)

Tags: [alerting](<https://devfeed.tech/tags/alerting.md>), [api](<https://devfeed.tech/tags/api.md>), [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](<https://devfeed.tech/tags/cost.md>), [cost-savings](<https://devfeed.tech/tags/cost-savings.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [cron](<https://devfeed.tech/tags/cron.md>), [gcp](<https://devfeed.tech/tags/gcp.md>), [metrics](<https://devfeed.tech/tags/metrics.md>), [monitor](<https://devfeed.tech/tags/monitor.md>), [python](<https://devfeed.tech/tags/python.md>), [slack](<https://devfeed.tech/tags/slack.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>)

## AI overview

The tutorial describes a weekly Python Cloud Function that checks Cloud Run CPU and memory utilization and sends service owners Slack alerts when usage stays below configured thresholds, indicating a possible downscale. Its MQL queries group metrics by service and location and use the 99th percentile over a one-week interval; additional checks skip services already at minimum thresholds. The function uses the Metrics API, goblet, and goblet-gcp-client, with Cloud Scheduler triggering the weekly run. Deployment requires configuring a Slack channel and token, service account, thresholds, and cron expression, then installing dependencies and running goblet deploy. The excerpt says other resource types can be monitored with different MQL queries.

## 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