extensions:
  file_storage:
    directory: /var/lib/otelcol/storage
    create_directory: true

receivers:
  filelog:
    include: [/var/log/pods/*/*/*.log]
    # The collector's own logs would otherwise be read back and re-shipped.
    exclude: [/var/log/pods/observability_k8s-logs-to-loki*/*/*.log]
    start_at: end
    include_file_path: true
    storage: file_storage
    operators:
      # Parses the CRI-O, containerd and Docker log formats and reads
      # namespace, pod, uid and container from the file path.
      - type: container
        id: container-parser

processors:
  memory_limiter:
    check_interval: 1s
    limit_percentage: 80
    spike_limit_percentage: 25
  # Adds deployment, node and pod uid from the Kubernetes API, matched by
  # the pod name and namespace the container operator read from the path.
  k8sattributes:
    auth_type: serviceAccount
    filter:
      node_from_env_var: K8S_NODE_NAME
    extract:
      metadata: [k8s.deployment.name, k8s.node.name, k8s.pod.uid]
    pod_association:
      - sources:
          - from: resource_attribute
            name: k8s.pod.name
          - from: resource_attribute
            name: k8s.namespace.name
  transform/cluster:
    error_mode: ignore
    log_statements:
      - context: resource
        statements:
          - set(attributes["k8s.cluster.name"], "${env:CLUSTER_NAME}")
  batch:
    timeout: 5s

exporters:
  otlphttp/loki:
    endpoint: http://loki.loki.svc.cluster.local:3100/otlp
    headers:
      X-Scope-OrgID: ${env:LOKI_TENANT}
    sending_queue:
      enabled: true
      queue_size: 5000
    retry_on_failure:
      enabled: true
      initial_interval: 5s
      max_interval: 30s
      max_elapsed_time: 300s

service:
  extensions: [file_storage]
  pipelines:
    logs:
      receivers: [filelog]
      processors: [memory_limiter, k8sattributes, transform/cluster, batch]
      exporters: [otlphttp/loki]
