{{- if and (.Values.metrics.enabled) (eq .Values.metrics.type "prometheus") }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "emqx.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "emqx.name" . }}
    helm.sh/chart: {{ include "emqx.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- if .Values.service.labels }}
    {{- toYaml .Values.service.labels | nindent 4 }}
  {{- end }}
  {{- if .Values.service.annotations }}
  annotations:
    {{- toYaml .Values.service.annotations | nindent 4 }}
  {{- end }}
spec:
  endpoints:
  - interval: 10s
    port: dashboard
    scheme: http
    path: /api/v5/prometheus/stats
    params:
      type:
        - prometheus
    basicAuth:
      password:
        name: {{ include "emqx.fullname" . }}-basic-auth
        key: password
      username:
        name: {{ include "emqx.fullname" . }}-basic-auth
        key: username
  jobLabel: {{ .Release.Name }}-scraping
  namespaceSelector:
    matchNames:
      -  {{ .Release.Namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "emqx.name" . }}
{{- end }}
