json_exporter执行JSONPath失败求助:无法提取Healthcheck的description字段
json_exporter配置问题排查与修复
问题背景
需要通过json_exporter抓取指定URL的Healthcheck信息,提取其中的description字段用于告警邮件展示故障详情。
原始Healthcheck返回数据:
{ "status": "OK", "description": "All good", "sql": null }
使用以下yaml配置后出现错误:
modules: default: headers: MyHeader: MyHeaderValue metrics: - name: jsonhc type: object help: healthcheks description path: '{.description}' labels: env: prod values: status: 1 description: '{.description}'
错误日志:
json_exporter | ts=2023-06-22T11:12:38.760Z caller=collector.go:136 level=error msg="Failed to execute jsonpath" err="description is not found" path={.description} data=""All good"" json_exporter | ts=2023-06-22T11:12:38.761Z caller=collector.go:88 level=error msg="Failed to extract value for metric" path={.description} err="description is not found" metric="Desc{fqName: "jsonhc_description", help: "healthcheks description", constLabels: {}, variableLabels: [env]}
调用curl http://localhost:7979/probe时返回"Target parameter is missing",修改path为'$.*'、'{ [*] }'均无效。
错误分析
- 配置逻辑错误:
type: object表示要处理的是完整JSON对象,但当前path: '{.description}'指向的是字符串类型的字段值,而非整个JSON对象。此时在values里再用{.description}解析字符串,自然找不到对应字段。 - probe接口调用错误:
/probe接口必须携带target参数指定要抓取的Healthcheck URL,否则会返回参数缺失错误。
修复方案
1. 修正json_exporter配置
将path改为指向整个JSON根对象,同时调整values字段映射逻辑(建议将status转为数值型,更适合Prometheus告警判断):
modules: default: headers: MyHeader: MyHeaderValue metrics: - name: jsonhc type: object help: Healthcheck status and description path: '$' # 指向整个JSON对象 labels: env: prod values: # 将status转为数值:OK为1,非OK为0 status: '{.status}' status_code: 'if({.status} == "OK", 1, 0)' description: '{.description}'
若仅需保留description和固定status值,可简化为:
modules: default: headers: MyHeader: MyHeaderValue metrics: - name: jsonhc type: object help: healthcheks description path: '$' labels: env: prod values: status: 1 description: '{.description}'
2. 正确调用probe接口
执行curl命令时必须携带target参数指定Healthcheck的URL,若使用非默认模块需同时指定module:
curl "http://localhost:7979/probe?target=http://your-healthcheck-endpoint-url&module=default"
验证
执行修正后的curl命令后,应能看到类似如下格式的指标输出:
# HELP jsonhc_status Healthcheck status and description # TYPE jsonhc_status gauge jsonhc_status{env="prod"} 1 # HELP jsonhc_status_code Healthcheck status and description # TYPE jsonhc_status_code gauge jsonhc_status_code{env="prod"} 1 # HELP jsonhc_description Healthcheck status and description # TYPE jsonhc_description info jsonhc_description{description="All good",env="prod"} 1
内容的提问来源于stack exchange,提问作者Skiv
相关产品推荐
相关产品推荐

