Enabling user-defined custom metrics for Horizontal Pod autoscalingStay organized with collectionsSave and categorize content based on your preferences.
Once these steps are done, all the user application logs are sent to Cloud Logging.
The next step is to annotate the user application for metrics collection.
Annotate a user application for metrics collection
To annotate a user application to be scraped and the logs sent to Cloud Monitoring, you must add correspondingannotationsto the metadata for the service, Pod, and endpoints.
In this section, you deploy a sample application with both logs and prometheus-compatible metrics.
Save the following Service and Deployment manifests to a file namedmy-app.yaml. Notice that the Service has the annotationprometheus.io/scrape: "true":
The Pods type metric has a default metric selector for the labels of the target Pods, which is how kube-controller-maneger works. In this example, you cna query the example_monitoring_up metric with a selector of{matchLabels: {app: example-monitoring}}as they are available in the target Pods. Any other selector specified is added to the list. To avoid the default selector, you canremove any labels on the target Pod or use the Object type metric.
Check that the user-defined application metrics are used by HPA
Check that the user defined application metrics are used by HPA:
Name: example-monitoring-hpa
Namespace: default
Labels:Annotations: CreationTimestamp: Mon, 19 Jul 2021 16:00:40 -0800
Reference: Deployment/example-monitoring
Metrics: ( current / target )
"example_monitoring_up" on pods: 1 / 20
Min replicas: 1
Max replicas: 5
Deployment pods: 1 current / 1 desired
Conditions:
Type Status Reason Message
AbleToScale True ReadyForNewScale recommended size matches current size
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from pods metric example_monitoring_up
ScalingLimited False DesiredWithinRange the desired count is within the acceptable range
Costs
Using custom metrics for HPA does not incur any additional charges. Users are charged only for application metrics and logs. SeeGoogle Cloud's operations suite pricingfor details. The Pod for enabling custom metrics consumes an extra 15m CPU and 20MB memory.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-12-17 UTC."],[],[]]