# Prometheus 抓取配置片段（合并到 prometheus.yml 的 scrape_configs:）
# 启动：docker run --network host -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: prod-1
    region: cn-bj-1

rule_files:
  - "kafka_alerts.yml"

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

scrape_configs:
  # ===== Broker JMX Exporter =====
  - job_name: kafka-broker
    metrics_path: /metrics
    static_configs:
      - targets:
          - kafka1:7071
          - kafka2:7071
          - kafka3:7071
        labels:
          role: broker
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        regex: '([^:]+):.*'
        replacement: '$1'

  # ===== kafka-exporter (Lag 专用) =====
  # https://github.com/danielqsj/kafka_exporter
  - job_name: kafka-exporter
    static_configs:
      - targets: ['kafka-exporter:9308']

  # ===== Schema Registry =====
  - job_name: schema-registry
    metrics_path: /metrics
    static_configs:
      - targets: ['schema-registry:5556']

  # ===== Connect Worker =====
  - job_name: kafka-connect
    metrics_path: /metrics
    static_configs:
      - targets: ['connect:7072']

# =====================================================
# 一份开箱即用的告警规则（可单独保存为 kafka_alerts.yml）
# =====================================================
# groups:
# - name: kafka.rules
#   rules:
#   - alert: KafkaUnderReplicated
#     expr: sum(kafka_server_replicamanager_underreplicatedpartitions) by (instance) > 0
#     for: 5m
#     labels: { severity: warning, team: data-infra }
#     annotations:
#       summary: "{{ $labels.instance }} 副本不全 ({{ $value }} 分区)"
#
#   - alert: KafkaOfflinePartitions
#     expr: sum(kafka_controller_kafkacontroller_offlinepartitionscount) > 0
#     for: 1m
#     labels: { severity: critical, page: oncall }
#     annotations:
#       summary: "Kafka 集群有 Offline 分区！"
#
#   - alert: KafkaActiveControllerWrong
#     expr: sum(kafka_controller_kafkacontroller_activecontrollercount) != 1
#     for: 1m
#     labels: { severity: critical }
#
#   - alert: KafkaUncleanLeaderElection
#     expr: sum(rate(kafka_controller_controllerstats_uncleanleaderelectionspersec_count[5m])) > 0
#     labels: { severity: critical }
#     annotations: { summary: "出现 Unclean Leader Election，可能丢数据！" }
#
#   - alert: KafkaConsumerLagHigh
#     expr: kafka_consumergroup_lag > 10000
#     for: 10m
#     labels: { severity: warning }
#
#   - alert: KafkaRequestHandlerLow
#     expr: kafka_server_kafkarequesthandlerpool_requesthandleravgidlepercent < 0.2
#     for: 10m
#     labels: { severity: warning }
#     annotations: { summary: "{{ $labels.instance }} I/O 线程繁忙，考虑加 num.io.threads" }
