This document describes how log data that is sent your Google Cloud project
by using the Telemetry (OTLP) API is mapped to a Cloud Logging
structure. 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.

## General structure of OTLP-formatted log data

When log data is sent to Google Cloud by using the Telemetry API, this
data must be in a format that is consistent with OTLP. The general structure
for this data is as shown:

    "resourceLogs": [
        {
          "resource": {
            "attributes": [...]
          },
          "scopeLogs": [
            {
              "logRecords": [...]
            }
          ]
        }
    ]

Notice that OpenTelemetry batches individual logs, each of which is represented by
a `logRecord` structure, with information about the source of those logs,
which is represented by the `resource` structure.

When Google Cloud Observability receives a `resourceLogs` object, it constructs one
[`LogEntry`](https://docs.cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry.md.txt) for each `logRecord`. Unlike OTLP which batches source
information with a collection of individual logs, each `LogEntry` structure
contains information about the source of the log and the log itself.

To learn more about the structure of OTLP-formatted log data,
see the OpenTelemetry [`logs.proto`](https://github.com/open-telemetry/opentelemetry-proto/blob/v1.5.0/opentelemetry/proto/logs/v1/logs.proto).

## How OTLP-formatted log data is processed

When you send an OTLP `resourceLogs` structure to the Telemetry API,
Google Cloud Observability performs the following actions:

1. For each `logRecord`, the system creates a `LogEntry`.

   The following documents describe how the system populates the `LogEntry`
   from the OTLP log data:
   - [OTLP log record to log entry mapping](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/otlp-log-record-to-log-entry.md.txt).
   - [OTLP attributes to Cloud Logging resource type mapping](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/otlp-attribute-to-logging-resource.md.txt).

   Because each `LogEntry` contains information about the corresponding
   Cloud Logging resource type, each `LogEntry` might be larger than the
   corresponding OTLP `logRecord`.

   The conversion from the OTLP `resourceLogs` structure into a series of
   `LogEntry` structure might be lossy. That is, you might not be able to
   convert from the `LogEntry` structure to the original
   `resource` and `logRecord` fields.
2. The `LogEntry` is routed to by aggregated sinks in the project hierarchy
   and by sinks in the project, in exactly the same manner as if the log entry
   was sent to your project by using the Cloud Logging API.

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

## Cloud Logging and data residency

By default, Cloud Logging routes log entries that originate in a project
to system-created log buckets. Log buckets are regional resources. You can
update the default log sink to send log entries to a custom log bucket that is
in a location of your choice. To learn more, see
[Regionalize your logs](https://docs.cloud.google.com/logging/docs/regionalized-logs.md.txt).

Cloud Logging provides settings you can configure for organizations and
folders. These settings specify the location of new system-created
log buckets, whether these log buckets use customer managed encryption keys
(CMEK), and the configuration of the default log sink. To learn more, see
[Support for organizations and folders](https://docs.cloud.google.com/logging/docs/routing/overview.md.txt#support_for_organizations_and_folders).

## Where to view ingested data

You can view your log data by using the **Logs Explorer** and
**Observability Analytics** pages. For information, see the following:

- [View and analyze logs](https://docs.cloud.google.com/logging/docs/view/logs-explorer-interface.md.txt).
- [Query and analyze logs with SQL](https://docs.cloud.google.com/logging/docs/analyze/query-and-view.md.txt).

## Limitations

This section describes limits. It also describes how Google Cloud Observability handles
certain types of data.

### Limits

| Description | Value | Notes |
|---|---|---|
| Maximum number of logs per OTLP request | 8192 | Refers to the maximum number of `logRecords` in an OTLP `resourceLogs` structure. Limit. |
| Maximum size of each request | 5 MiB | Limit. |
| Maximum cumulative size of converted `LogEntry` objects per request | 64 MiB | Includes replicated `Resource` and `InstrumentationScope` metadata per `LogEntry`. Cloud Logging rejects records that exceed this system limit with an `INVALID_ARGUMENT` error. Limit. |
| Maximum size of a [`LogEntry`](https://docs.cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry.md.txt) that is created from an OTLP log record | 256 KiB | Cloud Logging rejects log records whose converted `LogEntry` exceeds this size with an `INVALID_ARGUMENT` error. Limit. |
| Maximum length of an attribute key | 512 B | Applies to `LogRecord`, `Resource`, and `InstrumentationScope` attributes. Cloud Logging rejects oversized keys with an `INVALID_ARGUMENT` error. Limit. |
| Maximum length of an attribute value | 64 KiB | Applies to `LogRecord`, `Resource`, and `InstrumentationScope` attributes. Cloud Logging rejects oversized values with an `INVALID_ARGUMENT` error. Limit. |
| Maximum depth of attribute nesting | 20 | Applies to `LogRecord` and `Resource` attributes. Cloud Logging rejects requests that exceed this depth with an `INVALID_ARGUMENT` error. Limit. |
| Maximum number of resource attributes per `ResourceLogs` object | 1,024 | Cloud Logging rejects log records in `ResourceLogs` objects that exceed this system limit with an `INVALID_ARGUMENT` error. Limit. |
| Maximum size of schema URLs | 8,192 bytes | Applies to `ResourceLogs.schema_url` and `ScopeLogs.schema_url`. Cloud Logging rejects log records with oversized schema URLs with an `INVALID_ARGUMENT` error. Limit. |
| Maximum number of log-ingestion bytes per minute | 2.4 GB for the following regions: `asia-east1, asia-northeast1, asia-southeast1, asia-south1, europe-west1, europe-west2, europe-west3, europe-west4, us-central1, us-east4, us-west1`. 300 MB for all other regions. | Quota. |

### Behavior

- When both the OpenTelemetry severity number and severity text are set, the system
  uses the severity number to determine the Cloud Logging severity level.
  If the OTLP record doesn't contain severity information, then the
  Cloud Logging severity level is set to `DEFAULT`.

- When an OTLP record contains duplicate attribute keys, the system retains
  the first key and discards the attributes with duplicate keys.

- The system converts attributes attached to a log record into a string. For
  an example, see [Labels field](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/otlp-log-record-to-log-entry.md.txt#labels).

- Log names must be URL-safe or they are URL-encoded during ingestion.
  For information about how log names are set, see
  [How `LogEntry` fields are set](https://docs.cloud.google.com/stackdriver/docs/reference/telemetry/otlp-log-record-to-log-entry.md.txt#map-record-to-entry).

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