{{- if and (eq (include "opentelemetry-collector.serviceEnabled" .) "true") .Values.serviceMonitor.enabled .Values.serviceMonitor.metricsEndpoints }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "opentelemetry-collector.fullname" . }}
  namespace: {{ template "opentelemetry-collector.namespace" . }}
  labels:
    {{- include "opentelemetry-collector.labels" . | nindent 4 }}
    {{- range $key, $value := .Values.serviceMonitor.extraLabels }}
    {{- printf "%s: %s" $key (tpl $value $ | quote) | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      {{- include "opentelemetry-collector.selectorLabels" . | nindent 6 }}
      {{- include "opentelemetry-collector.component" . | nindent 6 }}
  endpoints:
  {{- toYaml .Values.serviceMonitor.metricsEndpoints | nindent 4 }}
      {{- with .Values.serviceMonitor.relabelings }}
      relabelings:
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.serviceMonitor.metricRelabelings }}
      metricRelabelings:
      {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
