# Prometheus 告警规则 - Edu 平台
# 分组：服务可用性 / 性能 / 资源
groups:
  # ============================================================
  # 服务可用性
  # ============================================================
  - name: service-availability
    rules:
      # 服务宕机
      - alert: ServiceDown
        expr: up == 0
        for: 1m
        labels:
          severity: critical
          category: availability
        annotations:
          summary: "服务宕机 {{ $labels.instance }}"
          description: "目标 {{ $labels.job }} / {{ $labels.instance }} 已离线超过 1 分钟。"

      # 熔断器打开
      - alert: CircuitBreakerOpen
        expr: circuit_breaker_state{state="open"} == 1
        for: 30s
        labels:
          severity: critical
          category: availability
        annotations:
          summary: "熔断器打开 {{ $labels.service }}"
          description: "服务 {{ $labels.service }} 熔断器处于 open 状态，可能正在拒绝请求。"

  # ============================================================
  # 性能
  # ============================================================
  - name: performance
    rules:
      # 高错误率（5xx 占比 > 5%）
      - alert: HighErrorRate
        expr: |
          (sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
            / sum(rate(http_requests_total[5m])) by (service))
          > 0.05
        for: 5m
        labels:
          severity: warning
          category: performance
        annotations:
          summary: "错误率过高 {{ $labels.service }}"
          description: "服务 {{ $labels.service }} 5xx 错误率超过 5%，持续 5 分钟。"

      # P99 延迟过高
      - alert: HighLatencyP99
        expr: |
          histogram_quantile(0.99,
            sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service))
          > 1
        for: 5m
        labels:
          severity: warning
          category: performance
        annotations:
          summary: "P99 延迟过高 {{ $labels.service }}"
          description: "服务 {{ $labels.service }} P99 延迟超过 1 秒，持续 5 分钟。"

      # Kafka 消费者积压
      - alert: KafkaConsumerLag
        expr: kafka_consumergroup_lag > 1000
        for: 10m
        labels:
          severity: warning
          category: performance
        annotations:
          summary: "Kafka 消费者积压 {{ $labels.consumergroup }}"
          description: "消费组 {{ $labels.consumergroup }} 在 topic {{ $labels.topic }} 上积压超过 1000 条，持续 10 分钟。"

  # ============================================================
  # 资源
  # ============================================================
  - name: resources
    rules:
      # MySQL 连接数过高
      - alert: MySQLConnectionsHigh
        expr: |
          (mysql_global_status_threads_connected
            / mysql_global_variables_max_connections)
          > 0.8
        for: 5m
        labels:
          severity: warning
          category: resource
        annotations:
          summary: "MySQL 连接数过高 {{ $labels.instance }}"
          description: "MySQL 实例 {{ $labels.instance }} 连接数超过最大连接数的 80%，持续 5 分钟。"

      # 磁盘空间不足
      - alert: DiskSpaceLow
        expr: |
          (100
            - (node_filesystem_avail_bytes
              / node_filesystem_size_bytes * 100))
          > 85
        for: 10m
        labels:
          severity: warning
          category: resource
        annotations:
          summary: "磁盘空间不足 {{ $labels.instance }}"
          description: "节点 {{ $labels.instance }} 挂载点 {{ $labels.mountpoint }} 磁盘使用率超过 85%，持续 10 分钟。"

      # 内存使用过高
      - alert: MemoryHigh
        expr: |
          (1
            - (node_memory_MemAvailable_bytes
              / node_memory_MemTotal_bytes))
          > 0.9
        for: 5m
        labels:
          severity: critical
          category: resource
        annotations:
          summary: "内存使用过高 {{ $labels.instance }}"
          description: "节点 {{ $labels.instance }} 内存使用率超过 90%，持续 5 分钟。"
