This document describes how metric data that is sent your Google Cloud project
by using the Telemetry (OTLP) API is mapped to the
Cloud Monitoring structures. This API implements the
[OpenTelemetry Protocol](https://opentelemetry.io/docs/specs/otlp). You can send data to this API when you instrument
your applications with an `otlphttp` exporter and an OpenTelemetry Collector,
or when you use the [OpenTelemetry SDKs](https://opentelemetry.io/docs/languages/).

OpenTelemetry is a Google Cloud-supported open source project with Google Cloud
engineers staffed to ensure support for ingesting and visualizing your
telemetry.

## Best practices

When instrumenting your applications to send trace data to your
Google Cloud project, we recommend that you use an exporter that
writes OTLP-formatted data to an [Collector](https://opentelemetry.io/docs/collector/),
which then sends your trace data to the Telemetry API.
In your collector, specify only the root URL:

    exporters:
      otlphttp:
        encoding: proto
        endpoint: https://telemetry.googleapis.com

OpenTelemetry detects the data type and automatically appends `/v1/traces`,
`/v1/metrics`, or `/v1/logs` as appropriate. For more information, see
[OTLP/HTTP Request](https://opentelemetry.io/docs/specs/otlp#otlphttp-request).

For examples that export trace or metric data to the Telemetry API,
see the following documents:

- [Overview of collector-based instrumentation samples](https://docs.cloud.google.com/stackdriver/docs/instrumentation/setup/sample-overview.md.txt).
- [OTLP ingestion overview](https://docs.cloud.google.com/stackdriver/docs/otlp/overview.md.txt).

When you can't use a collector, you can use an OpenTelemetry library that contains
an in-process OTLP exporter to send telemetry to the Telemetry API.
To learn how to directly export trace data, see
[Cloud Trace exporter to the OTLP endpoint](https://docs.cloud.google.com/stackdriver/docs/instrumentation/migrate-to-otlp-endpoints.md.txt).

## Authentication

You must configure your exporters with the credentials necessary to send
data to your Google Cloud project. For example, when you use collectors, typically
you use the `googleclientauth` extension to authenticate with Google
credentials.

For an example of authentication when using direct export of trace data, see
[Configure authentication](https://docs.cloud.google.com/stackdriver/docs/instrumentation/migrate-to-otlp-endpoints.md.txt#auth).
This example illustrates how to configure the exporter with your Google Cloud
[Application Default Credentials (ADC)](https://docs.cloud.google.com/docs/authentication/provide-credentials-adc.md.txt)
and add a language-specific Google Auth Library to your application.

To send telemetry data to your Google Cloud project by using the Telemetry API,
you must also do the following:

- Configure a quota project. To learn more, see
  [Set the quota project](https://docs.cloud.google.com/docs/quotas/set-quota-project.md.txt).

- Grant the following Identity and Access Management (IAM) roles to the user or to the
  service account that the application uses:

  - [Service Usage Consumer](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageConsumer) role (`roles/serviceusage.serviceUsageConsumer`) on the quota project.
  - [Cloud Telemetry Writer](https://docs.cloud.google.com/iam/docs/roles-permissions/telemetry.md.txt#telemetry.writer) role (`roles/telemetry.writer`) on the project. This role lets your application write log, metric, and trace data.

<br />

## OTLP metrics in Cloud Monitoring

<br />

When metrics are ingested into Cloud Monitoring by using an
OpenTelemetry Collector and the `otlphttp` exporter or sent directly by using
an OpenTelemetry SDK, the OTLP metrics
are mapped to Cloud Monitoring metric structures.
This section describes the following:

- The [mapping between OTLP resources and
  Cloud Monitoring monitored resources](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/v1.metrics#mr-mapping).
- The [mapping between OTLP metrics and
  Cloud Monitoring metrics](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/v1.metrics#metric-mapping).

<br />

### Monitored resource mapping

<br />

All metric points are written as they are for Google Cloud Managed Service for Prometheus,
by using the [Prometheus mapping](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/v1.metrics.md.txt#prometheus-mapping).

<br />

#### Prometheus mapping

<br />

Prometheus metrics require the use of the monitored-resource type
[`prometheus_target`](https://docs.cloud.google.com/monitoring/api/resources.md.txt#tag_prometheus_target).

The following labels on the `prometheus_target` resource type are
used to schematize and efficiently store data within Monarch. The more
precisely you specify values for these attributes, the better your
queryability and scalability will be.

We strongly recommend being as explicit as possible when setting values
for these labels, although we have implemented fallback logic for use
in the absence of explicit values.

The following table shows the sources of values for labels, in order of
priority:

| `prometheus-target` label | Value used (in priority order) |
|---|---|
| `location` (required) | - `location` attribute - `cloud.availability_zone` attribute - `cloud.region` attribute - Reject the point if empty |
| `cluster` | - `cluster` attribute - `k8s.cluster.name` attribute - `__gce__`, if `cloud.platform` attribute is `gcp_compute_engine` - `__run__`, if `cloud.platform` attribute is `gcp_cloud_run` - `__vae__`, if `cloud.platform` attribute is `gcp.agent_engine` - Empty string |
| `namespace` | - `namespace` attribute - `k8s.namespace.name` attribute - `service.namespace` attribute - Empty string |
| `job` | - `job` attribute - `"service.namespace"` + "/" + `service.namespace` attribute - `service.name` attribute - `service.name` attribute, if not `unknown_service:foo` - `faas.name` attribute - `k8s.deployment.name` attribute - `k8s.statefulset.name` attribute - `k8s.job.name` attribute - `k8s.cronjob.name` attribute - `service.name` attribute, if `unknown_service:foo` - Empty string |
| `instance` (required) | - `instance` attribute - `service.instance.id` attribute - `faas.instance` attribute - `k8s.pod.name:k8s.container.name` attribute - `k8s.pod.name` attribute, if no container name present - `host.id` attribute - Reject the point if empty |

<br />

### Metric mapping

<br />

Metrics are converted into the Prometheus time-series format. Metric names must have either no domain or the domain `prometheus.googleapis.com`. After conversion, the metric name will include the `prometheus.googleapis.com` prefix and an additional suffix, based on the OTLP point kind. The resulting Cloud Monitoring metric has the following structure:

<br />

    prometheus.googleapis.com/{metric_name}/{suffix}

Additionally, for each unique OpenTelemetry resource, the conversion adds a
`target_info` metric that contains all resource attributes except
`service.name`, `service.instance.id`, and `service.namespace`.

All OTLP `INT64` metrics are translated to the
[`DOUBLE` value type](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#ValueType) in Cloud Monitoring,
even if the collector specifies the value type as `INT64`.
This change is made because once a time series is in Monarch, you
can't change the value type. The most common consequence of supporting
`INT64` values is that you end up with collisions that only can be
resolved by deleting a metric.

> [!NOTE]
> **Note:** We expect there to be possible value-type collisions, specifically if you have previously sent `INT64` `target_info` metrics. If you encounter collisions, the best long-term solution for this is for you to delete the metric descriptor for the `INT64` `target_info` metrics. To delete this descriptor from your metric scope, use this [Golang script](https://raw.githubusercontent.com/GoogleCloudPlatform/prometheus-engine/fea30dcac16250c5bef3b4e030629021aa28320e/examples/scripts/delete_metric_descriptors/delete_target_info.go).

<br />

#### Prometheus metric mapping

<br />

Metric types are mapped as follows:

- OTLP Gauge maps to Cloud Monitoring [gauge](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#ValueType).
- OTLP Sum maps as follows:
  - To Cloud Monitoring [gauge](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) when `is_monotonic` is set to `false`.
  - To Cloud Monitoring [cumulative](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) when `aggregation_temporality` is set to `AGGREGATION_TEMPORALITY_CUMULATIVE`.
  - To Cloud Monitoring [delta](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) when `aggregation_temporality` is set to `AGGREGATION_TEMPORALITY_DELTA`.
- OTLP Histogram maps to Cloud Monitoring [distribution](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/TypedValue.md.txt#Distribution) with a metric kind of [cumulative or delta](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind), depending on the value of `aggregation_temporality`.
- OTLP Summary metrics are expanded into individual time series for each component: `count`, `sum`, and each `quantile`.
  - The names for count and sum metrics are suffixed with `_count` or `_sum` respectively, and are written as Cloud Monitoring [cumulative](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) metrics of type [DOUBLE](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#ValueType).
  - Each quantile becomes is its own [gauge](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) time series of type [DOUBLE](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#ValueType), with a `quantile` label.

The following table summarizes the metric mapping:

| OTLP [point kind](https://opentelemetry.io/docs/specs/otel/metrics/data-model/#point-kinds) | Monitoring [metric kind](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#MetricKind) | Monitoring [value type](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors.md.txt#ValueType) | Suffix | Notes |
|---|---|---|---|---|
| GAUGE | GAUGE | DOUBLE | /gauge |   |
| GAUGE (metric.metadata\["prometheus.type"\]="unknown") | GAUGE | DOUBLE | /unknown | Prometheus Unknowns are split into a counter and a gauge by the OpenTelemetry Collector. |
| SUM (monotonic, CUMULATIVE) | CUMULATIVE | DOUBLE | /counter |   |
| SUM (monotonic, CUMULATIVE, metric.metadata\["prometheus.type"\]="unknown") | CUMULATIVE | DOUBLE | /unknown:counter | Prometheus Unknowns are split into a counter and a gauge by the OpenTelemetry Collector. |
| SUM (monotonic, DELTA) | DELTA | DOUBLE | /delta |   |
| SUM (non-monotonic, CUMULATIVE) | GAUGE | DOUBLE | /gauge |   |
| SUM (non-monotonic, DELTA) | **Not supported** |   |   | Delta-temporality UpDownCounters are not supported. |
| HISTOGRAM (CUMULATIVE) | CUMULATIVE | DISTRIBUTION with [explicit buckets](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/TypedValue.md.txt#BucketOptions) | /histogram |   |
| EXPONENTIAL HISTOGRAM (CUMULATIVE) | CUMULATIVE | DISTRIBUTION with [exponential buckets](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/TypedValue.md.txt#BucketOptions) | /histogram |   |
| HISTOGRAM (DELTA) | DELTA | DISTRIBUTION with [explicit buckets](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/TypedValue.md.txt#BucketOptions) | /histogram:delta |   |
| EXPONENTIAL HISTOGRAM (DELTA) | DELTA | DISTRIBUTION with [exponential buckets](https://docs.cloud.google.com/monitoring/api/ref_v3/rest/v3/TypedValue.md.txt#BucketOptions) | /histogram:delta |   |
| SUMMARY (sum, count, quantile) | CUMULATIVE CUMULATIVE GAUGE | DOUBLE DOUBLE DOUBLE | _sum/summary:counter _count/summary /summary | Summary data points are written as multiple time series, one for count, sum, and each calculated quantile. Quantile metrics are also generated with a `quantile` label. |

<br />

## Differences between the `googlemanagedprometheus` exporter and the Telemetry API

<br />

The Telemetry API (`telemetry.googleapis.com`) handles metrics differently
from the [`googlemanagedprometheus` exporter](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/googlemanagedprometheusexporter):

- The Telemetry API allows the period (`.`) and slash (`/`) characters with
  in metric names. The `googlemanagedprometheus` exporter converts all
  instances of these characters to the underscore (`_`) character.
  For example, an OTLP metric called
  `prometheus.googleapis.com/foo.bar/gauge` is exported verbatim
  by the OTLP exporter but is exported as
  `prometheus.googleapis.com/foo_bar/gauge` by the
  `googlemanagedprometheus` exporter.

  When the metrics are ingested, Cloud Monitoring creates metric descriptors
  based on the names. The difference in how the period (`.`) and slash (`/`)
  characters are handled by the ingestion pathways means that resulting
  metric descriptors differ between metrics ingested by using
  the `googlemanagedprometheus` exporter and those ingested by using the
  `otlphttp` exporter. If you use both ingestion paths, then you have
  two sets of metrics; to get complete results when querying, you have to
  manually union results from the Prometheus and OTLP versions of the
  metrics.
- The Telemetry API doesn't append a unit to a metric name when a
  unit is present, and it doesn't append a `_total` suffix to counters. So a
  metric exported as `prometheus.googleapis.com/foo/counter` when using the
  Telemetry API is exported as
  `prometheus.googleapis.com/foo_seconds_total/counter` by the
  `googlemanagedprometheus` exporter.
  This difference also applies to the `_total` and
  `_ratio` suffixes.

- The API synthesizes the `sum_of_squared_deviation` value for distribution
  values derived from exponential histograms. The `googlemanagedprometheus`
  exporter does not set this field for exponential histograms.

- The API converts all integer point values to double values for Prometheus
  metrics.

- The API doesn't set the `scope_version` or `scope_name` labels if those
  labels have empty values.

## Cloud Monitoring and data residency

To learn about how your metric data is stored, see
[Data regionality for Cloud Monitoring](https://docs.cloud.google.com/monitoring/docs/region-support.md.txt).

## Where to view ingested data

Metric data that is ingested through the Telemetry API can be viewed using
the **Metrics Explorer** page.
For information about viewing and charting your metric data, see
[Create charts with Metrics Explorer](https://docs.cloud.google.com/monitoring/charts/metrics-explorer.md.txt).

## Quotas

For information about quotas associated with the Telemetry API,
see [Telemetry API limits](https://docs.cloud.google.com/stackdriver/quotas.md.txt#telemetry-api-limits).