【问题标题】:Does Prometheus allow you to scrape JSON information from an endpoint?Prometheus 是否允许您从端点抓取 JSON 信息?
【发布时间】:2017-07-13 14:27:05
【问题描述】:

我正在使用 Prometheus 来检测 Node.js 应用程序以进行监控。我目前正在使用以下 Node.js 客户端进行检测:

prom-client

我已将所有内容配置为从我的 Node.js 应用程序收集和收集默认指标,并且监控按预期工作。我想知道 Prometheus 是否可以从我的应用程序公开的端点抓取 JSON。

例如,Node.js 应用程序有一个运行状况检查端点 (/health),它返回有关应用程序整体运行状况及其依赖项的简单 JSON 数据(布尔值或 0/1)。我可以将 Prometheus 和/或 prom-client 配置为从健康端点抓取 JSON,然后根据该信息记录指标吗?

【问题讨论】:

    标签: json node.js monitoring prometheus


    【解决方案1】:

    我相信你可以。

    我在下面链接的博文详细介绍了如何使用 Prometheus Python 客户端将 JSON 格式的指标提取到 Prometheus 中。

    https://www.robustperception.io/writing-a-jenkins-exporter-in-python/ https://www.robustperception.io/writing-json-exporters-in-python/

    【讨论】:

    • 感谢您的信息。我可能会做类似的事情,但使用节点客户端
    【解决方案2】:

    我能够使用prom-client 找到解决方案并构建我自己的自定义指标。将为可能有兴趣这样做的任何人提供下面的示例。假设有一个健康检查端点返回以下 JSON:

    {
        "app": {
            "message": "Service is up and running!",
            "success": true
        }
    }
    

    我使用包 request 来调用端点,解析数据并创建一个仪表以反映基于健康检查状态的值。以下是 JavaScript 中 /metrics 端点的示例:

    const express = require('express');
    const router = express.Router();
    const request = require('request');
    
    // Config for health check endpoint
    const healthCheckURL = 'https://SOME_ENDPOINT/health';
    const zone = 'DEV';
    
    // Initialize Prometheus
    const Prometheus = require('prom-client');
    const collectDefaultMetrics = Prometheus.collectDefaultMetrics;
    collectDefaultMetrics({
        timeout: 5000
    });
    
    router.get('/', (req, res) => {
        res.end(Prometheus.register.metrics());
    });
    
    const serviceHealthGauge = new Prometheus.Gauge({
        name: 'service_health',
        help: 'Health of service component',
        labelNames: ['zone']
    });
    
    setInterval(() => {
        request({
                url: healthCheckURL,
                method: "GET",
            },
            function(error, response, body) {
                if (!error && response.statusCode == 200) {
                    const JSONBody = JSON.parse(body);
    
                    // check service health
                    if (JSONBody.app && JSONBody.app.success) {
                        serviceHealthGauge.set({
                            zone: zone
                        }, 1);
                    } else {
                        serviceHealthGauge.set({
                            zone: zone
                        }, 0);
    
                    }
                } else {
                    serviceHealthGauge.set({
                        zone: zone
                    }, 0);
                }
            }   
        );
      }, 10000);
    
    module.exports.metricNames = ['service_health'];
    
    module.exports = router;
    

    【讨论】:

      【解决方案3】:

      如果您想让 prom-client 收集这些信息,您可以查看库中的 heap sizes 收集器。

      如果此收集器获取堆大小,您可以改为抓取 JSON 端点,或者直接调用 JSON 端点后面的功能来发布一些带有 0 或 1 的仪表。

      【讨论】:

      • 我将尝试按照您的建议使用 prom-client 创建自己的自定义指标。感谢您的信息。
      • @JoshuaAlger - 成功了吗?请发表评论并考虑接受此回答。
      • 我用我的发现回答了以下问题。您的回答有所帮助,但最终不是我的解决方案
      【解决方案4】:

      不是直接的,因为 Prometheus 只理解他们的文本格式或 GRPC 格式。见https://prometheus.io/docs/instrumenting/exposition_formats/

      当然,也可以编写一个翻译“桥梁”或导出器,以这种格式翻译 JSON 结构,就像他的回答中描述的 @ConorB。

      【讨论】:

      【解决方案5】:

      【讨论】:

      猜你喜欢
      • 2022-01-15
      • 2022-09-23
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2017-06-03
      • 2022-12-21
      相关资源
      最近更新 更多