Collect logs with the OpenTelemetry Collector
The Grafana Cloud stack includes a logging service powered by Grafana Loki, a Prometheus-inspired log aggregation system.
Loki natively supports ingesting OpenTelemetry logs over HTTP.
For ingesting logs to Loki using the OpenTelemetry Collector, you must use the otlphttp exporter.
For more information about using OpenTelemetry with Grafana products, refer to the Grafana OpenTelemetry documentation.
Loki configuration
When logs are ingested by Loki using an OpenTelemetry protocol (OTLP) ingestion endpoint, some of the data is stored as Structured Metadata.
You must set allow_structured_metadata to true within your Loki config file. Otherwise, Loki will reject the log payload as malformed. Note that Structured Metadata is enabled by default in Loki 3.0 and later.
limits_config:
allow_structured_metadata: trueConfigure the OpenTelemetry Collector to write logs into Loki
You need to make the following changes to the OpenTelemetry Collector config to write logs to Loki on its OTLP ingestion endpoint.
exporters:
otlphttp:
endpoint: http://<loki-addr>/otlpAnd enable it in service.pipelines:
service:
pipelines:
logs:
receivers: [...]
processors: [...]
exporters: [..., otlphttp]If you want to authenticate using basic auth, we recommend the basicauth extension.
extensions:
basicauth/otlp:
client_auth:
username: username
password: password
exporters:
otlphttp:
auth:
authenticator: basicauth/otlp
endpoint: http://<loki-addr>/otlp
service:
extensions: [basicauth/otlp]
pipelines:
logs:
receivers: [...]
processors: [...]
exporters: [..., otlphttp]Format considerations
Since the OpenTelemetry protocol differs from the Loki storage model, here is how data in the OpenTelemetry format will be mapped by default to the Loki data model during ingestion, which can be changed as explained later:
Index labels: Resource attributes map well to index labels in Loki, since both usually identify the source of the logs. The default list of Resource Attributes to store as Index labels can be configured using
default_resource_attributes_as_index_labelsunder distributor’s otlp_config. By default, the following resource attributes will be stored as index labels, while the remaining attributes are stored as Structured Metadata with each log entry:cloud.availability_zone
cloud.region
container.name
deployment.environment.name
k8s.cluster.name
k8s.container.name
k8s.cronjob.name
k8s.daemonset.name
k8s.deployment.name
k8s.job.name
k8s.namespace.name
k8s.pod.name
k8s.replicaset.name
k8s.statefulset.name
service.instance.id
service.name
service.namespace
Note
Because Loki has a default limit of 15 index labels, we recommend storing only select resource attributes as index labels. Although the default config selects more than 15 Resource Attributes, it should be fine since a few are mutually exclusive.
Tip
For Grafana Cloud Logs, see the current OpenTelemetry guidance.
Timestamp: One of
LogRecord.TimeUnixNanoorLogRecord.ObservedTimestamp, based on which one is set. If both are not set, the ingestion timestamp will be used.LogLine:
LogRecord.Bodyholds the body of the log. However, since Loki only supports Log body in string format, we will stringify non-string values using the AsString method from the OTel collector lib.Structured Metadata: Anything which can’t be stored in Index labels and LogLine would be stored as Structured Metadata. Here is a non-exhaustive list of what will be stored in Structured Metadata to give a sense of what it will hold:
- Resource Attributes not stored as Index labels is replicated and stored with each log entry.
- Everything under InstrumentationScope is replicated and stored with each log entry.
- Everything under LogRecord except
LogRecord.Body,LogRecord.TimeUnixNanoand sometimesLogRecord.ObservedTimestamp.
The default list of resource attributes to store as labels can be configured using default_resource_attributes_as_index_labels under the
distributor’s otlp_config. You can set global limits using
limits_config.otlp_config. If you are using Grafana Cloud, contact support to configure this setting.
Caution
Because of the potential for high cardinality,
k8s.pod.nameandservice.instance.idare no longer recommended as default labels. But because removing these resource attributes from the default labels would be a breaking change for existing users, they have not yet been deprecated as default labels. If you are a new user of Grafana Loki, we recommend that you modify your Alloy or OpenTelemetry Collector configuration to convertk8s.pod.nameandservice.instance.idfrom index labels to structured metadata. For sample configurations, refer to Remove default labels.
Things to note before ingesting OpenTelemetry logs to Loki:
Dots (.) are converted to underscores (_).
Loki does not support
.or any other special characters other than_in label names. The unsupported characters are replaced with an_while converting Attributes to Index Labels or Structured Metadata. Also, please note that while writing the queries, you must use the normalized format, i.e. use_instead of special characters while querying data using OTel Attributes.For example,
service.namein OTLP would becomeservice_namein Loki.Flattening of nested Attributes
While converting Attributes in OTLP to Index labels or Structured Metadata, any nested attribute values are flattened out using
_as a separator. It is done in a similar way as to how it is done in the LogQL json parser.Stringification of non-string Attribute values
While converting Attribute values in OTLP to Index label values or Structured Metadata, any non-string values are converted to string using AsString method from the OTel collector lib.
Changing the default mapping of OTLP to Loki Format
Loki supports per tenant OTLP config which lets you change the default mapping of OTLP to Loki format for each tenant. It currently only supports changing the storage of Attributes. Here is what the config looks like:
# OTLP log ingestion configurations
limits_config:
otlp_config:
# Configuration for Resource Attributes to store them as index labels or
# Structured Metadata or drop them altogether
resource_attributes:
# Configure whether to ignore the default list of resource attributes set in
# 'distributor.otlp.default_resource_attributes_as_index_labels' to be
# stored as index labels and only use the given resource attributes config
[ignore_defaults: <boolean>]
[attributes_config: <list of attributes_configs>]
# Configuration for Scope Attributes to store them as Structured Metadata or
# drop them altogether
[scope_attributes: <list of attributes_configs>]
# Configuration for Log Attributes to store them as Structured Metadata or
# drop them altogether
[log_attributes: <list of attributes_configs>]
attributes_config:
# Configures action to take on matching Attributes. It allows one of
# [structured_metadata, drop] for all Attribute types. It additionally allows
# index_label action for Resource Attributes
[action: <string> | default = ""]
# List of attributes to configure how to store them or drop them altogether
[attributes: <list of strings>]
# Regex to choose attributes to configure how to store them or drop them
# altogether
[regex: <Regexp>]Note
If you are a Grafana Cloud customer, you can use the config self-serve API to configure your OTLP config.



