配置 Hive 的 JMX 指标到 Prometheus 并设置 SLA 告警
解读
国内中大型公司在 Hive 上跑离线数仓,SLA 通常要求 99.9% 任务在 08:30 前成功产出核心表。面试官想验证三件事:
- 能否把 Hive 运行时指标(JMX)无侵入地接到公司统一的 Prometheus;
- 能否根据指标设计可量化、可回卷的 SLA 规则;
- 能否在告警风暴与误报之间做权衡,体现“可运营”的运维思维。
回答必须落地到国产化环境:CentOS 7、JDK 8、Hadoop 3.x、Hive 3.1.x、Prometheus 2.40+、Alertmanager 0.25+,并考虑Kerberos 与 Ranger已开启的场景。
知识点
- Hive 内置 JMX MBean:
–metrics:service=HS2,name=active_sessions
–metrics:service=HS2,name=completed_queries_total
–metrics:service=HS2,name=metastore_api_slow_calls - JMX-to-Prometheus 主流方案:
– 独立 jmx_exporter 0.17(社区最稳,国内银行普遍采用)
– Hadoop 3.x 自带hadoop-prometheus-jmxjar(需改 hadoop-env,升级风险高) - Prometheus relabel 规则:
– 去掉无用 label,防止cardinality 爆炸(国内某厂曾因 hostname 未收敛导致 Prometheus OOM) - SLA 指标设计:
– 离线场景用**“任务结束时间”而非常规 QPS;
– 需区分调度层 SLA(Airflow/DolphinScheduler)与引擎层 SLA(Hive)**,本题聚焦后者。 - 告警治理:
– 使用 Alertmanager 的group_by: [cluster, job]抑制风暴;
– 关键告警需短信+语音,普通告警只发企业微信,并带**“一键拉群”**回调地址。
答案
一、无侵入接入 Prometheus
- 下载官方 jmx_exporter 0.17,上传至
/opt/hive/exporters/jmx_exporter.jar,权限 644,属主 hive:hadoop。 - 在
hive-env.sh追加:export HIVE_OPTS="$HIVE_OPTS -javaagent:/opt/hive/exporters/jmx_exporter.jar=9100:/opt/hive/conf/hive_jmx_config.yaml" - 编写
hive_jmx_config.yaml,只暴露 HiveServer2 与 Metastore 相关 MBean,过滤掉 Hadoop Common 指标:hostPort: localhost:9100 lowercaseOutputName: true rules: - pattern: "metrics<name=HS2\\.(.+)><>(\\w+)" name: "hive_hs2_$1" help: "HiveServer2 metric $1" type: GAUGE - pattern: "metrics<name=metastore\\.(.+)><>(\\w+)" name: "hive_metastore_$1" help: "Hive Metastore metric $1" type: GAUGE - 若开启 Kerberos,需给
hive.keytab追加 HTTP/hostname 主体,并在 jmx_exporter 启动参数里加:
防止 9100 端口被 krb5 拦截导致 Prometheus 拉取 401。-Djava.security.auth.login.config=/opt/hive/conf/jmx_jaas.conf
二、Prometheus 侧配置
在 prometheus.yml 新增 job:
- job_name: 'hive'
static_configs:
- targets: ['hivehs2-01:9100','hivehs2-02:9100']
labels:
cluster: 'bi_dw'
group: 'ads'
relabel_configs:
- source_labels: [__address__]
regex: '([^:]+):.*'
target_label: instance
replacement: '${1}'
注意:cluster 与 group 标签必须与公司 CMDB 对齐,方便后续做故障自愈(通过回调自动重启 HS2)。
三、SLA 指标计算
离线数仓 SLA 核心不是 QPS,而是**“核心表最晚产出时间”**。
- 在 Hive 层选择指标
hive_hs2_completed_queries_total与hive_hs2_active_sessions,并不能直接得到产出时间,需与调度平台联动。 - 因此采用**“代理指标”**方案:
– 每天 02:00–08:30 之间,若increase(hive_hs2_completed_queries_total{job="hive"}[1d])小于昨日同时段 50%,认为引擎侧可能卡住;
– 若hive_metastore_api_slow_calls > 100持续 5 min,则 Metastore 出现慢查询,极可能导致后续任务大面积延迟。 - 将上述两个条件记为
hive_engine_sla:0/1,通过 Recording Rule 固化:- record: hive:engine_sla_failure expr: | ( increase(hive_hs2_completed_queries_total[1d]) < 0.5 * increase(hive_hs2_completed_queries_total[1d] offset 1d) or hive_metastore_api_slow_calls > 100 ) and on (cluster) (hour() >= 2 and hour() < 8)
四、Alertmanager 告警规则
groups:
- name: hive_sla
interval: 60s
rules:
- alert: HiveEngineSLABreach
expr: hive:engine_sla_failure == 1
for: 5m
labels:
severity: critical
team: bigdata
oncall: "true"
annotations:
summary: "Hive 引擎层 SLA 可能受损"
description: "集群 {{ $labels.cluster }} 在 02:00–08:30 时段查询完成数下降 50% 或 Metastore 慢调用 >100,需立即介入。"
路由策略:
– group_by: [cluster, alertname]
– group_wait: 0s
– group_interval: 5m
– repeat_interval: 30m
– 关键告警走腾讯云短信+语音通道,并附带回调地址,一键拉企业微信群;非关键告警只发微信,避免凌晨扰民。
五、上线后验证
- 使用
jmxterm本地验证 MBean 值与 Prometheus 值一致,误差 < 2%。 - 模拟 Metastore 慢查询:
hive --hiveconf hive.metastore.client.socket.timeout=1s -e "select count(*) from big_partition_table",观察告警 5 min 内触发。 - 每周生成SLA 周报,记录 breach 次数、平均修复时长,纳入运维 KPI。
拓展思考
- 联邦集群场景:若 Hive 部署在两地三中心,Prometheus 使用 Thanos Receive 做聚合,需给每个中心加
replica标签,防止交叉重复告警。 - 指标与成本挂钩:把
hive_hs2_active_sessions与 Yarn 队列资源使用率做关联,白天低峰自动缩容 HS2 Pod,节省 20% 云主机费用。 - 智能预测:基于 30 天
completed_queries_total训练 Prophet 模型,提前 1 小时预测 SLA breach,从“事后抢修”变为“事前干预”。