
prometheuscommunity/json-exporterjson_exporter 是由 Prometheus 社区开发的一款指标导出工具(exporter),用于通过 JSONPath 表达式从远程 JSON 数据源抓取数据,并将其转换为 Prometheus 可识别的指标格式。该工具支持灵活的模块配置,可针对不同结构的 JSON 数据定义独立的提取规则,是连接 JSON 数据源与 Prometheus 监控体系的关键组件。
构建并启动 exporter(本地编译方式):
console$ make build $ ./json_exporter --config.file examples/config.yml &
启动测试 HTTP 服务器(用于提供 JSON 测试数据):
console$ python3 -m http.server 8000 & Serving HTTP on :: port 8000 (http://[::]:8000/) ...
通过 probe 端点指定 default 模块和目标 JSON 数据源:
console$ curl "http://localhost:7979/probe?module=default&target=http://localhost:8000/examples/data.json"
返回指标示例:
# HELP example_global_value Example of a top-level global value scrape in the json # TYPE example_global_value untyped example_global_value{environment="beta",location="planet-mars"} 1234 # HELP example_timestamped_value_count Example of a timestamped value scrape in the json # TYPE example_timestamped_value_count untyped example_timestamped_value_count{environment="beta"} 2 # HELP example_value_active Example of sub-level value scrapes from a json # TYPE example_value_active untyped example_value_active{environment="beta",id="id-A"} 1 example_value_active{environment="beta",id="id-C"} 1 ...
针对不同 JSON 结构使用 animals 模块:
console$ curl "http://localhost:7979/probe?module=animals&target=http://localhost:8000/examples/animal-data.json"
返回指标示例:
# HELP animal_population Example of top-level lists in a separate module # TYPE animal_population untyped animal_population{name="deer",predator="false"} 456 animal_population{name="lion",predator="true"} 123 animal_population{name="pigeon",predator="false"} 789
启动 Prometheus 容器,挂载包含 json_exporter 抓取配置的 prometheus.yml:
console$ docker run --rm -it -p 9090:9090 -v $PWD/examples/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
访问 Prometheus UI:
可将 JSON 中的字段指定为指标的 Unix 时间戳(int64 类型),但需注意:
可通过配置启用 HTTPS,具体 TLS 参数配置详见 exporter-toolkit/web 文档。
当配置 modules.<module_name>.body 参数时,exporter 将以 POST 方法发送请求,请求体内容由 body.content 定义,支持以下特性:
yamlbody: content: | My static information: {"time_diff": "1m25s", "anotherVar": "some value"}
需设置 body.templatize: true,可使用 Go Template 及 Sprig 库 函数:
yamlbody: content: | {"time_diff": "{{ duration `95` }}","anotherVar": "{{ randInt 12 30 }}"} templatize: true
Prometheus 发送给 exporter 的查询参数可作为模板变量使用:
yamlbody: content: | {"time_diff": "{{ duration `95` }}","anotherVar": "{{ .myVal | first }}"} templatize: true
请求示例:
console$ curl "[***]"
实际发送的 POST 请求体:
{"time_diff": "1m35s","anotherVar": "something"}
console$ docker run -v $PWD/examples/config.yml:/config.yml quay.io/prometheuscommunity/json-exporter --config.file=/config.yml
-v $PWD/examples/config.yml:/config.yml:挂载本地配置文件到容器内--config.file=/config.yml:指定 exporter 使用的配置文件路径--config.file:(必选)指定 JSON exporter 的配置文件路径,配置文件需包含模块定义、JSONPath 规则等body.content:POST 请求体内容,支持静态文本或模板字符串body.templatize:布尔值,是否启用模板化处理(默认 false),启用后 body.content 将作为 Go Template 解析





manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务