{{- if .Values.logIngest.enabled }}
apiVersion: opentelemetry.io/v1beta1
kind: OpenTelemetryCollector
metadata:
  name: {{ .Release.Name }}-opentelemetry-collector
  namespace: {{ .Release.Namespace }}
spec:
  mode: sidecar
  image: ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:latest
  config:
    receivers:
      filelog:
        include:
          - {{ .Values.logIngest.logPathPattern }}
        start_at: beginning
        include_file_path: true
        include_file_name: true
        operators:
          - type: json_parser
            timestamp:
              parse_from: record.time.timestamp
              layout: unix_float
          - type: move
            from: record.level.name
            to: severity
          - type: move
            from: record.message
            to: message
    
    processors:
      k8sattributes:
        auth_type: "serviceAccount"
        passthrough: false
        extract:
          - k8s.pod.name
          - k8s.pod.ip
          - k8s.pod.uid
          - k8s.deployment.name
          - k8s.deployment.uid
          - k8s.namespace.name
          - k8s.node.name
        pod_association:
          - sources:
              - from: resource_attribute
                name: k8s.pod.ip
          - sources:
              - from: resource_attribute
                name: k8s.pod.uid
      resource:
        attributes:
          - key: service.name
            value: "{{ .Release.Name }}"
            action: upsert
          - key: service.namespace
            value: "{{ .Release.Namespace }}"
            action: upsert
      batch:
        send_batch_size: 1000
        timeout: 10s
    
    exporters:
      loki:
        endpoint: {{ .Values.logIngest.lokiEndpoint }}
        labels:
          resource: 
            service.name: "service.name"
            service.namespace: "service.namespace"
            k8s.pod.name: "k8s.pod.name"
            k8s.namespace.name: "k8s.namespace.name"
            k8s.node.name: "k8s.node.name"
            k8s.deployment.name: "k8s.deployment.name"
          record:
            severity: "severity"
    
    logging:
      verbosity: detailed
    
    service:
      telemetry:
        logs:
          level: "info"
      pipelines:
        logs:
          receivers: [filelog]
          processors: [k8sattributes, resource, batch]
          exporters: [loki, logging]
{{- end }}