groups:
  - name: account
    rules:
        # account server alert
      - alert: account_severDown
        annotations:
          description: 'Account server pod {{ $labels.namespace }}/{{ $labels.pod }} has been down for more than 2 minutes.'
          summary: 'Account server pod is down for more than 2 minutes'
        expr: |
          up{job=~"account.*"} == 0
        for: 2m
        labels:
          severity: major
          response_class: ticket
          component: account-server
          oncall_team: account
      #  login_success_rate
      - alert: account_loginFailMoreThan5
        annotations:
          description: 'Account login failures in namespace {{ $labels.namespace }} exceeded 5 within the last 30 minutes.'
          summary: 'Account login failures exceed 5 in 30 minutes'
        expr: |
          (sum(dbaas_account_login_count{eventResult="FAILD", namespace=~".*-ms"}) by (namespace)
            -sum(dbaas_account_login_count{eventResult="FAILD" ,namespace=~".*-ms"} offset 30m) by (namespace)
          )>5
        for: 2m
        labels:
          severity: major
          response_class: ticket
          component: account-server
          oncall_team: account

          #  login_fail_alert
      - alert: account_loginFailMoreThan10
        annotations:
          description: 'Account login failures in namespace {{ $labels.namespace }} exceeded 10 within the last 30 minutes.'
          summary: 'Account login failures exceed 10 in 30 minutes'
        expr: |
          (sum(dbaas_account_login_count{eventResult="FAILD", namespace=~".*-ms"}) by (namespace)
            -sum(dbaas_account_login_count{eventResult="FAILD" ,namespace=~".*-ms"} offset 30m) by (namespace)
          )>10
        for: 5m
        labels:
          severity: critical
          response_class: page
          component: account-server
          oncall_team: account

          #  error_code_alert warning
      - alert: account_errorCodeTooMany(20%)
        annotations:
          description: 'More than 20% of account-central requests in namespace {{ $labels.namespace }} returned 5xx over the last 30 minutes.'
          summary: 'Account-central 5xx ratio exceeds 20% over 30 minutes'
        expr: |
          (sum(increase (kong_http_status{code=~"5.*",route=~".*-ms.account-central(.*)"}[30m])) by (namespace)
            / sum(increase (kong_http_status{route=~".*-ms.account-central(.*)"}[30m]))  by (namespace)
           )>0.2
              and (sum(increase (kong_http_status{code=~"5.*",route=~".*-ms.account-central(.*)"}[30m])) by (namespace)>5 )
        for: 1m
        labels:
          severity: major
          response_class: ticket
          component: account-server
          oncall_team: account

          #  error_code_alert  critical
      - alert: account_errorCodeTooMany(30%)
        annotations:
          description: 'More than 30% of account-central requests in namespace {{ $labels.namespace }} returned 5xx over the last 30 minutes.'
          summary: 'Account-central 5xx ratio exceeds 30% over 30 minutes'
        expr: |
          ((sum(increase (kong_http_status{code=~"5.*",route=~".*-ms.account-central(.*)"}[30m])) by (namespace)
            / sum(increase (kong_http_status{route=~".*-ms.account-central(.*)"}[30m]))  by (namespace)
           )>0.3)
                and (sum(increase (kong_http_status{code=~"5.*",route=~".*-ms.account-central(.*)"}[30m])) by (namespace)>10 )
        for: 1m
        labels:
          severity: critical
          response_class: page
          component: account-server
          oncall_team: account

        #  sla_alert  warning
      - alert: account_slaAlert(80%)
        annotations:
          description: 'Account-central success ratio in namespace {{ $labels.namespace }} dropped below 50% (2xx of 2xx+5xx) over the last 30 minutes, with more than 10 5xx responses.'
          summary: 'Account-central success ratio drops below 50% over 30 minutes'
        expr: |
          ((sum(increase (kong_custom_http_status{kube_service=~"(.*)account-central(.*)",code=~"2(.*)"} [30m])) by (namespace)
            / sum(increase (kong_custom_http_status{kube_service=~"(.*)account-central(.*)",code=~"5(.*)|2(.*)"}[30m]))  by (namespace))<0.5)  and (sum(increase (kong_custom_http_status{kube_service=~"(.*)account-central(.*)",code=~"5(.*)"} [30m])) by (namespace)>10 )

        for: 1m
        labels:
          severity: warning
          component: account-server
          oncall_team: account

        #  pod_restart  warning
      - alert: account_podRestaetAlert
        annotations:
          description: 'Account pod {{ $labels.pod }} restarted more than 2 times within the last 24 hours.'
          summary: 'Account pod restarts more than 2 times in 24 hours'
        expr: |
          sum(increase (kube_pod_container_status_restarts_total{namespace=~"staging-ms|prod-ms", pod=~"account-(.*)"} [24h])) by (pod) > 2
        for: 1m
        labels:
          severity: warning
          component: account-server
          oncall_team: account
