{{- if .Values.logs.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "kubernetes-agent.fullname" . }}-daemonset
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kubernetes-agent.labels" . | nindent 4 }}
data:
  otel-collector-config.yaml: |
    extensions:
      health_check:
        endpoint: "0.0.0.0:13133"

    receivers:
      # Collect node, pod, and container resource metrics from kubelet
      kubeletstats:
        collection_interval: {{ .Values.collectionInterval }}
        auth_type: serviceAccount
        endpoint: "https://${env:NODE_IP}:10250"
        insecure_skip_verify: true
        metric_groups:
          - node
          - pod
          - container
        extra_metadata_labels:
          - container.id
        k8s_api_config:
          auth_type: serviceAccount

      # Collect pod logs from /var/log/pods
      filelog:
        include:
          - /var/log/pods/*/*/*.log
        exclude:
          # Exclude the agent's own logs to avoid feedback loop
          - /var/log/pods/{{ "{{ .Release.Namespace }}" }}_{{ "{{ include \"kubernetes-agent.fullname\" . }}" }}*/**/*.log
        start_at: end
        include_file_path: true
        include_file_name: false
        operators:
          # Parse CRI log format
          - type: router
            id: get-format
            routes:
              - output: parser-docker
                expr: 'body matches "^\\{"'
              - output: parser-cri
                expr: 'body matches "^[^ Z]+ "'
              - output: parser-containerd
                expr: 'body matches "^[^ Z]+Z"'
          # Docker JSON log format
          - type: json_parser
            id: parser-docker
            output: extract-metadata-from-filepath
            timestamp:
              parse_from: attributes.time
              layout: '%Y-%m-%dT%H:%M:%S.%LZ'
          # CRI log format
          - type: regex_parser
            id: parser-cri
            regex: '^(?P<time>[^ Z]+) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
            output: extract-metadata-from-filepath
            timestamp:
              parse_from: attributes.time
              layout: '%Y-%m-%dT%H:%M:%S.%L%j'
          # Containerd log format
          - type: regex_parser
            id: parser-containerd
            regex: '^(?P<time>[^ ^Z]+Z) (?P<stream>stdout|stderr) (?P<logtag>[^ ]*) ?(?P<log>.*)$'
            output: extract-metadata-from-filepath
            timestamp:
              parse_from: attributes.time
              layout: '%Y-%m-%dT%H:%M:%S.%LZ'
          # Extract k8s metadata from file path
          - type: regex_parser
            id: extract-metadata-from-filepath
            regex: '^.*\/(?P<namespace>[^_]+)_(?P<pod_name>[^_]+)_(?P<uid>[a-f0-9\-]+)\/(?P<container_name>[^\._]+)\/(?P<restart_count>\d+)\.log$'
            parse_from: attributes["log.file.path"]
          - type: move
            from: attributes.log
            to: body
          - type: move
            from: attributes.stream
            to: attributes["log.iostream"]
          - type: move
            from: attributes.namespace
            to: resource["k8s.namespace.name"]
          - type: move
            from: attributes.pod_name
            to: resource["k8s.pod.name"]
          - type: move
            from: attributes.container_name
            to: resource["k8s.container.name"]
          - type: move
            from: attributes.uid
            to: resource["k8s.pod.uid"]

    processors:
      # Enrich with K8s metadata
      k8sattributes:
        auth_type: serviceAccount
        extract:
          metadata:
            - k8s.pod.name
            - k8s.pod.uid
            - k8s.namespace.name
            - k8s.node.name
            - k8s.deployment.name
            - k8s.replicaset.name
            - k8s.statefulset.name
            - k8s.daemonset.name
            - k8s.job.name
            - k8s.cronjob.name
            - k8s.container.name
          labels:
            - tag_name: k8s.pod.label.app
              key: app
              from: pod
            - tag_name: k8s.pod.label.app.kubernetes.io/name
              key: app.kubernetes.io/name
              from: pod
        pod_association:
          - sources:
              - from: resource_attribute
                name: k8s.pod.ip
          - sources:
              - from: resource_attribute
                name: k8s.pod.uid
          - sources:
              - from: connection

      # Stamp with cluster name and default service name
      resource:
        attributes:
          - key: k8s.cluster.name
            value: {{ .Values.clusterName | quote }}
            action: upsert
          - key: service.name
            value: "kubernetes-agent-{{ .Values.clusterName }}"
            action: upsert

      # Set service.name from deployment/statefulset/daemonset name when available
      transform:
        log_statements:
          - context: resource
            statements:
              - set(attributes["service.name"], attributes["k8s.deployment.name"]) where attributes["k8s.deployment.name"] != nil and attributes["k8s.deployment.name"] != ""
              - set(attributes["service.name"], attributes["k8s.statefulset.name"]) where attributes["k8s.statefulset.name"] != nil and attributes["k8s.statefulset.name"] != "" and attributes["k8s.deployment.name"] == nil
              - set(attributes["service.name"], attributes["k8s.daemonset.name"]) where attributes["k8s.daemonset.name"] != nil and attributes["k8s.daemonset.name"] != "" and attributes["k8s.deployment.name"] == nil and attributes["k8s.statefulset.name"] == nil
        metric_statements:
          - context: resource
            statements:
              - set(attributes["service.name"], attributes["k8s.deployment.name"]) where attributes["k8s.deployment.name"] != nil and attributes["k8s.deployment.name"] != ""
              - set(attributes["service.name"], attributes["k8s.statefulset.name"]) where attributes["k8s.statefulset.name"] != nil and attributes["k8s.statefulset.name"] != "" and attributes["k8s.deployment.name"] == nil
              - set(attributes["service.name"], attributes["k8s.daemonset.name"]) where attributes["k8s.daemonset.name"] != nil and attributes["k8s.daemonset.name"] != "" and attributes["k8s.deployment.name"] == nil and attributes["k8s.statefulset.name"] == nil

      batch:
        send_batch_size: 1024
        timeout: 10s

      memory_limiter:
        check_interval: 5s
        limit_mib: 400
        spike_limit_mib: 100

    exporters:
      otlphttp:
        endpoint: "{{ .Values.oneuptime.url }}/otlp"
        headers:
          x-oneuptime-token: "${env:ONEUPTIME_API_KEY}"

    service:
      extensions:
        - health_check
      pipelines:
        metrics:
          receivers:
            - kubeletstats
          processors:
            - memory_limiter
            - k8sattributes
            - resource
            - transform
            - batch
          exporters:
            - otlphttp
        logs:
          receivers:
            - filelog
          processors:
            - memory_limiter
            - k8sattributes
            - resource
            - transform
            - batch
          exporters:
            - otlphttp
{{- end }}
