{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and ( include "opensearch.data.enabled" . ) .Values.data.metrics.enabled .Values.data.metrics.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "opensearch.data.fullname" . }}
  namespace: {{ default (include "common.names.namespace" .) .Values.data.metrics.serviceMonitor.namespace | quote }}
  {{- $labels := include "common.tplvalues.merge" (dict "values" (list .Values.data.metrics.serviceMonitor.labels .Values.commonLabels) "context" .) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: opensearch
    app.kubernetes.io/component: data
  {{- if or .Values.data.metrics.serviceMonitor.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.data.metrics.serviceMonitor.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.data.metrics.serviceMonitor.jobLabel | quote }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: opensearch
      app.kubernetes.io/component: data
      prometheus.io/scrape: "true"
      {{- if .Values.data.metrics.serviceMonitor.selector }}
      {{- include "common.tplvalues.render" (dict "value" .Values.data.metrics.serviceMonitor.selector "context" $) | nindent 6 }}
      {{- end }}
  endpoints:
    - port: http-metrics
      path: "/_prometheus/metrics"
      {{- if .Values.data.metrics.serviceMonitor.interval }}
      interval: {{ .Values.data.metrics.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.data.metrics.serviceMonitor.scrapeTimeout }}
      scrapeTimeout: {{ .Values.data.metrics.serviceMonitor.scrapeTimeout }}
      {{- end }}
      {{- if .Values.data.metrics.serviceMonitor.honorLabels }}
      honorLabels: {{ .Values.data.metrics.serviceMonitor.honorLabels }}
      {{- end }}
      {{- if .Values.data.metrics.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.data.metrics.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.data.metrics.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.data.metrics.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
  namespaceSelector:
    matchNames:
    - {{ include "common.names.namespace" . | quote }}
{{- end }}
