Stay organized with collections Save and categorize content based on your preferences.
This document describes how to create user-defined metrics and how to write this metric data by using the Cloud Monitoring API. User-defined metrics use the same elements that the built-in Cloud Monitoring metrics use:
User-defined metrics, sometimes called custom metrics, can be used in the same way as built-in metrics. That is, you can create charts and alerts for this metric data.
Log-based metrics are a class of user-defined metrics, but you can't create them by using the Cloud Monitoring API. Log-based metrics derive metric data from log entries, but the Monitoring API provides no way to specify how to extract metric data from log entries. Instead, use Cloud Logging to create log-based metrics. When you create a log-based metric, Logging creates the structures described in this document and sends the metric data to Cloud Monitoring for you. For information about creating log-based metrics, see the following documents:
To instrument your application, we recommend that you use a vendor-neutral instrumentation framework that is open source, such as OpenTelemetry, instead of vendor- and product-specific APIs or client libraries. For information about instrumenting your application, see Instrumentation and observability.
Note: User-defined metrics are a chargeable feature of Cloud Monitoring and there could be costs for these metrics. For pricing information, see the Cloud Monitoring sections of the Google Cloud Observability pricing page. Before you beginTo learn about the structures that underlie all metrics, see Metrics, time series, and resources.
To use Cloud Monitoring, you must have a Google Cloud project with billing enabled. When necessary, do the following:
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Verify that billing is enabled for your Google Cloud project.
To create a user-defined metric, you either define a MetricDescriptor
object that specifies various information about the metric, or you write metric data. When you write metric data, Monitoring creates the metric descriptor for you based on the structure of the data you provide. For information about designing a metric descriptor, see Metric descriptors for user-defined metrics.
If you write metric data when a metric descriptor for that user-defined metric doesn't yet exist, then a metric descriptor is created automatically. However, this new metric descriptor might not be exactly what you want; auto-creation of metric descriptors involves some assumptions and defaults.
Cloud Monitoring creates a new MetricDescriptor
when the TimeSeries
object included in a call to timeSeries.create
references a Metric
object that specifies a nonexistent metric-type name. Cloud Monitoring uses the following rules to populate the MetricDescriptor
:
type
: The type is copied from the Metric
object's type
field.name
: The name is created from the project ID in the method call and the value of the type
in the Metric
object.labels
: The labels that appear in the Metric
object. Each label descriptor in the new metric descriptor has the following fields:
key
: the label key in the Metric
object.valueType
: STRING
description
: not setmetricKind
: The metric kind is set to GAUGE
unless you specify the metricKind
parameter of the TimeSeries
object. When you specify the metricKind
, the new metric has that kind. You can specify only GAUGE
and CUMULATIVE
kinds.valueType
: The value type is taken from the typed value of the Point
being written. The value type must be BOOL
, INT64
, DOUBLE
, or DISTRIBUTION
. When you specify a value type in the valueType
field of the TimeSeries
, that type must match the type of the Point
.unit
: not setdescription
: "Auto created custom metric."
.displayName
: not setIn a single timeSeries.create
call, you can include multiple TimeSeries
objects that refer to the same nonexistent metric type. In that case, the labels in the new metric descriptor consist of the union of all the labels in the Metric
objects in all the time series in this call to create
.
Next step: See Write user-defined metrics.
Manual creation of metric descriptorsTo create a metric descriptor, do the following:
Determine the structure of your metric descriptor. For help in making these choices, you can browse the built-in metrics and look at their time series data:
Choose a metric name for your user-defined metric.
Choose a display name and description for your metric. The display name is used in the Google Cloud console.
Choose a project or projects in which to define your user-defined metric and write its time series data. When you need the same metric in several projects, make identical definitions of the metric in each project.
Determine the metric's kind, value type, and (optionally) units. Not all value types and metric kinds are supported for user-defined metrics. For more information on these fields, see Value types and metric kinds.
Choose the metric's labels—their names, value types, and descriptions.
Determine the monitored resources against which the metric data is written. Choose from the following list:
aws_ec2_instance
: Amazon EC2 instance.dataflow_job
: Dataflow job.gae_instance
: App Engine instance.gce_instance
: Compute Engine instance.generic_node
: User-specified computing node.generic_task
: User-defined task.gke_container
: GKE container instance.global
: Use this resource when no other resource type is suitable. For most use cases, generic_node
or generic_task
are better choices than global
.k8s_cluster
: Kubernetes cluster.k8s_container
: Kubernetes container.k8s_node
: Kubernetes node.k8s_pod
: Kubernetes pod.Create a MetricDescriptor
object, and then pass it as an argument to a call to the metricDescriptors.create
method.
It is usually an error to call metricDescriptors.create
using the same type name as an existing metric descriptor. However, if all the fields of the new MetricDescriptor
object match the fields of the existing descriptor exactly, then it isn't an error but it has no effect.
In the following example, you create a gauge metric.
ProtocolTo create a metric descriptor, use the metricDescriptors.create
method. You can execute this method by using the APIs Explorer widget on the method's reference page. See APIs Explorer for more information.
The following are the sample parameters to metricDescriptors.create
:
projects/[PROJECT_ID]
Request body: supply a MetricDescriptor
object such as the following:
{
"name": "",
"description": "Daily sales records from all branch stores.",
"displayName": "Sales",
"type": "custom.googleapis.com/stores/sales",
"metricKind": "GAUGE",
"valueType": "DOUBLE",
"unit": "{USD}",
"labels": [
{
"key": "store_id",
"valueType": "STRING",
"description": "The ID of the store."
},
],
}
Supply these values to the fields in the widget, using your project's ID in place of [PROJECT_ID
]:
Click the Execute button to run the method.
Note: To view the request as a curl command, as an HTTP request, or in JavaScript, click fullscreen Full screen in APIs Explorer.When creating a new metric, the name
field in the MetricDescriptor
is ignored and can be omitted. The create
method returns the new metric descriptor with the name
field filled in, which in this example would be the following:
"name": "projects/[PROJECT_ID]/metricDescriptors/custom.googleapis.com/stores/daily_sales"
If, for example, you want to get a metric's descriptor, you use this name.
C#To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
GoTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
JavaTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.jsTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHPTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PythonTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
RubyTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See Troubleshoot API calls if you have difficulty.
Next step: See Write user-defined metrics.
Write user-defined metricsYou can write data only to metric types for user-defined metrics. To write your data, use the timeSeries.create
method. When the time series exists, this method appends a new data point to the existing time series. When the time series doesn't exist, this method creates it and appends the data.
You write data points by passing a list of TimeSeries
objects to timeSeries.create
. The maximum list size is 200 and each object in the list must specify a different time series:
metric
and resource
fields identify a specific TimeSeries
object. These fields represent the metric type of the data and the monitored resource from which the data was collected.metricKind
and valueType
; they are ignored when writing data points.Each TimeSeries
object must contain only a single Point
object:
TimeInterval
.To write more than one point to the same time series, use a separate call to the timeSeries.create
method for each point. Don't write data to a single time series faster than one point each 5 seconds. When you add data points to different time series, there is no rate limitation.
To write metric data, use the timeSeries.create
method. You can execute this method by using the APIs Explorer widget on the method's reference page. See APIs Explorer for more information.
To write a point to the stores/daily_sales
metric created in the Manual creation of metric descriptors:
timeSeries.create
.Use the following sample parameters:
projects/[PROJECT_ID]
request body: include a list of TimeSeries
objects. The following sample has only one time series in the list.
project_id
and endTime
values in the following request body. Use the ID of your project. The endTime
must be close to the current time; date -Iseconds
returns the current time in a usable format.
{
"timeSeries": [
{
"metric": {
"type": "custom.googleapis.com/my_metric",
"labels": {
"my_label": "my_value"
}
},
"resource": {
"type": "gce_instance",
"labels": {
"project_id": "[PROJECT_ID]",
"instance_id": "1234567890123456789",
"zone": "us-central1-f"
}
},
"points": [
{
"interval": {
"endTime": "2018-06-01T10:00:00-04:00"
},
"value": {
"doubleValue": 123.45
}
}
]
}
]
}
To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
GoTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
JavaTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.jsTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHPTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PythonTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
RubyTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See Troubleshoot API calls if you have difficulty.
Delete user-defined metricsTo delete a user-defined metric, delete its metric descriptor. You can't delete the time-series data stored in your Google Cloud project; however, deleting the metric descriptor renders the data inaccessible. The data expires and is deleted according to the data retention policy.
You can't delete the metric descriptor for a built-in metric.
Note: Deleting a metric descriptor makes its existing time series data inaccessible. If you recreate a deleted metric descriptor, exactly as it was before deletion, then you might regain access to the data. Don't depend on this behavior.To delete your metric descriptor, call the metricDescriptors.delete
method.
To delete a metric descriptor, use the metricDescriptors.delete
method. You can execute this method by using the APIs Explorer widget on the method's reference page. See APIs Explorer for more information.
To delete the stores/daily_sales
metric created in Manual creation of metric descriptors:
metricDescriptors.delete
:Supply the name of the metric descriptor to the APIs Explorer widget:
name: projects/[PROJECT_ID]/metricDescriptors/custom.googleapis.com/stores/daily_sales
Click the Execute button.
To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
GoTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
JavaTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.jsTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHPTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PythonTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
RubyTo authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See Troubleshoot API calls if you have difficulty.
Modify a user-defined metricTo modify a user-defined metric, you must update the MetricDescriptor
object that defines the metric. The only supported modification is to add labels.
To add labels to an existing user-defined metric, use the timeSeries.create
method and include the new labels with the time-series data. The labels are added to the metric descriptor when the labels you attempt to write are valid and the total number of labels is less than 30.
The time series data is then written as though the label had been there from the beginning.
If you want to do more than add new labels, then you must delete and recreate the metric descriptor. In this case, you lose all the time series data previously collected for the old metric descriptor. See Delete user-defined metrics for more information.
You can't rename a metric.
What's nextExcept as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-15 UTC.
[[["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-08-15 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4