You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Flink 1.16 Prometheus指标基数过高问题求助

针对你遇到的动态标签导致基数爆炸的问题,以下是几个直接有效的解决方法:

1. 用Flink内置配置排除指定标签(推荐)

Flink 1.15及以上版本的PrometheusReporter原生支持通过配置过滤标签,无需编写代码。直接在flink-conf.yaml中添加以下配置,就能排除不需要的动态标签:

# 配置PrometheusReporter
metrics.reporter.prom.class: org.apache.flink.metrics.prometheus.PrometheusReporterFactory
metrics.reporter.prom.port: 9250
# 排除指定标签,多个用逗号分隔
metrics.reporter.prom.filter.labels.exclude: task_attempt_id,task_id,tm_id

这个配置会在指标导出阶段直接移除指定标签,从根源减少时间序列数量。

2. 自定义Reporter过滤标签(灵活扩展)

如果内置配置满足不了更复杂的过滤逻辑,可以自定义PrometheusReporter:

public class FilteredPrometheusReporter extends PrometheusReporter {
    @Override
    protected Map<String, String> filterLabels(Map<String, String> labels) {
        // 移除不需要的标签
        labels.remove("task_attempt_id");
        labels.remove("task_id");
        labels.remove("tm_id");
        // 也可以添加自定义过滤规则,比如只保留特定算子的标签
        return labels;
    }
}

编译打包后,在flink-conf.yaml中指定自定义Reporter类:

metrics.reporter.prom.class: com.yourcompany.metrics.FilteredPrometheusReporter
metrics.reporter.prom.port: 9250

3. 限制指标聚合维度

通过配置metrics.grouping,只保留业务需要的粗粒度维度,丢弃细粒度的task、task_attempt级指标:

# 仅保留job和operator级别的指标维度
metrics.grouping: job,operator

注意:这个配置会丢失task、TaskManager级别的细粒度监控数据,需要和SRE团队确认是否可以接受。

4. Prometheus采集阶段过滤

如果无法修改Flink配置,也可以在Prometheus端通过metric_relabel_configs在采集时丢弃不需要的时间序列:

scrape_configs:
  - job_name: 'flink'
    static_configs:
      - targets: ['flink-taskmanager:9250']
    metric_relabel_configs:
      # 丢弃包含task_attempt_id标签的指标
      - source_labels: [task_attempt_id]
        regex: '.*'
        action: drop
      # 同理丢弃其他不需要的标签对应的指标
      - source_labels: [task_id]
        regex: '.*'
        action: drop
      - source_labels: [tm_id]
        regex: '.*'
        action: drop

5. 禁用非核心指标

除了延迟指标,还可以禁用其他非必需的指标组,比如:

# 禁用checkpoint相关的细粒度指标(根据实际需求调整)
metrics.checkpointing.enabled: false
# 禁用TaskManager的JVM指标(如果已有其他监控采集JVM数据)
metrics.jvm.enabled: false

内容的提问来源于stack exchange,提问作者dotz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:41:08