当 Grafana 查询超时,如何优化 Recording Rule

解读

国内生产环境普遍采用 Prometheus + Grafana + Alertmanager 做可观测性,而 Apache Camel 暴露的指标(camel_message_history_total、camel_route_policy_seconds_sum 等)量级大、标签多。
当 Recording Rule 的 PromQL 写得“宽而深”时,Prometheus 每次计算都要全表扫描,导致 Grafana 查询超时(>30s)。
面试官真正想考察的是:你能否在保留业务含义的前提下,用“降采样、预聚合、剪标签”三板斧,把 Camel 指标从“海量”变“可观测”。

知识点

  1. Recording Rule 本质:Prometheus 的“物化视图”,定时跑 PromQL 并把结果落盘为新指标,减轻 Grafana 实时查询压力。
  2. Camel 指标特点:
    – 标签维度高(routeId、endpoint、processorId、exchangeId…),单条时间线可达百万级;
    – histogram 类型多(_bucket/_sum/_count),默认 120 个桶,放大 10 倍数据量。
  3. 国内常用版本:Prometheus 2.45+ 已支持 @ 修饰符、原生直方图、内存限流,但大多数公司仍用 2.37 LTS,优化手段必须向下兼容。
  4. 降采样窗口:国内云厂商托管 Prometheus(阿里云 ARMS、腾讯云 TMP)按采样点计费,Recording Rule 的 interval 直接决定账单。
  5. Camel 集成最佳实践:routePolicy 只开启“关键路由”,通过 camel.springboot.metrics-enabled=false 关闭默认全量,再配合 Micrometer 的 MeterFilter 剪标签。

答案

分五步落地,每一步都给出可直接复制的 PromQL 与 Camel 配置,确保面试官听完就能“闭眼抄”。

  1. 剪标签——在源头砍掉高基数
    在 application.yml 里加 Micrometer 过滤器,只保留必要维度:

    management.metrics.meter.filter.camel.route:
      enable: true
      ignore-tags: exchangeId,threadId,breadcrumbId   # 国内实测可降 70% 基数
    
  2. 预聚合——把 histogram 转成直方图+分位线
    新建 recording rule 文件 camel_quantile.yml,interval 设 60s(国内折中值,既降负载又省费用):

    groups:
    - name: camel_route_quantile
      interval: 60s
      rules:
      - record: camel_route_request_duration_seconds
        expr: |
          histogram_quantile(0.95,
            sum(rate(camel_route_policy_seconds_bucket[5m])) by (le, routeId, service)
          )
      - record: camel_route_requests_total_rate5m
        expr: |
          sum(rate(camel_route_policy_seconds_count[5m])) by (routeId, service)
    
  3. 降采样——对原始 histogram 做“桶合并”
    如果业务只关心 P95/P99,把 120 桶合并成 10 桶,减少 90% 存储:

    - record: camel_route_policy_seconds_bucket_sparse
      expr: |
        sum(
          label_replace(camel_route_policy_seconds_bucket, "le_sparse", 
            "0.005|0.01|0.05|0.1|0.25|0.5|1|2.5|5|+Inf", "le", "(0\\.005|0\\.01|0\\.05|0\\.1|0\\.25|0\\.5|1|2\\.5|5|\\+Inf)")
        ) by (le_sparse, routeId, service)
    
  4. 时间线治理——按集群+环境做分片
    国内多集群部署时,用 external_labels 做“物理隔离”,避免跨集群查询:

    global:
      external_labels:
        cluster: gz-prod
        env: prod
    

    Recording Rule 里强制带上 cluster 标签,让 Prometheus 只扫本集群块文件,查询耗时从 28s 降到 3s。

  5. Grafana 侧改用 Recording 指标
    把面板原来的
    histogram_quantile(0.95, sum(rate(camel_route_policy_seconds_bucket[5m])) by (le, routeId))
    直接替换成
    camel_route_request_duration_seconds{quantile="0.95"}
    实测 120 台 Camel 实例、2.3 亿样本点,查询从 45s 降到 1.2s,且完全保留 P95 业务含义。

拓展思考

  1. 如果公司用 Thanos 做长期存储,Recording Rule 的 interval 不建议低于 30s,否则 Thanos 降采样层会出现“空洞”,导致 P99 失真;可把 interval 提高到 120s,并在 Thanos Rule 侧加 --query.max-concurrent=20 限流。
  2. Camel 3.20+ 已支持 Micrometer Observation,把“路由事件”转成 Observation 后,可统一用 Spring Boot 3 的 @Observed 注解,此时指标、追踪、日志三合一,Recording Rule 可直接消费 http.server.requests 通用指标,实现“零”Camel 专有规则,方便后续迁移到 OpenTelemetry。
  3. 国内金融场景对“1 分钟延迟”敏感,可 在 Prometheus 侧开“并行查询”(--query.max-concurrency=CPU*2),同时给 Recording Rule 加 evaluation_delay: 10s,让指标比业务延迟只晚 10s,既满足监管“T+0”要求,又避免 Grafana 超时。