{{ define "alloy.config.logs.pod_logs_processor" }}
loki.process "pod_logs" {
  stage.match {
    selector = "{tmp_container_runtime=~\"containerd|cri-o\"}"
    // the cri processing stage extracts the following k/v pairs: log, stream, time, flags
    stage.cri {}

    // Set the extract flags and stream values as labels
    stage.labels {
      values = {
        flags  = "",
        stream  = "",
      }
    }
  }

  stage.match {
    selector = "{tmp_container_runtime=\"docker\"}"
    // the docker processing stage extracts the following k/v pairs: log, stream, time
    stage.docker {}

    // Set the extract stream value as a label
    stage.labels {
      values = {
        stream  = "",
      }
    }
  }
{{ if .Values.logs.pod_logs.structuredMetadata }}
  // Move high-cardinality labels to structured metadata
  stage.structured_metadata {
    values = {
      {{- range $label, $lokiLabel := .Values.logs.pod_logs.structuredMetadata }}
      {{ $label }} = {{ $lokiLabel | quote }},
      {{- end }}
    }
  }
{{- end }}
  // Drop the filename label, since it's not really useful in the context of Kubernetes, where we already have cluster,
  // namespace, pod, and container labels. Drop any structured metadata. Also drop the temporary
  // container runtime label as it is no longer needed.
  stage.label_drop {
    values = [
      "filename",
      "tmp_container_runtime",
{{- if .Values.logs.pod_logs.structuredMetadata }}
  {{- range $label, $lokiLabel := .Values.logs.pod_logs.structuredMetadata }}
      {{ $lokiLabel | default $label | quote }},
  {{- end }}
{{- end }}
    ]
  }

{{- if .Values.logs.pod_logs.extraStageBlocks }}
{{ tpl .Values.logs.pod_logs.extraStageBlocks $ | indent 2 }}
{{ end }}
  forward_to = [loki.process.logs_service.receiver]
}
{{ end }}
