Apache Beam自定义指标无法在Flink仪表盘显示,如何导出?
问题描述
我基于Flink Runner运行Apache Beam管道,使用Java SDK自定义了指标,但无法在Flink仪表盘、Flink REST API响应或Prometheus中查看该指标,且Flink作业未处于分离模式。
不过能在Accumulators区域看到指标内容:
{ "metrics": { "attempted": [{ "urn": "beam:metric:user:sum_int64:v1", "type": "beam:metrics:sum_int64:v1", "payload": "FA==", "labels": { "NAMESPACE": "Bingo", "NAME": "bingotest", "PTRANSFORM": "log/ParMultiDo(LogOutput)" } }] } }
同时也能在Beam侧打印出该指标,当前使用Apache Beam 2.55.0 + Flink Runner 1.16版本。想问操作是否正确,或者遗漏了哪些步骤?
分析与解决方案
为什么只在Accumulators里看到指标?
这是Beam与Flink指标体系兼容的默认行为:Beam自定义指标默认仅作为Accumulators暴露,不属于Flink原生指标体系,因此不会出现在Flink仪表盘、REST API或Prometheus中——你当前的操作本身没错,但要让指标进入Flink监控体系,需要额外配置。
需要补充的步骤
启用Beam到Flink指标的桥接
在提交作业时开启指标桥接配置:- 代码层面设置PipelineOptions:
options.as(FlinkPipelineOptions.class).setMetricsFlinkIntegrationEnabled(true); - 命令行提交时添加参数:
--metricsFlinkIntegrationEnabled=true
该配置会让Beam将自定义指标注入Flink的MetricRegistry,使Flink能识别并暴露这些指标。
- 代码层面设置PipelineOptions:
配置Flink指标暴露规则
- 若使用Prometheus,修改
flink-conf.yaml开启Prometheus导出(示例配置):metrics.reporter.promgateway.class: org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporter metrics.reporter.promgateway.host: <你的Prometheus Push Gateway地址> metrics.reporter.promgateway.port: <对应端口> metrics.reporter.promgateway.jobName: <你的作业名称> metrics.reporter.promgateway.randomJobNameSuffix: true metrics.reporter.promgateway.deleteOnShutdown: false - 若要在Flink仪表盘查看,确保Flink WebUI配置正常,作业运行时可正常访问WebUI。
- 若使用Prometheus,修改
了解Beam指标的Flink映射规则
桥接后,Beam指标会按以下规则转换为Flink指标:- 指标名称格式:
beam_<namespace>_<name>(比如你的指标会变成beam_Bingo_bingotest) - 标签会保留Beam原有的维度(如
ptransform对应原PTRANSFORM标签)
可通过Flink REST API的/jobs/<job-id>/metrics端点搜索对应命名的指标。
- 指标名称格式:
确认版本兼容性
Beam 2.55.0与Flink 1.16的组合是兼容的,但需确保Flink Runner依赖与Flink集群版本完全一致,避免版本不匹配导致指标桥接失效。
验证流程
- 带
metricsFlinkIntegrationEnabled=true配置重新提交作业 - 打开Flink WebUI的Metrics标签页,搜索转换后的指标名称
- 检查Prometheus是否已拉取到对应指标
内容的提问来源于stack exchange,提问作者Aditya Shirsath
相关产品推荐
相关产品推荐

