apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  # Be careful to change the name and namespace, it's related to the receiver name,
  # which is used to configure in the jiralert.
  name: observability-alertmanager-config
  namespace: default
  labels:
    alertmanagerConfig: observability-alertmanager-config
spec:
  route:
{{- if .Values.alertmanager.route }}
    {{- toYaml .Values.alertmanager.route | nindent 4 }}
{{- else }}
    groupBy:
      - alertname
      - level
      - severity
      - priority
      - rule
      - cluster
      - tenant
      - cluster_id
    groupWait: 30s
    receiver: junkyard
    repeatInterval: 30m
    routes:
    # Jiralert Service Routes
{{- if eq .Values.env "prod" }}
{{/* we are storing all the tidbcloud metrics under o11y prod */}}
    - receiver: jiralert-dev
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'k8s_cluster_info', 'o11y_cluster_info', 'cluster', 'severity', 'namespace', 'changefeed']
      continue: false
      groupWait: 10s
      groupInterval: 10m
      repeatInterval: 2h
      matchers:
      - name: severity
        value: critical|emergency|blocker|major|warning
        matchType: '=~'
      - name: o11y_biz_type
        value: dev-(.*)
        matchType: '=~'
      routes:
        - receiver: customer-dev
          repeatInterval: 5m
          continue: true
          matchers:
          - name: visibility
            value: external
            matchType: '='
        - receiver: jiralert-dev-warning
          matchers:
          - name: severity
            value: warning
            matchType: '=~'
        - receiver: jiralert-dev-major
          matchers:
          - name: severity
            value: major
            matchType: '=~'
    - receiver: jiralert-dev
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'k8s_cluster_info', 'o11y_cluster_info', 'cluster', 'severity', 'namespace', 'changefeed']
      continue: false
      groupWait: 10s
      groupInterval: 10m
      repeatInterval: 2h
      matchers:
      - name: severity
        value: critical|emergency|blocker|major|warning
        matchType: '=~'
      - name: o11y_tenant_id
        value: infra-dev
        matchType: '='
      routes:
        - receiver: customer-dev
          continue: true
          repeatInterval: 5m
          matchers:
          - name: visibility
            value: external
            matchType: '='
        - receiver: jiralert-dev-warning
          matchers:
          - name: severity
            value: warning
            matchType: '=~'
        - receiver: jiralert-dev-major
          matchers:
          - name: severity
            value: major
            matchType: '=~'
    - receiver: jiralert-staging
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'k8s_cluster_info', 'o11y_cluster_info', 'cluster', 'severity', 'namespace', 'changefeed']
      continue: false
      groupWait: 10s
      groupInterval: 10m
      repeatInterval: 2h
      matchers:
      - name: severity
        value: critical|emergency|blocker|major|warning
        matchType: '=~'
      - name: o11y_biz_type
        value: staging-(.*)
        matchType: '=~'
      routes:
        - receiver: customer-staging
          repeatInterval: 5m
          continue: true
          matchers:
          - name: visibility
            value: external
            matchType: '='
        - receiver: jiralert-staging-warning
          matchers:
          - name: severity
            value: warning
            matchType: '=~'
        - receiver: jiralert-staging-major
          matchers:
          - name: severity
            value: major
            matchType: '=~'
    - receiver: jiralert-staging
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'k8s_cluster_info', 'o11y_cluster_info', 'cluster', 'severity', 'namespace', 'changefeed']
      continue: false
      groupWait: 10s
      groupInterval: 10m
      repeatInterval: 2h
      matchers:
      - name: severity
        value: critical|emergency|blocker|major|warning
        matchType: '=~'
      - name: o11y_tenant_id
        value: infra-staging
        matchType: '=~'
      routes:
        - receiver: customer-staging
          repeatInterval: 5m
          continue: true
          matchers:
          - name: visibility
            value: external
            matchType: '='
        - receiver: jiralert-staging-warning
          matchers:
          - name: severity
            value: warning
            matchType: '=~'
        - receiver: jiralert-staging-major
          matchers:
          - name: severity
            value: major
            matchType: '=~'
{{- end }}
    # for prod env, route the rest of alerts to jiralert-prod
    - receiver: customer-{{ .Values.env }}
      repeatInterval: 3h
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'job_id', 'changefeed']
      continue: true
      matchers:
      - name: visibility
        value: external
        matchType: '='
    - receiver: jiralert-{{ .Values.env }}
      groupBy: ['alertname', 'cluster_id', 'tidb_cluster_id', 'k8s_cluster_info', 'o11y_cluster_info', 'cluster', 'severity', 'namespace', 'changefeed']
      continue: true
      groupWait: 10s
      groupInterval: 10m
      repeatInterval: 2h
      matchers:
      - name: severity
        value: critical|emergency|blocker|major|warning
        matchType: '=~'
      routes:
        - receiver: jiralert-{{ .Values.env }}-warning
          matchers:
          - name: severity
            value: warning
            matchType: '=~'
        - receiver: jiralert-{{ .Values.env }}-major
          matchers:
          - name: severity
            value: major
            matchType: '=~'
{{- end }}
  receivers:
{{- if .Values.alertmanager.receivers }}
    {{- toYaml .Values.alertmanager.receivers | nindent 4 }}
{{- else }}
    - name: junkyard

{{- if ne .Values.env "prod" }}
    - name: jiralert-{{ .Values.env }}
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-{{ .Values.env }}-major
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-{{ .Values.env }}-warning
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
{{- end }}
{{- if eq .Values.env "prod" }}
    - name: jiralert-prod
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-prod.pingcap.net/alert
    - name: jiralert-prod-major
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-prod.pingcap.net/alert
    - name: jiralert-prod-warning
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-prod.pingcap.net/alert
    - name: jiralert-staging
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-staging-major
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-staging-warning
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-dev
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-dev-major
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: jiralert-dev-warning
      webhookConfigs:
        - sendResolved: true
          url: https://jiralert-eks-dev.pingcap.net/alert
    - name: customer-dev
      webhookConfigs:
      # send alert to dev clinic-cloud-server
      - url: http://dev-clinic.pingcap.com:8085/api/alertInternal/v1/alertNotification
    - name: customer-staging
      webhookConfigs:
      # send alert to staging clinic-cloud-server
      - url: http://staging-clinic.pingcap.com:8085/api/alertInternal/v1/alertNotification
    - name: customer-prod
      webhookConfigs:
      - url: http://clinic.pingcap.com:8085/api/alertInternal/v1/alertNotification
{{- end }}
{{- if eq .Values.env "dev" }}
    - name: customer-dev
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: http://dev-clinic.pingcap.com:8085/api/alertInternal/v1/alertNotification
{{- end }}
{{- if eq .Values.env "staging" }}
    - name: customer-staging
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: http://staging-clinic.pingcap.com:8085/api/alertInternal/v1/alertNotification
{{- end }}
{{- end }}
