{{ define "agent.config.prometheus" }}
{{- with .Values.externalServices.prometheus }}
// Grafana Cloud Prometheus
local.file "prometheus_host" {
  filename  = "/etc/grafana-agent-credentials/prometheus_host"
}
{{ if .basicAuth.username }}
local.file "prometheus_username" {
  filename  = "/etc/grafana-agent-credentials/prometheus_username"
}
{{- end }}
{{ if .basicAuth.password }}
local.file "prometheus_password" {
  filename  = "/etc/grafana-agent-credentials/prometheus_password"
  is_secret = true
}
{{- end }}
{{ if .tenantId }}
local.file "prometheus_tenantid" {
  filename  = "/etc/grafana-agent-credentials/prometheus_tenantId"
}
{{- end }}
prometheus.remote_write "grafana_cloud_prometheus" {
  endpoint {
    url = nonsensitive(local.file.prometheus_host.content) + "{{ .writeEndpoint }}"
{{- if .tenantId }}
    headers = { "X-Scope-OrgID" = local.file.prometheus_tenantid.content }
{{- end }}
{{- if .proxyURL }}
    proxy_url = {{ .proxyURL | quote }}
{{- end }}
{{ if .basicAuth }}
    basic_auth {
      {{ if .basicAuth.username }}username = local.file.prometheus_username.content{{ end }}
      {{ if .basicAuth.password }}password = local.file.prometheus_password.content{{ end }}
    }
{{- end }}
{{ if .writeRelabelConfigRules }}
{{ .writeRelabelConfigRules | indent 4 }}
{{- 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 }}
