{{- if and .Values.monitoring.enabled .Values.monitoring.serviceMonitor.enabled -}}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "agent-monitor.fullname" . }}
  {{- if .Values.monitoring.serviceMonitor.namespace }}
  namespace: {{ .Values.monitoring.serviceMonitor.namespace }}
  {{- end }}
  labels:
    {{- include "agent-monitor.labels" . | nindent 4 }}
    {{- with .Values.monitoring.serviceMonitor.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      {{- include "agent-monitor.selectorLabels" . | nindent 6 }}
  {{- if .Values.monitoring.serviceMonitor.namespace }}
  namespaceSelector:
    matchNames:
      - {{ .Release.Namespace }}
  {{- end }}
  endpoints:
    - port: http
      path: {{ .Values.monitoring.serviceMonitor.path | default "/api/metrics" }}
      interval: {{ .Values.monitoring.serviceMonitor.interval | default "30s" }}
      scrapeTimeout: {{ .Values.monitoring.serviceMonitor.scrapeTimeout | default "10s" }}
      honorLabels: {{ .Values.monitoring.serviceMonitor.honorLabels | default false }}
{{- end }}
