A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/monitoring/custom-metrics/creating-metrics below:

Create user-defined metrics with the API | Cloud Monitoring

Skip to main content Create user-defined metrics with the API

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 begin

To 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:

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Ensure the Monitoring API is enabled. For details, see Enabling the Monitoring API.
  4. For applications running outside of Google Cloud, your Google Cloud project must authenticate your application using local Application Default Credentials (ADC). For more information, see Set up ADC for on-premises or another cloud provider.
Create a user-defined metric type

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.

Auto-creation of metric descriptors

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:

In 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 descriptors

To create a metric descriptor, do the following:

  1. 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:

    1. Choose a metric name for your user-defined metric.

    2. Choose a display name and description for your metric. The display name is used in the Google Cloud console.

    3. 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.

    4. 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.

    5. Choose the metric's labels—their names, value types, and descriptions.

  2. Determine the monitored resources against which the metric data is written. Choose from the following list:

  3. 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.

Protocol

To 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:

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.

Try It!

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.

Go

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Java

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Node.js

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

PHP

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Python

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Ruby

To 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 metrics

You 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:

Protocol

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:

  1. Go to the reference page for timeSeries.create.
  2. Supply the parameters below to the APIs Explorer widget.
  3. Click the Execute button.

Use the following sample parameters:

Try It!

Note: To view the request as a curl command, as an HTTP request, or in JavaScript, click fullscreen Full screen in APIs Explorer. C#

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Go

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Java

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Node.js

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

PHP

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Python

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Ruby

To 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 metrics

To 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.

Protocol

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:

  1. Go to the reference page for metricDescriptors.delete:
  2. Supply the name of the metric descriptor to the APIs Explorer widget:

    name: projects/[PROJECT_ID]/metricDescriptors/custom.googleapis.com/stores/daily_sales

  3. Click the Execute button.

Try It!

Note: To view the request as a curl command, as an HTTP request, or in JavaScript, click fullscreen Full screen in APIs Explorer. C#

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Go

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Java

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Node.js

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

PHP

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Python

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

Ruby

To 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 metric

To 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 next

Except 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