{{- if .Values.logIngest.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-frontend-otelcol-config
  namespace: {{ .Release.Namespace }}
data:
  otelcol-contrib.yaml: |
    receivers:
      filelog:
        include:
          - {{ .Values.logIngest.frontendLogPathPattern }}
        start_at: end
        include_file_path: false
        include_file_name: false
        operators:
          - type: json_parser
            parse_from: body
            parse_to: attributes
    processors:
      resource:
        attributes:
          - action: insert
            key: k8s.node.name
            value: ${KUBE_META_NODE_NAME}
          - action: insert
            key: k8s.pod.name
            value: ${KUBE_META_POD_NAME}
          - action: insert
            key: k8s.pod.ip
            value: ${KUBE_META_POD_IP}
          - action: insert
            key: k8s.pod.uid
            value: ${KUBE_META_POD_UID}
          - action: insert
            key: k8s.namespace.name
            value: ${KUBE_META_NAMESPACE}
          - action: insert
            key: k8s.deployment.name
            value: ${KUBE_META_OBJECT_NAME}
          - action: insert
            key: service.name
            value: magicleaps-frontend
          - action: insert
            key: service.component
            value: frontend
      transform:
        log_statements:
          - context: log
            statements:
              - set(resource.attributes["application"], log.attributes["context"]["app"])
              - set(resource.attributes["environment"], log.attributes["context"]["env"])
              - set(resource.attributes["kubernetes_node_name"], resource.attributes["k8s.node.name"])
              - set(resource.attributes["kubernetes_pod_name"], resource.attributes["k8s.pod.name"])
              - set(resource.attributes["kubernetes_pod_ip"], resource.attributes["k8s.pod.ip"])
              - set(resource.attributes["kubernetes_deployment_name"], resource.attributes["k8s.deployment.name"])
              - set(resource.attributes["kubernetes_namespace"], resource.attributes["k8s.namespace.name"])
              - set(resource.attributes["body_json"], ParseJSON(log.body))
              - set(resource.attributes["body_json"]["kubernetes"]["pod"], resource.attributes["k8s.pod.name"])
              - set(resource.attributes["body_json"]["kubernetes"]["namespace"], resource.attributes["k8s.namespace.name"])
              - set(resource.attributes["body_json"]["kubernetes"]["pod_ip"], resource.attributes["k8s.pod.ip"])
              - set(resource.attributes["body_json"]["kubernetes"]["pod_uid"], resource.attributes["k8s.pod.uid"])
              - set(resource.attributes["body_json"]["kubernetes"]["deployment"], resource.attributes["k8s.pod.name"])
              - set(resource.attributes["body_json"]["kubernetes"]["node"], resource.attributes["k8s.node.name"])
              - set(resource.attributes["body_json"]["kubernetes"]["namespace"], resource.attributes["k8s.namespace.name"])
              - set(resource.attributes["body_json"]["service"]["name"], "magicleaps-frontend")
              - set(resource.attributes["body_json"]["service"]["component"], "frontend")
              - set(log.body, resource.attributes["body_json"])
              - delete_key(resource.attributes, "body_json")
      batch:
        send_batch_size: 5
        timeout: 10s  
    exporters:
      otlphttp/logs:
        endpoint: {{ .Values.logIngest.lokiEndpoint }}/otlp
        tls:
          insecure: true
    service:
      telemetry:
        logs:
          level: info
      pipelines:
        logs:
          receivers: [filelog]
          processors: [resource, transform, batch]
          exporters: [otlphttp/logs]
{{- end }}
