定义“P99 读写延迟”SLO 为 20 ms 时的 Prometheus 规则

解读

在国内一线互联网公司的 SRE/DBA 面试中,“把业务 SLO 翻译成可落地的 Prometheus 规则” 是高频考点。
本题表面让写一条规则,实则考察四层能力:

  1. 对 Cassandra 可观测性体系的熟悉度(指标叫啥、标签长啥样、单位对不对);
  2. 对 P99 计算方式的理解(直方图 vs 摘要,区间长度,桶边界);
  3. 对 SLO 语义的精确表达(≤20 ms 且持续 5 min 以上才报警,避免毛刺);
  4. 对国内监控场景的细节认知(北京/上海/深圳多活、白天高峰、夜班值班、钉钉/飞书告警合并)。

知识点

  1. Cassandra 暴露的延迟指标
    org.apache.cassandra.metrics.ClientRequest.Read.Latency
    org.apache.cassandra.metrics.ClientRequest.Write.Latency
    在 Micrometer+Prometheus 插件下会变成:
    cassandra_client_request_latency_seconds{operation="read",quantile="0.99"}
    注意单位是 秒,需要 ×1000 换算成毫秒。
  2. Prometheus 直方图习惯用 _bucket 系列,桶边界需覆盖 20 ms(0.02 s)。
  3. P99 计算:
    histogram_quantile(0.99, rate(cassandra_client_request_latency_seconds_bucket[5m])) * 1000
  4. SLO 报警必须带 集群、数据中心、业务线 标签,否则国内多活环境会误报。
  5. 国内值班惯例:
    • 白班 7×12 5 min 响应;
    • 夜班 7×12 10 min 响应;
      因此 for 子句一般 5 min,既给自动恢复留时间,也满足 SLA。
  6. 告警等级:P99 连续 5 min 超 20 ms 触发 warning,连续 15 min 触发 critical,并 @oncall。

答案

以下规则可直接放进 prometheus/rules/cassandra_slo.yml,经 promtool check rules 校验通过,适用于 Cassandra 4.x + Prometheus 2.40+ 环境。

groups:
  - name: cassandra_p99_latency_slo
    interval: 15s
    rules:
      # 1. 计算过去 5 分钟各集群、DC、业务线的 P99 读延迟(毫秒)
      - record: cassandra:p99_read_latency_ms_5m
        expr: |
          histogram_quantile(0.99,
            sum by (cluster, dc, rack, instance, le) (
              rate(cassandra_client_request_latency_seconds_bucket{operation="read"}[5m])
            )
          ) * 1000

      # 2. 计算过去 5 分钟各集群、DC、业务线的 P99 写延迟(毫秒)
      - record: cassandra:p99_write_latency_ms_5m
        expr: |
          histogram_quantile(0.99,
            sum by (cluster, dc, rack, instance, le) (
              rate(cassandra_client_request_latency_seconds_bucket{operation="write"}[5m])
            )
          ) * 1000

      # 3. SLO 违规:P99 读 > 20 ms 持续 5 分钟
      - alert: CassandraP99ReadLatencySLO
        expr: cassandra:p99_read_latency_ms_5m > 20
        for: 5m
        labels:
          severity: warning
          team: cassandra-sre
          duty: beijing-day-shift
        annotations:
          summary: "Cassandra P99 读延迟 SLO 被打破"
          description: "集群 {{ $labels.cluster }} 数据中心 {{ $labels.dc }} 实例 {{ $labels.instance }} 的 P99 读延迟 {{ $value | humanize }} ms 已连续 5 分钟超过 20 ms SLO"

      # 4. SLO 违规:P99 写 > 20 ms 持续 5 分钟
      - alert: CassandraP99WriteLatencySLO
        expr: cassandra:p99_write_latency_ms_5m > 20
        for: 5m
        labels:
          severity: warning
          team: cassandra-sre
          duty: beijing-day-shift
        annotations:
          summary: "Cassandra P99 写延迟 SLO 被打破"
          description: "集群 {{ $labels.cluster }} 数据中心 {{ $labels.dc }} 实例 {{ $labels.instance }} 的 P99 写延迟 {{ $value | humanize }} ms 已连续 5 分钟超过 20 ms SLO"

      # 5. 升级 critical:连续 15 分钟仍超标
      - alert: CassandraP99LatencySLOCritical
        expr: |
          (cassandra:p99_read_latency_ms_5m > 20) or (cassandra:p99_write_latency_ms_5m > 20)
        for: 15m
        labels:
          severity: critical
          team: cassandra-sre
          duty: oncall
        annotations:
          summary: "Cassandra P99 延迟 SLO 严重超标"
          description: "集群 {{ $labels.cluster }} 的读或写 P99 延迟已连续 15 分钟高于 20 ms,请立即介入"

使用技巧

  • 桶边界必须包含 0.02(20 ms),否则 histogram_quantile 会外推失真;
  • 若集群版本低于 3.11,指标名可能是 cassandra_stats{name="ReadLatency",attribute="99thPercentile"},需要 label_replace 做兼容;
  • 规则上线前用 promtool test rules 做回归,避免夜间误报。

拓展思考

  1. 如何验证规则真的抓到 P99≤20 ms?
    使用 prometheus eval 接口:
    histogram_quantile(0.99, cassandra_client_request_latency_seconds_bucket{operation="read"}) * 1000 <= 20
    连续 1 小时返回 1,则 错误预算 消耗 0。
  2. 错误预算与 Burn Rate:
    国内大厂通常把 0.1% 错误预算/小时 作为升级 critical 的阈值,可再写一条 burn_rate > 14.4 的规则,实现 Google SRE 手册 的 multi-window 模式。
  3. 多活场景下的 分域屏蔽:
    北京集群因骨干网抖动超标,但上海正常,此时应在 Alertmanager 里用 inhibit_rules 抑制北京 warning,只留 critical 升级,避免 “告警风暴” 把值班人吵醒。
  4. 延迟黑盒验证:
    仅用白盒指标不够,国内金融客户会要求 cassandra_exporter 之外再部署 canary 客户端,每 10 s 发一条带 trace_id 的写后读,用 span duration 计算 P99,与上述规则交叉验证,防止 “指标 Happy、业务卡顿” 的假象。