{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "rabbitmq.fullname" . }}
  {{- if .Values.metrics.serviceMonitor.namespace }}
  namespace: {{ .Values.metrics.serviceMonitor.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    app: {{ template "rabbitmq.name" . }}
    chart: {{ template "rabbitmq.chart" .  }}
    heritage: "{{ .Release.Service }}"
    release: {{ if .Values.metrics.serviceMonitor.release }}"{{ .Values.metrics.serviceMonitor.release }}"{{ else }}"{{ .Release.Name }}"{{ end }}
    {{- if .Values.metrics.serviceMonitor.additionalLabels }}
{{ toYaml .Values.metrics.serviceMonitor.additionalLabels | indent 4 }}
    {{- end }}
spec:
  endpoints:
  - port: metrics
    interval: {{ .Values.metrics.serviceMonitor.interval }}
    {{- if .Values.metrics.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.metrics.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.metrics.serviceMonitor.honorLabels }}
    {{- if .Values.metrics.serviceMonitor.relabellings }}
    metricRelabelings:
{{ toYaml .Values.metrics.serviceMonitor.relabellings | indent 6 }}
    {{- end }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  selector:
    matchLabels:
      app: {{ template "rabbitmq.name" . }}
      release: "{{ .Release.Name }}"
{{- end }}
