{{- if .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
{{- if .Values.serviceMonitor.labels }}
  labels:
{{ toYaml .Values.serviceMonitor.labels | indent 4}}
{{- end }}
  name: {{ template "prometheus-postgres-exporter.fullname" . }}
{{- if .Values.serviceMonitor.namespace }}
  namespace: {{ .Values.serviceMonitor.namespace }}
{{- end }}
spec:
  endpoints:
  - targetPort: {{ .Values.service.name }}
{{- if .Values.serviceMonitor.interval }}
    interval: {{ .Values.serviceMonitor.interval }}
{{- end }}
{{- if .Values.serviceMonitor.telemetryPath }}
    path: {{ .Values.serviceMonitor.telemetryPath }}
{{- end }}
{{- if .Values.serviceMonitor.timeout }}
    scrapeTimeout: {{ .Values.serviceMonitor.timeout }}
{{- end }}
{{- if .Values.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ toYaml .Values.serviceMonitor.metricRelabelings | indent 4 }}
{{- end }}
  jobLabel: {{ template "prometheus-postgres-exporter.fullname" . }}
  namespaceSelector:
    matchNames:
    - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app: {{ template "prometheus-postgres-exporter.name" . }}
      release: {{ .Release.Name }}
{{- if .Values.serviceMonitor.targetLabels }}
  targetLabels:
{{- range .Values.serviceMonitor.targetLabels }}
  - {{ . }}
{{- end }}
{{- end }}
{{- end }}
