apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
  name: agent-monitor-canary-analysis
  namespace: agent-monitor
  labels:
    app.kubernetes.io/name: agent-monitor
    app.kubernetes.io/instance: agent-monitor
    app.kubernetes.io/version: "1.0.0"
    app.kubernetes.io/component: canary-analysis
    app.kubernetes.io/managed-by: kustomize
spec:
  args:
    - name: service-name
      value: agent-monitor-canary
    - name: namespace
      value: agent-monitor
  metrics:
    # Success rate must be above 99%
    - name: success-rate
      interval: 60s
      count: 5
      successCondition: result[0] >= 0.99
      failureLimit: 2
      provider:
        prometheus:
          address: http://prometheus.monitoring.svc.cluster.local:9090
          query: |
            sum(
              rate(
                http_requests_total{
                  namespace="{{args.namespace}}",
                  service="{{args.service-name}}",
                  code!~"5.."
                }[2m]
              )
            )
            /
            sum(
              rate(
                http_requests_total{
                  namespace="{{args.namespace}}",
                  service="{{args.service-name}}"
                }[2m]
              )
            )

    # P99 latency must be under 500ms
    - name: p99-latency
      interval: 60s
      count: 5
      successCondition: result[0] < 500
      failureLimit: 2
      provider:
        prometheus:
          address: http://prometheus.monitoring.svc.cluster.local:9090
          query: |
            histogram_quantile(
              0.99,
              sum(
                rate(
                  http_request_duration_milliseconds_bucket{
                    namespace="{{args.namespace}}",
                    service="{{args.service-name}}"
                  }[2m]
                )
              ) by (le)
            )

    # Error rate must stay below 1%
    - name: error-rate
      interval: 60s
      count: 5
      successCondition: result[0] <= 0.01
      failureLimit: 2
      provider:
        prometheus:
          address: http://prometheus.monitoring.svc.cluster.local:9090
          query: |
            sum(
              rate(
                http_requests_total{
                  namespace="{{args.namespace}}",
                  service="{{args.service-name}}",
                  code=~"5.."
                }[2m]
              )
            )
            /
            sum(
              rate(
                http_requests_total{
                  namespace="{{args.namespace}}",
                  service="{{args.service-name}}"
                }[2m]
              )
            )
