OpenTelemetry Collector to Loki
A logs gateway for Loki's native OTLP endpoint that keeps pod names and instance ids out of the index and sets the tenant header from an environment variable.
The contrib loki exporter was deprecated in collector v0.105.0 and removed in v0.131.0. Loki 3.x ingests OTLP/HTTP logs natively on /otlp/v1/logs, so this config template uses the otlphttp exporter, per Loki's docs.
Loki does the mapping itself. Its default index labels are service.name, service.namespace, service.instance.id, deployment.environment and deployment.environment.name, cloud.region, cloud.availability_zone, k8s.cluster.name, container.name, and the Kubernetes namespace, pod, container, and workload names. Everything else (other resource attributes, scope name and version, log attributes, severity, trace and span ids) becomes structured metadata on each line. Dots in names become underscores, so service.name is queried as service_name. Without service.name, Loki fills service_name from a short list of other labels, falling back to unknown_service, so this config template sets no default.
The gateway changes one thing. Loki's docs advise new installs to stop indexing the high-cardinality k8s.pod.name and service.instance.id, so both become structured metadata here, still filterable, as in {service_name="checkout"} | k8s_pod_name="checkout-7d9f-abc12".
Set LOKI_OTLP_ENDPOINT to Loki's base URL plus /otlp, like http://loki:3100/otlp. The exporter appends /v1/logs. Set LOKI_TENANT_ID to the tenant if auth_enabled: true (Loki's default). For a single-tenant Loki (auth_enabled: false), leave it unset. Loki then stores everything under the tenant fake.
# Loki 3.x config. Native OTLP ingestion needs a tsdb index, schema v13,
# and structured metadata turned on (the default since Loki 3.0).
auth_enabled: true # Loki's default; the collector sends X-Scope-OrgID
schema_config:
configs:
- from: 2024-04-01 # in the past on a new install, in the future when adding to an existing one
store: tsdb
object_store: filesystem # or the object store you already use
schema: v13
index:
prefix: index_
period: 24h
limits_config:
allow_structured_metadata: trueCollector config (YAML)ShowHide
# Ship OTel data to Loki
# Built by Telflo. otelcol-contrib 0.159.0 to Loki 3.x over native OTLP/HTTP.
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
memory_limiter:
check_interval: 1s
limit_mib: 1600 # sized for a 2 GiB gateway; scale both values with the host
spike_limit_mib: 320
transform/loki_labels:
error_mode: ignore
log_statements:
# Loki indexes k8s.pod.name and service.instance.id by default and its docs
# advise new installs against it. Copy both onto each record, where Loki
# stores them as structured metadata...
- context: log
statements:
- set(log.attributes["k8s.pod.name"], resource.attributes["k8s.pod.name"]) where resource.attributes["k8s.pod.name"] != nil
- set(log.attributes["service.instance.id"], resource.attributes["service.instance.id"]) where resource.attributes["service.instance.id"] != nil
# ...then remove them from the resource so they never become index labels.
- context: resource
statements:
- delete_key(resource.attributes, "k8s.pod.name")
- delete_key(resource.attributes, "service.instance.id")
batch:
send_batch_size: 2048 # keeps one push under Loki's 6 MB default burst limit
send_batch_max_size: 2048
exporters:
otlphttp/loki:
endpoint: ${env:LOKI_OTLP_ENDPOINT} # e.g. http://loki:3100/otlp, the exporter appends /v1/logs
headers:
X-Scope-OrgID: ${env:LOKI_TENANT_ID} # leave unset when Loki runs with auth_enabled false
sending_queue:
sizer: items
queue_size: 100000
extensions:
health_check:
endpoint: 0.0.0.0:13133
service:
extensions: [health_check]
pipelines:
logs:
receivers: [otlp]
processors: [memory_limiter, transform/loki_labels, batch]
exporters: [otlphttp/loki]Use this flow
Open the config in Telflo and it becomes a working pipeline on the canvas: adapt what's specific to you, test it against recorded traffic, and push it to your fleet. Free account, no card.
Components
What's in it, and why
Listens on 0.0.0.0 at 4317 (gRPC) and 4318 (HTTP) so remote apps, SDKs, agents, and collectors can reach it.
Caps memory at 1600 MiB for a 2 GiB host, with a 320 MiB spike limit, the README's suggested 20% starting point. Above the 1280 MiB soft limit it refuses data, pushing back on the receiver instead of running out of memory.
Moves `k8s.pod.name` and `service.instance.id` from the resource onto each log record, so Loki doesn't index them.
Caps pushes at 2,048 records, keeping each under Loki's 6 MB default burst limit while records average under 3 KB with structured metadata.
Posts gzipped OTLP protobuf with the `X-Scope-OrgID` tenant header and queues up to 100,000 records in memory. Retries 429, 502, 503, and 504 with backoff for up to five minutes before dropping.
Serves a liveness endpoint on port 13133 for the container runtime or load balancer.
Notes
Gotchas
- 1
Loki returns a 400 for refused records, such as ones older than
reject_old_samples_max_age(one week by default) or over an hour behind their stream's newest entry. The collector never retries a 400, even when Loki stored part of the request. In a test batch of six, Loki stored three and refused three older than seven days, yetotelcol_exporter_send_failed_log_recordscounted all six. - 2
Loki's default tenant rate limit, 4 MB/s, counts structured metadata. In testing, four short log lines counted as 1,001 bytes, and a push over the burst limit got a 429 on every retry until dropped. If a tenant under its rate sees steady 429s, lower both
send_batch_sizeandsend_batch_max_size. - 3
Structured metadata is capped at 64 KB and 128 entries per line. In testing, a 73 KB
exception.stacktracegot a 400. Loki's OTel docs show a transform moving it into the body, wheremax_line_size(256 KB by default) applies instead. - 4
With
LOKI_TENANT_IDunset, the collector starts normally and logs a warning, but ifauth_enabled: true, every push fails with401 no org idand is dropped without retry. - 5
Loki has no authentication beyond optional client TLS certificates, and its docs call for an authenticating reverse proxy. For Basic auth, add a
basicauthextension and point the exporter'sauth.authenticatorat it, as Loki's OTel docs show. - 6
Loki 3.x with structured metadata on won't start unless the active schema period uses
tsdbandv13. On existing installs, add a schema entry with a futurefromdate rather than edit the current one. - 7
Loki allows 15 index labels per stream by default, so promote extra resource attributes with
limits_config.otlp_configsparingly. - 8
A collector restart drops the in-memory queue. There's no disk queue, on purpose. A backlog over an hour old, replayed after other senders kept its stream current, would be rejected anyway.
Sources and references (21)ShowHide
Everything consulted while researching and fact-checking this flow, including the README of every component it uses.
- grafana.com/docs/loki/v3.7.x/send-data/otel/
- grafana.com/docs/loki/v3.7.x/get-started/labels/structured-metadata/
- grafana.com/docs/loki/v3.7.x/get-started/labels/modify-default-labels/
- grafana.com/docs/loki/v3.7.x/operations/multi-tenancy/
- grafana.com/docs/loki/v3.7.x/operations/authentication/
- grafana.com/docs/loki/v3.7.x/operations/troubleshooting/troubleshoot-ingest/
- grafana.com/docs/loki/v3.7.x/operations/storage/schema/
- grafana.com/docs/loki/v3.7.x/configure/
- github.com/grafana/loki/releases/tag/v3.7.10
- github.com/open-telemetry/opentelemetry-collector-contrib/releases/tag/v0.105.0
- github.com/open-telemetry/opentelemetry-collector-contrib/releases/tag/v0.131.0
- github.com/open-telemetry/opentelemetry-collector-contrib/issues/33916
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/receiver/otlpreceiver/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/processor/memorylimiterprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/processor/batchprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/processor/transformprocessor/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/exporter/otlphttpexporter/README.md
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/exporter/otlphttpexporter/otlp.go
- github.com/open-telemetry/opentelemetry-collector/blob/v0.159.0/exporter/exporterhelper/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/extension/healthcheckextension/README.md
- github.com/open-telemetry/opentelemetry-collector-contrib/blob/v0.159.0/extension/basicauthextension/README.md
More flows
Related flows
Replace vendor agents on your VM fleet
Scrapes CPU, memory, load, disk, filesystem, and network metrics plus system and application logs on every VM.
Ship OTel data to ClickHouse
The ClickStack (HyperDX) ingestion collector as upstream ships it: parses JSON log bodies into attributes, infers severity from level fields or the body text, and routes browser session-replay events to their own ClickHouse table.
Collect per-node telemetry with a DaemonSet
Per-node DaemonSet that scrapes kubelet and host metrics, tails pod logs with the container parser and file_storage checkpoints, enriches everything with k8sattributes, and forwards to the in-cluster gateway.
Test it before your fleet runs it
Free account, no card. Open this flow in the editor, adapt it, and see what it does to real data before anything ships.