Skip to content
All flows

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.

Check Loki's config once before sending
# 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: true
How the data moves
receiverprocessorexporterextension
logs
otlpmemory_limitertransform/loki_labelsbatchotlphttp/loki
Collector config (YAML)Show
config.yaml
# 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

otlp

Listens on 0.0.0.0 at 4317 (gRPC) and 4318 (HTTP) so remote apps, SDKs, agents, and collectors can reach it.

memory_limiter

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.

transform/loki_labels

Moves `k8s.pod.name` and `service.instance.id` from the resource onto each log record, so Loki doesn't index them.

batch

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.

otlphttp/loki

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.

health_check

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, yet otelcol_exporter_send_failed_log_records counted 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_size and send_batch_max_size.

  • 3

    Structured metadata is capped at 64 KB and 128 entries per line. In testing, a 73 KB exception.stacktrace got a 400. Loki's OTel docs show a transform moving it into the body, where max_line_size (256 KB by default) applies instead.

  • 4

    With LOKI_TENANT_ID unset, the collector starts normally and logs a warning, but if auth_enabled: true, every push fails with 401 no org id and 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 basicauth extension and point the exporter's auth.authenticator at it, as Loki's OTel docs show.

  • 6

    Loki 3.x with structured metadata on won't start unless the active schema period uses tsdb and v13. On existing installs, add a schema entry with a future from date 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_config sparingly.

  • 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)Show

Everything consulted while researching and fact-checking this flow, including the README of every component it uses.

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.