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:
    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: ['...']
      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: customer-webhook-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: ['...']
      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: customer-webhook-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: ['...']
      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: customer-webhook-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: ['...']
      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: customer-webhook-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: customer-webhook-{{ .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: ['...']
      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: '=~'
  receivers:
    - name: junkyard
    - name: jiralert-{{ .Values.env }}
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-{{ .Values.env }}-major
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-{{ .Values.env }}-warning
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
{{- if eq .Values.env "prod" }}
    - name: jiralert-staging
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-staging-major
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-staging-warning
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-dev
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-dev-major
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: jiralert-dev-warning
      webhookConfigs:
        - sendResolved: true
          url: http://eelb-1570947222.us-west-1.elb.amazonaws.com:9097/alert
    - name: customer-dev
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: https://us-west-2.nightly.shared.aws.tidbcloud.com/api/useralerts/v1/alert
    - name: customer-staging
      webhookConfigs:
      # send alert to staging internal-portal-server-http server
      - url: https://us-west-2.staging.shared.aws.tidbcloud.com/api/useralerts/v1/alert
    - name: customer-prod
      webhookConfigs:
      - url: https://us-west-2.prod.aws.tidbcloud.com/api/useralerts/v1/alert
    - name: customer-webhook-dev
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: https://us-west-2.nightly.shared.aws.tidbcloud.com/api/useralerts/v1/alert-webhook
    - name: customer-webhook-staging
      webhookConfigs:
      # send alert to staging internal-portal-server-http server
      - url: https://us-west-2.staging.shared.aws.tidbcloud.com/api/useralerts/v1/alert-webhook
    - name: customer-webhook-prod
      webhookConfigs:
      - url: https://us-west-2.prod.aws.tidbcloud.com/api/useralerts/v1/alert-webhook
{{- end }}
{{- if eq .Values.env "dev" }}
    - name: customer-dev
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: https://us-west-2.nightly.shared.aws.tidbcloud.com/api/useralerts/v1/alert
    - name: customer-webhook-dev
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: https://us-west-2.nightly.shared.aws.tidbcloud.com/api/useralerts/v1/alert-webhook
{{- end }}
{{- if eq .Values.env "staging" }}
    - name: customer-staging
      webhookConfigs:
      # send alert to dev internal-portal-server-http server
      - url: https://us-west-2.staging.shared.aws.tidbcloud.com/api/useralerts/v1/alert
    - name: customer-webhook-staging
      webhookConfigs:
      # send alert to staging internal-portal-server-http server
      - url: https://us-west-2.staging.shared.aws.tidbcloud.com/api/useralerts/v1/alert-webhook
{{- end }}