{{ define "agent.config.traces" }}
// Traces
{{- with .Values.traces }}
otelcol.processor.batch "trace_batch_processor" {
  send_batch_size = {{ .processors.batch.size | int }}
  send_batch_max_size = {{ .processors.batch.maxSize | int }}
  timeout = {{ .processors.batch.timeout | quote}}
  output {
    traces = [otelcol.processor.transform.trace_transform_processor.input]
  }
}
{{- end }}

otelcol.processor.transform "trace_transform_processor" {
  error_mode = "ignore"

  trace_statements {
    context = "resource"
    statements = [
      // Accessing a map with a key that does not exist will return nil.
      "set(attributes[\"k8s.cluster.name\"], \"{{ required ".Values.cluster.name is a required value. Please set it and try again." .Values.cluster.name }}\") where attributes[\"k8s.cluster.name\"] == nil",
      "set(attributes[\"cluster\"], \"{{ required ".Values.cluster.name is a required value. Please set it and try again." .Values.cluster.name }}\") where attributes[\"cluster\"] == nil",
    ]
  }

  output {
    {{- if eq .Values.externalServices.tempo.protocol "otlp" }}
    traces = [otelcol.exporter.otlp.traces_service.input]
    {{- else if eq .Values.externalServices.tempo.protocol "otlphttp" }}
    traces = [otelcol.exporter.otlphttp.traces_service.input]
    {{- end }}
  }
}

{{- end }}
