您可以使用 Prometheus JSON Exporter ( https://github.com/prometheus-community/json_exporter ) 调用您的服务并从 JSON 中提取值
部署 Prometheus JSON Exporter,它可以被 Prometheus 拉取,Exporter 可以访问你的 URL
对于 JSON Exporter 的 JSON 示例 config.xml 将类似于
---
metrics:
- name: user_count
path: "{$.data.UserCount}"
type: value
help: UserCount value
- name: user_count_active
path: "{$.data.UserCountActive}"
type: value
help: UserCountActive value
并在 Prometheus (prometheus.yml) 中抓取配置:
## gather the metrics from third party json sources, via the json exporter
- job_name: json_user_stat
metrics_path: /probe
static_configs:
- targets:
# URL of each API for json exporter
- https://example.com/api/stats
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
# Location of the json exporter's real <hostname>:<port> from Prometheus
replacement: json_exporter:7979
首先通过点击 URL 测试您的导出器(如果您想在浏览器之外使用,请对“目标”值进行编码,浏览器将自动编码)http://json_exporter:7979/probe?target=https://example.com/api /stats 并检查输出
# HELP UserCount value
# TYPE logstash_audit_events_in untyped
user_count{} 140
# HELP lUserCountActive value
# TYPE logstash_audit_events_out untyped
user_count_active{} 23
如果你明白了 - 在 Prometheus 中配置 scape 并享受你的指标