json_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 解析来自真实用户的反馈,见证轩辕镜像的优质服务
免费版仅支持 Docker Hub 加速,不承诺可用性和速度;专业版支持更多镜像源,保证可用性和稳定速度,提供优先客服响应。
免费版仅支持 docker.io;专业版支持 docker.io、gcr.io、ghcr.io、registry.k8s.io、nvcr.io、quay.io、mcr.microsoft.com、docker.elastic.co 等。
当返回 402 Payment Required 错误时,表示流量已耗尽,需要充值流量包以恢复服务。
通常由 Docker 版本过低导致,需要升级到 20.x 或更高版本以支持 V2 协议。
先检查 Docker 版本,版本过低则升级;版本正常则验证镜像信息是否正确。
使用 docker tag 命令为镜像打上新标签,去掉域名前缀,使镜像名称更简洁。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
在 Linux 系统配置镜像加速服务
在 Docker Desktop 配置镜像加速
Docker Compose 项目配置加速
Kubernetes 集群配置 Containerd
在宝塔面板一键配置镜像加速
Synology 群晖 NAS 配置加速
飞牛 fnOS 系统配置镜像加速
极空间 NAS 系统配置加速服务
爱快 iKuai 路由系统配置加速
绿联 NAS 系统配置镜像加速
QNAP 威联通 NAS 配置加速
Podman 容器引擎配置加速
HPC 科学计算容器配置加速
ghcr、Quay、nvcr 等镜像仓库
无需登录使用专属域名加速
需要其他帮助?请查看我们的 常见问题 或 官方QQ群: 13763429