写点什么

Prometheus Exporter (三十四)JSON Exporter

作者:耳东@Erdong
  • 2021 年 12 月 30 日
  • 本文字数:2496 字

    阅读完需:约 8 分钟

Prometheus Exporter (三十四)JSON Exporter

本文已经收录在 Prometheus 合集 Prometheus 都可以采集那些指标?-- 常用 Exporter 合集 中。


JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据,简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。


JSON


是我们经常使用的一种格式,针对这种格式 Prometheus 社区开发提供了 JSON Exporter 通过 JSONPath 来获取远端的 JSON 数据。官方仓库地址是 https://github.com/prometheus-community/json_exporter ,最新版本是 v0.3.0 ,发布于 2021 年 2 月 12 日。


JSON Exporter 通过 JSONPath 来获取远端的 JSON 数据,要查看这个导出器支持的 JSON Path 配置,请访问这里 https://kubernetes.io/docs/reference/kubectl/jsonpath/


查看示例导出器配置、Prometheus 配置和预期数据格式的示例目录。


配置语法在 0.3.x 版本中已经更改。如果从 0.2.x 迁移,那么请使用上面提到的 JSONPath 指导正确的配置语法。

安装运行

我们来看个例子,假设要采集的 JSON 数据文件 data.json 内容如下:


{    "counter": 1234,    "values": [        {            "id": "id-A",            "count": 1,            "some_boolean": true,            "state": "ACTIVE"        },        {            "id": "id-B",            "count": 2,            "some_boolean": true,            "state": "INACTIVE"        },        {            "id": "id-C",            "count": 3,            "some_boolean": false,            "state": "ACTIVE"        }    ],    "location": "mars"}
复制代码


那么 JSON Exporter 的配置文件 config.yml 内容如下:


---metrics:- name: example_global_value  path: "{ .counter }"  help: Example of a top-level global value scrape in the json  labels:    environment: beta # static label    location: "planet-{.location}"          # dynamic label
- name: example_value type: object help: Example of sub-level value scrapes from a json path: '{.values[?(@.state == "ACTIVE")]}' labels: environment: beta # static label id: '{.id}' # dynamic label values: active: 1 # static value count: '{.count}' # dynamic value boolean: '{.some_boolean}'
headers: X-Dummy: my-test-header
复制代码


我们来启动一个简单的 Web 服务 python -m SimpleHTTPServer 8000 &


这个时候 JSON Exporter 的启动方式如下:


./json_exporter --config.file examples/config.yml
复制代码


JSON Exporter 启动以后默认会在 7979 暴露自己的监控数据,和其它 Exporter 一样,可以通过 Curl 来查看指标内容


$ curl "http://localhost:7979/probe?target=http://localhost:8000/examples/data.json" | grep ^example

example_global_value{environment="beta",location="planet-mars"} 1234example_value_active{environment="beta",id="id-A"} 1example_value_active{environment="beta",id="id-C"} 1example_value_boolean{environment="beta",id="id-A"} 1example_value_boolean{environment="beta",id="id-C"} 0example_value_count{environment="beta",id="id-A"} 1example_value_count{environment="beta",id="id-C"} 3
复制代码


这个时候可以配置 Prometheus 来收集数据,Prometheus 的配置文件 prometheus.yml 内容可以参考


scrape_configs:
## gather metrics of prometheus itself- job_name: prometheus static_configs: - targets: - localhost:9090
## gather the metrics of json_exporter application itself- job_name: json_exporter static_configs: - targets: - localhost:7979 ## Location of the json exporter's real <hostname>:<port>
## gather the metrics from third party json sources, via the json exporter- job_name: json metrics_path: /probe static_configs: - targets: - http://host-1.foobar.com/dummy/data.json - http://host-2:8000/other-examples/data.json - http://localhost:8000/examples/data.json ## Used from the example steps in Readme relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: localhost:7979 ## Location of the json exporter's real <hostname>:<port>
复制代码

通过 HTTP POST 发送 body 内容

如果在配置中设置了 body 参数,它将作为刮擦请求中的 body 内容由 Exporter 发送。在本例中,HTTP 方法也将被设置为“POST”。


body:  content: |    My static information: {"time_diff": "1m25s", "anotherVar": "some value"}
复制代码


Body 内容也可以是一个 Go 模板,模板中可以使用 Sprig 库中的所有函数。在呈现模板时,prometheus 在刮擦查询中发送给 Exporter 的所有查询参数都可以作为值使用。


使用模板函数的例子


body:  content: |    {"time_diff": "{{ duration `95` }}","anotherVar": "{{ randInt 12 30 }}"}  templatize: true
复制代码


使用查询参数赋值的模板函数示例:


body:  content: |    {"time_diff": "{{ duration `95` }}","anotherVar": "{{ .myVal | first }}"}  templatize: true
复制代码


使用 curl 请求 "http://exporter:7979/probe?target=http://scrape_target:8080/test/data.json&myVal=something",


会导致将下面的 body 作为 HTTP POST 有效负载发送到 http://scrape_target:8080/test/data.json:


{"time_diff": "1m35s","anotherVar": "something"}.
复制代码

小结

JSON 本身并不复杂,采集 JSON 文件的内容也是通过启动了一个程序提供了端口,然后再去采集。或者通过 JSON Exporter 采集其他应用的 JSON 格式的输出内容,但是格式还是要符合监控要求的。

发布于: 刚刚
用户头像

耳东@Erdong

关注

还未添加个人签名 2020.05.24 加入

主要研究分享运维技术,专注于监控、CICD、操作系统、云原生领域,公众号【耳东学堂】,知识星球同名,坚持原创,希望能和大家在运维路上结伴而行 邮箱:erdong@mail.erdong.site

评论

发布
暂无评论
Prometheus Exporter (三十四)JSON Exporter