{{ define "agent.config.loki" }}
{{- with .Values.externalServices.loki }}
// Grafana Cloud Loki
local.file "loki_host" {
  filename  = "/etc/grafana-agent-credentials/loki_host"
}
{{ if .basicAuth.username }}
local.file "loki_username" {
  filename  = "/etc/grafana-agent-credentials/loki_username"
}
{{- end }}
{{ if .basicAuth.password }}
local.file "loki_password" {
  filename  = "/etc/grafana-agent-credentials/loki_password"
  is_secret = true
}
{{- end }}
{{ if .tenantId }}
local.file "loki_tenantid" {
  filename  = "/etc/grafana-agent-credentials/loki_tenantId"
}
{{- end }}
loki.write "grafana_cloud_loki" {
  endpoint {
    url = nonsensitive(local.file.loki_host.content) + "{{ .writeEndpoint }}"
{{- if .tenantId }}
    tenant_id = local.file.loki_tenantid.content
{{- end }}
{{- if .proxyURL }}
    proxy_url = {{ .proxyURL | quote }}
{{- end }}
{{ if .basicAuth }}
    basic_auth {
      {{ if .basicAuth.username }}username = local.file.loki_username.content{{ end }}
      {{ if .basicAuth.password }}password = local.file.loki_password.content{{ end }}
    }
{{- end }}
  }
  external_labels = {
    {{- range $k, $v := .externalLabels }}
    {{ $k }} = {{ $v | quote }},
    {{- end }}
{{- end }}
    cluster = {{ required ".Values.cluster.name is a required value. Please set it and try again." .Values.cluster.name | quote }},
  }
}
{{ end }}
