Skip to content

Commit

Permalink
feat(operator): User-guide for OTLP configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
xperimental committed Oct 25, 2024
1 parent a962edb commit 65ea1f5
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions operator/docs/user-guides/open-telemetry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
title: "OpenTelemetry / OTLP"
description: ""
lead: ""
date: 2024-10-25T12:43:23+02:00
lastmod: 2024-10-25T12:43:23+02:00
draft: false
images: []
menu:
docs:
parent: "user-guides"
weight: 100
toc: true
---

## Introduction

Loki 3.0 introduced the OpenTelemetry Protocol (OTLP) as a new API for ingesting log entries in addition to the standard Push API that was available in previous versions of Loki.

Because OTLP is not specifically geared towards Loki but is a standard format, it needs additional configuration on Loki's side to map the OpenTelemetry data format to Loki's data model.

Specifically, OTLP has no concept of "stream labels" or "structured metadata". OTLP instead provides metadata about a log entry in _attributes_ that are grouped into three buckets (resource, scope and log), depending on the number of entries an attribute applies to.

## Prerequisites

Log ingestion using OTLP depends on structured metadata being available in Loki. This capability was introduced with schema version 13, which is available in Loki Operator when using `v13` in a schema configuration entry.

When creating a new `LokiStack`, make sure to include `version: v13` in the storage schema configuration. If there is an existing schema configuration, a new configuration needs to be added, so that it becomes active in the future (see [Upgrading Schemas](loki-upgrading-schemas) in the Loki documentation).

```yaml
# [...]
spec:
storage:
schemas:
- version: v13
effectiveDate: 2024-10-25
```
Once the `effectiveDate` has passed your `LokiStack` will be using the new schema configuration and is ready to store structured metadata.

## Attribute Mapping

Loki splits the configuration for mapping OTLP attributes to stream labels and structured metadata into two places:

- `default_resource_attributes_as_index_labels` in the [`distributor` configuration](loki-docs-distributor-config)
- `otlp_config` in the `limits_config` (see [Loki documentation](loki-docs-limits-config))

By default, `default_resource_attributes_as_index_labels` provides a set of resource-attributes that are mapped to stream-labels on the Loki side.

As the field in the distributor configuration is limited to resource-level attributes and can only produce stream-labels as an output, the `otlp_config` needs to be used to map scope or log level attributes to structured metadata.

**Note:** All attributes that are not mapped to either a stream label or structured metadata will not be stored into Loki.

### Operator Defaults

When using the Loki Operator the default attribute mappings depend on the [tenancy mode]({{< ref "api.md#loki-grafana-com-v1-ModeType" >}}) used for the `LokiStack`:

- `static` and `dynamic` use the Grafana defaults
- `openshift-logging` uses OpenShift defaults

### Custom Attribute Mapping

All tenancy modes support customization of the attribute mapping configuration. This can be done globally (for all tenants) or on a per-tenant basis. When a custom attribute mapping configuration is defined, then the Grafana defaults are not used. If the default labels are desired as well, they need to be added to the custom configuration. See also the section about [Customizing OpenShift Defaults](#customizing-openshift-defaults) below.

**Note:** A major difference between the Operator and Loki is how it handles inheritance. Loki by default only copies the attributes defined in the `default_resource_attributes_as_index_labels` setting to the tenants whereas the Operator will copy all global configuration into every tenant.

The attribute mapping configuration in `LokiStack` is done through the limits configuration:

```yaml
# [...]
spec:
limits:
global:
otlp: {} # Global OTLP Attribute Configuration
tenants:
example-tenant:
otlp: {} # OTLP Attribute Configuration for tenant "example-tenant"
```

Both global and per-tenant OTLP configuration can contain rules mapping attributes to stream-labels or structured-metadata. At least _one stream-label_ is needed for successfully saving a log entry to Loki storage, so the configuration should account for that.

Stream labels can only be generated from resource-level attributes, which is mirrored in the data structure of the `LokiStack` resource:

```yaml
# [...]
spec:
limits:
global:
otlp:
streamLabels:
resourceAttributes:
- name: "k8s.namespace.name"
- name: "k8s.pod.name"
- name: "k8s.container.name"
```

Structured metadata on the other hand can be generated from all types of attributes (resource, scope and log):

```yaml
# [...]
spec:
limits:
global:
otlp:
streamLabels:
# [...]
structuredMetadata:
resourceAttributes:
- name: "process.command_line"
- name: "k8s\\.pod\\.labels\\..+"
regex: true
scopeAttributes:
- name: "service.name"
logAttributes:
- name: "http.route"
```

The previous example also shows that the attribute names can be expressed as _regular expressions_ by setting `regex: true`.

Using a regular expression makes sense when there are many attributes with similar names that should be mapped into Loki. It is not recommended to be used for stream labels, as it can potentially create a lot of data.

### Customizing OpenShift Defaults

The `openshift-logging` tenancy mode contains its own set of default attributes. Some of these attributes (called "required attributes") can not be removed by applying a custom configuration, because they are needed for other OpenShift components to function properly. Other attributes (called "recommended attributes") are provided but can be disabled in case they influence performance negatively. The complete set of attributes is documented in the [data model](rhobs-data-model) repository.

Because the OpenShift attribute configuration is applied based on the tenancy mode, the simplest configuration is to just set the tenancy mode and not apply any custom attributes. This will provide instant compatibility with the other OpenShift tools.

In case additional attributes are needed, either as stream labels or structured metadata, the normal custom attribute configuration mentioned above can be used. In contrast to the Grafana default labels, providing a custom attribute configuration in the `openshift-logging` tenancy mode does not remove the defaults. Instead the custom configuration is **merged** with the default configuration.

#### Removing Recommended Attributes

In case of issues with the default set of attributes, there is a way to slim down the default set of attributes applied to a LokiStack operating in `openshift-logging` tenancy mode:

```yaml
# [...]
spec:
tenants:
mode: openshift-logging
openshift:
otlp:
disableRecommendedAttributes: true # Set this to remove recommended attributes
```

Setting `disableRecommendedAttributes: true` reduces the set of default attributes to only the "required attributes".

This option is meant for situations when some of the default attributes cause performance issues during ingestion of logs or if the default set causes excessive use of storage.

Because the set of required attributes only contains stream labels and even reduces that set, only setting this option will negatively affect query performance. It needs to be combined with a custom attribute configuration that reintroduces attributes that are needed under the specific circumstances, so that the data contained in those attributes is available again.

## References

- [Loki Labels](loki-labels)
- [Structured Metadata](loki-structured-metadata)
- [OpenTelemetry Attribute](otel-attributes)
- [OpenShift Default Attributes](rhobs-data-model)

[loki-docs-distributor-config]: https://grafana.com/docs/loki/latest/configure/#distributor
[loki-docs-limits-config]: https://grafana.com/docs/loki/latest/configure/#limits_config
[loki-labels]: https://grafana.com/docs/loki/latest/get-started/labels/
[loki-structured-metadata]: https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/
[loki-upgrading-schemas]: https://grafana.com/docs/loki/latest/configure/storage/#upgrading-schemas
[otel-attributes]: https://opentelemetry.io/docs/specs/otel/common/#attribute
[rhobs-data-model]: https://github.com/rhobs/observability-data-model/blob/main/cluster-logging.md#attributes

0 comments on commit 65ea1f5

Please sign in to comment.