apiVersion: monitoring.grafana.com/v1alpha1
kind: PodLogs
metadata:
  name: all-logs-fixed
  labels:
    app.kubernetes.io/name: loki
    app.kubernetes.io/instance: loki
spec:
  namespaceSelector:
    any: true
  selector:
    matchLabels: {}
  relabelings:
  - action: replace
    sourceLabels:
    - __meta_kubernetes_pod_node_name
    targetLabel: __host__
  - action: labelmap
    regex: __meta_kubernetes_pod_label_(.+)
  - action: replace
    replacement: $1
    separator: '-'
    sourceLabels:
    - __meta_kubernetes_pod_label_app_kubernetes_io_name
    - __meta_kubernetes_pod_label_app_kubernetes_io_component
    targetLabel: __service__
  - action: replace
    replacement: $1
    separator: /
    sourceLabels:
    - __meta_kubernetes_namespace
    - __service__
    targetLabel: job
  - action: replace
    sourceLabels:
    - __meta_kubernetes_pod_container_name
    targetLabel: container
  - action: replace
    sourceLabels:
    - __meta_kubernetes_namespace
    targetLabel: namespace
  - action: replace
    replacement: {{ .Values.clusterName | default "loki" }}
    targetLabel: cluster
  pipelineStages:
    - cri: {}
    - regex:
        # Capture into a temporary variable 'raw_level' to avoid overwriting existing logic
        # Matches any of the keywords anywhere in the line
        expression: '(?P<raw_level>DEBUG|INFO|WARN|ERROR|LOG|TRACE)'
    - template:
        source: level
        # Use simple equality check on the extracted keyword
        template: '{{ "{{" }} if eq .Extraction.raw_level "LOG" {{ "}}" }}info{{ "{{" }} else if eq .Extraction.raw_level "WARN" {{ "}}" }}warn{{ "{{" }} else if eq .Extraction.raw_level "ERROR" {{ "}}" }}error{{ "{{" }} else if eq .Extraction.raw_level "DEBUG" {{ "}}" }}debug{{ "{{" }} else if eq .Extraction.raw_level "TRACE" {{ "}}" }}trace{{ "{{" }} else if .Extraction.raw_level {{ "}}" }}{{ "{{" }} .Extraction.raw_level | lower {{ "}}" }}{{ "{{" }} else {{ "}}" }}info{{ "{{" }} end {{ "}}" }}'
    - labels:
        level: level
