0

我要监控的应用程序为运行状况检查提供了一个 api 端点,该端点以 json 中的指标进行响应。举个例子:

$ curl  https://example.com/api/stats
{"status":"success","code":0,"data":{"UserCount":140,"UserCountActive":23}}

我已经设置了 Prometheus blackbox_exporter 来监控这个端点是否返回,200 Ok但是我也希望获得这些指标。我了解仪器直接从应用程序导出这些数据。但是由于该应用程序已经在 json 对象中导出了我想要的内容,因此我更喜欢不维护我自己的这个软件的分支以包含检测所需的 Prometheus 库。我应该如何使用 json 中的指标?

4

2 回答 2

2

目前没有官方的导出器来抓取 JSON 端点。也许是因为从头开始编写一个很容易,并且任何通用解决方案都必须使用一些默认行为,例如从不考虑度量类型的数据的路径构建度量的名称;或任何相关标签来应用或解析日期等等。

您可以使用首选搜索引擎轻松找到可用的 JSON 导出器。他们可以很容易地替换 blackbox_exporter。考虑到提供的样品,它们应该很合适。

我想提一下的一种解决方案是exporter_exporter,因为我发现它对于在等待临时导出器时快速实现导出器很有用。它可用于执行生成普罗米修斯指标的脚本。在您的情况下,编写一个抓取 Json 端点并使用它在标准输出中编写相应的普罗米修斯格式的 python 脚本非常容易。

于 2019-09-08T20:45:30.010 回答
0

您可以使用 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 并享受你的指标

于 2021-09-15T19:51:58.573 回答