【问题标题】:Prometheus json metrics普罗米修斯 json 指标
【发布时间】:2020-01-10 15:59:15
【问题描述】:

我要监控的应用程序提供了一个用于运行状况检查的 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 中的指标?

【问题讨论】:

    标签: prometheus prometheus-blackbox-exporter


    【解决方案1】:

    目前没有官方的导出器来抓取 JSON 端点。也许是因为它是easy to write one from scratch,并且任何通用解决方案都必须使用一些默认行为,例如从不考虑指标类型的数据路径构建指标名称;或任何相关标签来应用或解析日期等等。

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

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

    【讨论】:

      【解决方案2】:

      您可以使用 Prometheus JSON Exporter (https://github.com/prometheus-community/json_exporter) 调用您的服务并从 JSON 中提取值

      将 Prometheus JSON Exporter 部署到 Prometheus 可以拉取它并且 Exporter 可以访问您的 URL 的位置

      对于您的 JSON 示例,JSON Exporter 的 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 并享受你的指标

      【讨论】:

        猜你喜欢
        • 2020-11-12
        • 2022-12-02
        • 1970-01-01
        • 1970-01-01
        • 2022-12-22
        • 1970-01-01
        • 2022-01-22
        • 2020-12-23
        • 2023-02-01
        相关资源
        最近更新 更多