{{- if and .Values.metrics.kafka.enabled .Values.metrics.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ include "kafka.fullname" . }}
  {{- if .Values.metrics.prometheusRule.namespace }}
  namespace: {{ .Values.metrics.prometheusRule.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace | quote }}
  {{- end }}
  labels: {{- include "kafka.labels" . | nindent 4 }}
    app.kubernetes.io/component: kafka
    {{- if .Values.metrics.prometheusRule.additionalLabels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.metrics.prometheusRule.additionalLabels "context" $) | nindent 4 }}
    {{- end }}
spec:
  groups:
  {{- if eq (len .Values.metrics.prometheusRule.rules) 0 }}
    - name: {{ include "kafka.fullname" $ }}
      rules:
        - alert: KafkaTopicsReplicas
          expr: sum(kafka_topic_partition_in_sync_replica) by (topic) < 3
          for: 0m
          labels:
            severity: critical
          annotations:
            summary:  Kafka topic({{ `{{ $labels.topic }}` }}) in-sync partition 
            description: Kafka topic({{ `{{ $labels.topic }}` }}) in-sync partition current values:{{ `{{ $value }}` }}
        - alert: KafkaConsumersGroup
          expr: sum(kafka_consumergroup_lag) by (consumergroup) > 50
          for: 1m
          labels:
            severity: critical
          annotations:
            summary: Kafka consumers group ({{ `{{ $labels.consumergroup }}` }})
            description: Kafka consumers group ({{ `{{ $labels.consumergroup }}` }}) CurrentValue:{{ `{{ $value }}` }}
  {{- else }}
  {{- with .Values.metrics.prometheusRule.rules }}
    - name: {{ template "kafka.fullname" $ }}
      rules: {{- include "common.tplvalues.render" (dict "value" . "context" $) | nindent 8 }}
  {{- end }}
  {{- end }}
{{- end }}
