{{ define "agent.config.metricsServiceRemoteWrite" }}
{{- with .Values.externalServices.prometheus }}
prometheus.remote_write "metrics_service" {
  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 or (.basicAuth.username) (.basicAuth.password) }}
    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 }}
{{- if .tls }}
    tls_config {
    {{- range $k, $v := .tls }}
      {{ $k }} = {{ $v | toJson }}
    {{- 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 }}
